Exemplo n.º 1
0
Arquivo: banker.c Projeto: esfdk/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);
  int j;
  for(j = 0; j < n; j++)
  {
	if(request[j] > s->need[i][j])
	{
		fprintf(stderr, "Request higher than Need for process %d!", i);
		pthread_mutex_unlock(&state_mutex);
		exit(0);
	}
  }
  
  for(j = 0; j < n; j++)
  {
	if(request[j] > s->available[j])
	{
		pthread_mutex_unlock(&state_mutex);
		return 0;
	}
  }
  
  for(j = 0; j < n; j++)
  {
	s->available[j] = s->available[j] - request[j];
	s->allocation[i][j] = s->allocation[i][j] + request[j];
	s->need[i][j] = s->need[i][j] - request[j];
  }
  
  if(!safety_check())
  {
	for(j = 0; j < n; j++)
	{
		s->available[j] = s->available[j] + request[j];
		s->allocation[i][j] = s->allocation[i][j] - request[j];
		s->need[i][j] = s->need[i][j] + request[j];
	}
	pthread_mutex_unlock(&state_mutex);
	return 0;
  }
  else
  {
	pthread_mutex_unlock(&state_mutex);
	return 1;
  }
}
Exemplo n.º 2
0
void PAGE_ShowSafetyDialog()
{
    if (dialog) {
        u64 unsafe = safety_check();
        if (! unsafe) {
            DialogClose(dialog, 0);
            safety_confirmed();
        } else {
            safety_string_cb(NULL, NULL);
            u32 crc = Crc(tempstring, strlen(tempstring));
            if (crc != dialogcrc) {
                GUI_Redraw(dialog);
                dialogcrc = crc;
            }
        }
    } else {
        tempstring[0] = 0;
        dialogcrc = 0;
        dialog = GUI_CreateDialog(&gui->dialog, DIALOG1_X, DIALOG1_Y,
                 DIALOG1_WIDTH, DIALOG1_HEIGHT, NULL, safety_string_cb, safety_ok_cb, dtOk, NULL);
    }
}
Exemplo n.º 3
0
Arquivo: banker.c Projeto: esfdk/BOSC
int main(int argc, char* argv[])
{
  /* Get size of current state as input */
  int i, j;
  printf("Number of processes: \n");
  scanf("%d", &m);
  printf("Number of resources: \n");
  scanf("%d", &n);

  /* Allocate memory for state */
  s = malloc(sizeof(State));
  s->resource = malloc(sizeof(int) * n);
  s->available = malloc(sizeof(int) * n);
  s->max = malloc(sizeof(int *) * m);
  s->allocation = malloc(sizeof(int *) * m);
  s->need = malloc(sizeof(int *) * m);

  for(i = 0; i < m; i++)
  {
	s->max[i] = malloc(sizeof(int) * n);
	s->allocation[i] = malloc(sizeof(int) * n);
	s->need[i] = malloc(sizeof(int) * n);
  }
  
  /* Get current state as input */
  printf("Resource vector: \n");
  for(i = 0; i < n; i++)
    scanf("%d", &s->resource[i]);
  printf("Enter max matrix: \n");
  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(safety_check())
  {
    printf("State was safe. \n");
  }
  else
  {
	fprintf(stderr, "State was not safe.\n");
	return 1;
  }

  /* Seed the random number generator */
  struct timeval tv;
  gettimeofday(&tv, NULL);
  srand(tv.tv_usec);
  
  /* Create m threads */
  pthread_t *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 */
  pthread_exit(0);
  free(tid);

  /* Free state memory */
  	for (i = 0; i < m; i++) {
		free(s->max[i]);
		free(s->allocation[i]);
		free(s->need[i]);
	}

	free(s->resource);
	free(s->available);
	free(s->max);
	free(s->allocation);
	free(s->need);
	free(s);
}