/* the philosopher thread function - create N threads, each of which calls 
 * this function with its philosopher number 0..N-1
 */
void *philosopher_thread(void *context) 
{
    int philosopher_num = (int)context; /* hack... */

    while (1) {
        sleep_exp(4.0);
        get_forks(philosopher_num);
        sleep_exp(2.5);
        release_forks(philosopher_num);
    }
    
    return 0;
}
Esempio n. 2
0
/* the customer thread function - create 10 threads, each of which calls
 * this function with its customer number 0..9
 */
void *customer_thread(void *context) 
{
    int customer_num = (int)context; 
    while(1){
       
       /* maintaing a 10 secs gap between creation of two customer threads */ 
       sleep_exp(10.0, NULL);
       
       /* calling the customer function with the customer number as the argument */ 
       customer(customer_num);
    }

    return 0;
}
Esempio n. 3
0
/* the barber method
 */
void barber(void)
{
    
    /* locking the mutex */
    pthread_mutex_lock(&m);

    while (1) {

        if( customers_in_shop == 0)
        {
           printf("DEBUG: %f barber goes to sleep \n", timestamp() );
           
           /* barber waiting for a customer to enter the shop */
           pthread_cond_wait(&Barber, &m);

           /* barber is signaled by the customer to wake up and it wakes up to cut hair */
           printf("DEBUG: %f barber wakes up \n", timestamp());
        }

        // signal to start haircut of customer
        pthread_cond_signal(&haircut_start);

        /* incerment the barber's chair status to denote that its occupied*/
        stat_count_incr(bchair_stat);
        
        /* time what barber takes to cut hair */
        sleep_exp(1.2, &m);
  
        /* barber signal the customer on completion of the hair cut */
        pthread_cond_signal(&haircut_done);

        /* barber waits inorder to check the status of number of customers in shop */
        pthread_cond_wait(&c_left, &m);
    }
  
    /* unlock the mutex before returning from the function */
    pthread_mutex_unlock(&m);
}