Esempio n. 1
0
void* run_a_baboons(void* thread_id) {
  //unsigned short xsubi[3];
  // print_thread_info("About to start", "A to B", thread_id);
  sleep_rand(1000);
  sem_wait(&mutex);
  if ((curr_dir == a_to_b || curr_dir == none) && xing_count < 5 && (xed_count + xing_count) < 10) {
    curr_dir = a_to_b;
    xing_count++;
    sem_post(&mutex);
  } else {
    //print_thread_info("Waiting to cross", "B to A", thread_id);
    to_b_wait_count++;
    sem_post(&mutex);
    sem_wait(&to_b);
    to_b_wait_count--;
    xing_count++;
    curr_dir = a_to_b;
    sem_post(&mutex);
  }


  int valp;
  sem_getvalue(&mutex, &valp);
  //fprintf(stderr, "thread id: %d, mutex: %d\n", *((int*)thread_id), valp);

  print_thread_info("Crossing", "A to B", thread_id);
  sleep_rand(10000);
  print_thread_info("Done crossing", "A to B", thread_id);

  sem_getvalue(&mutex, &valp);
  //fprintf(stderr, "thread id: %d, mutex: %d\n", *((int*)thread_id), valp);

  sem_wait(&mutex);
  xed_count++;
  xing_count--;
  if (xing_count > 0 && xing_count <= 4 && (((xed_count + xing_count) < 10 && to_b_wait_count == 0) || 
					    ((xed_count + xing_count) >= 10 && to_a_wait_count != 0))) {
    sem_post(&mutex);
  } else if (xing_count >= 0 &&  xing_count <= 4 && (((xed_count + xing_count) < 10 && to_b_wait_count != 0) || 
						   ((xed_count+xing_count) >= 10 && to_a_wait_count == 0 && to_b_wait_count != 0))) {
    sem_post(&to_b);
  } else if ((xing_count == 0 && (((xed_count + xing_count) < 10 && to_b_wait_count == 0 && to_a_wait_count != 0) || 
				  ((xed_count+xing_count) >= 10 && to_a_wait_count != 0)))) {
    curr_dir = b_to_a;
    xed_count = 0;
    sem_post(&to_a);    
  } else if (xing_count == 0 && to_b_wait_count == 0 && to_a_wait_count == 0) {
    curr_dir = none; 
    xed_count = 0; 
    sem_post(&mutex);
  } else {
    print_thread_info("ERROR", "A to B", thread_id);
    fprintf(stderr, "xed_count: %d, xing_count: %d, to_a: %d, to_b: %d, xing_dir: %d\n", xed_count, xing_count, to_a_wait_count, to_b_wait_count, curr_dir);
    exit(1);
  }


  pthread_exit(0);
  return NULL;
}
Esempio n. 2
0
 void think() 
 {
     if (debugflag) std::printf("[%d] thinking\n", id);
        sleep_rand(think_delay_max);
     if (debugflag) std::printf("[%d] hungry\n", id);
     wait_start = std::chrono::high_resolution_clock::now();
 }
Esempio n. 3
0
 void eat() 
 {
     philstat->wait_time += std::chrono::duration_cast<std::chrono::milliseconds>
                            (std::chrono::high_resolution_clock::now() - wait_start).count();
     if (debugflag) std::printf("[%d] eating\n", id);
     sleep_rand(eat_delay_max);
     philstat->eat_count++;
 }
Esempio n. 4
0
File: callout.c Progetto: zackb/code
void do_round(long minutes)    {
    time_t start = time(NULL);
    time_t end = start + (minutes * 60);
    int total_punches_start = total_punches;
    while (1)    {
        time_t now = time(NULL);
        if ((end - now) < 1)  {
            break;
        }
        callout(choose_combo());
        sleep_rand(5);
    }
    say("stop");
    int total_punches_round = total_punches - total_punches_start;
    char phrase[BUFFER_SIZE];
    snprintf(phrase, BUFFER_SIZE, "total punches that round %i", total_punches_round);
    say(phrase);
    snprintf(phrase, BUFFER_SIZE, "total punches %i", total_punches);
    say(phrase);
}