Example #1
0
File: banker.c Project: Jelb/BOSC
/* Allocate resources in request for process i, only if it 
   results in a safe state and return 1, else return 0 */
int resource_request(int i, int *request)
{    
    pthread_mutex_lock(&state_mutex);
    
    if(islarger(request, s->need[i], n)) {
        printf("¡¡¡ERROR!!!\n");
        printf("Process %i's request is greather than its need.\n", i);
        killProcesses();
    }
    
    subArr(s->available, request, n);
    addArr(s->allocation[i], request, n);
    subArr(s->need[i], request, n);
    
    if (safe_state(s)) {
        pthread_mutex_unlock(&state_mutex);
        return 1;
    } else {
        addArr(s->available, request, n);
        subArr(s->allocation[i], request, n);
        addArr(s->need[i], request, n);
        pthread_mutex_unlock(&state_mutex);
        return 0;
    }
}
static int is_no_deadlock(process *proc)
{
	matrix_t cpy_claim[NPROC][NRES];
	matrix_t cpy_alloc[NPROC][NRES];
	matrix_t cpy_available[NRES];

	cpy_matrix(claim_matrix, cpy_claim);
	cpy_matrix(allocation_matrix, cpy_alloc);
	cpy_row_vector(available_vector, cpy_available);

	/* simulate the allocation of resources to this process */
	increment_row_vector(proc->request_vector, cpy_alloc[proc->row_index]);
	decrement_row_vector(proc->request_vector, cpy_available);

	return (safe_state(cpy_claim, cpy_alloc, cpy_available));
}
// Define functions declared in banker.h here
bool request_res(int n_customer, int request[]) {
  //Available = 1, Unavailable = 0
  bool rquest = false;
  for(int i = 0; i < NUM_RESOURCES; i++) {
    if(request[i] <= bank.need[n_customer][i]) {
      //while(request[i] > bank.available[i]);
      if(request[i] <= bank.available[i]) {
        #pragma omp critical {
          bank.available[i] -= request[i];
          bank.allocation[n_customer][i] += request[i];
          bank.need[n_customer][i] -= request[i];
          if(safe_state(n_customer)) {
            rquest = true;
          } else {
            bank.available[i] += request[i];
            bank.allocation[n_customer][i] -= request[i];
            bank.need[n_customer][i] += request[i];
          }
        }
      }
    } else {
Example #4
0
File: banker.c Project: Jelb/BOSC
int main(int argc, char* argv[])
{
    /* Get size of current state as input */
    int i, j;
    printf("Number of processes: ");
    scanf("%d", &m);
    printf("Number of resources: ");
    scanf("%d", &n);

    /* Allocate memory for state */
    s             = (State *) malloc(sizeof(State));
    s->resource   = (int *)malloc(n * sizeof(int));
    s->available  = (int *)malloc(n * sizeof(int));
    s->max        = (int **)malloc(m * sizeof(int *));
    s->allocation = (int **)malloc(m * sizeof(int *));
    s->need       = (int **)malloc(m * sizeof(int *));
    
    for (i = 0; i < m; i++) {
        s->max[i]           = (int *) malloc(n * sizeof(int));
        s->allocation[i]    = (int *) malloc(n * sizeof(int));
        s->need[i]          = (int *) malloc(n * sizeof(int));
    }
    
    if (s == NULL) { printf("\nYou need to allocate memory for the state!\n"); exit(0); };

    /* Get current state as input */
    printf("Resource vector: ");
    for(i = 0; i < n; i++)
        scanf("%d", &s->resource[i]);
    printf("Enter max matrix: ");
    for(i = 0;i < m; i++)
        for(j = 0;j < n; j++)
            scanf("%d", &s->max[i][j]);
    printf("Enter allocation matrix: ");
    for(i = 0; i < m; i++)
        for(j = 0; j < n; j++)
            scanf("%d", &s->allocation[i][j]);
    printf("\n");

    /* Calcuate the need matrix */
    for(i = 0; i < m; i++)
        for(j = 0; j < n; j++)
            s->need[i][j] = s->max[i][j]-s->allocation[i][j];

    /* Calcuate the availability vector */
    for(j = 0; j < n; j++) {
        int sum = 0;
        for(i = 0; i < m; i++)
            sum += s->allocation[i][j];
        s->available[j] = s->resource[j] - sum;
    }

    /* Output need matrix and availability vector */
    printf("Need matrix:\n");
    for(i = 0; i < n; i++)
        printf("R%d ", i+1);
    printf("\n");
    for(i = 0; i < m; i++) {
        for(j = 0; j < n; j++)
            printf("%d  ",s->need[i][j]);
        printf("\n");
    }
    printf("Availability vector:\n");
    for(i = 0; i < n; i++)
        printf("R%d ", i+1);
    printf("\n");
    for(j = 0; j < n; j++)
        printf("%d  ",s->available[j]);
    printf("\n");

    /* If initial state is unsafe then terminate with error */
    if(!safe_state(s)) {
        printf("Start state is unsafe\n");
    } else {
        pthread_mutex_init(&state_mutex, NULL);
        
        /* Seed the random number generator */
        struct timeval tv;
        gettimeofday(&tv, NULL);
        srand(tv.tv_usec);
      
        /* Create m threads */
        tid = malloc(m*sizeof(pthread_t));
        for (i = 0; i < m; i++)
            pthread_create(&tid[i], NULL, process_thread, (void *) (long) i);
      
        /* Wait for threads to finish */
        for (i = 0; i < m; i++)
            pthread_join(tid[i],NULL);
        printf("Something went wrong processes has terminated.\n");
    }
    /* Free state memory */
    free(s->resource);
    free(s->available);
    for (i = 0; i < m; i++) {
        free(s->max[i]);
        free(s->allocation[i]);
        free(s->need[i]);
    }
    free(s);
    sem_destroy(&state_mutex);
    exit(1);
}