classic IPC problem using threads in C

Hello all. I am designing the sleeping barber problem. I need to implement the solution using synchronization primitives (pthread_mutex_t for mutal exclusion).
Implementation must consist of a control program that:
1) initializes the synchronization variable.
2) creates and terminates customers and barber.
Control program must simulate the number of chairs(n) free, and chairs full.
It must also ask for number(n) of customers. This is what I have working so far...

#include "myheader.h"
#define CHAIRS 5 // slots in the buffer.

pthread_mutex_t the_mutex;
pthread_cond_t cond_baber, cond_customer;
int customers_waiting = 0;

void *Customer(void *)
{
int i;

for(i < 0; i < MAX; i++)
pthread_mutex_lock(&the_mutex); //lock the mutex
if(customers < MAX)
pthread_cond_wait(&cond_customers,&the_mutex);
customers++;
pthread_cond_signal(&cond_barber); // wake up barber
pthread_mutex_unlock(&the_mutex); // unlock mutex
}
pthread_exit(0);
}

void *Barber(void *)
{
int i;

for(i < 0; i < MAX; i++)
pthread_mutex_lock(&the_mutex);
while(customers == 0)
pthread_cond_wait(&cond_barber,&the_mutex);
customers--;
pthread_cond_signal(&cond_customer);
pthread_mutex_unlock(&the_mutex);
}
pthread_exit(0);
}

/* My problem seems to be in control program */

int main(int argc, int **argv)
{
int int_var = 0;
pthread_t bar,cust;


/**** Problem area: trying to get main to prompt for number ***/
/**** of customers, then output number of chairs empty of full ***/

puts("Please enter number of customers:
");
scanf("%d", &int_var);


pthread_mutex_init(&the_mutex,0);

pthread_cond_init(&cond_customer,0);
pthread_cond_init(&cond_barber,0);

pthread_create(&bar,0,Barber,0):
pthread_create(&cust,0,Customer,0);

pthread_join(bar,0);
pthread_join(cust,0);

pthread_cond_destroy(&cond_customer)
pthread_cond_destroy(&cond_barber);
pthread_mutex_destroy(&the_mutex);
}

The problem seems to be prompting user for number of customers.
I also need to figure out how to determine number of full and
empty chairs.
Any help would be deeply appreciated. Thanks







Comments

  • : Hello all. I am designing the sleeping barber problem. I need to
    : implement the solution using synchronization primitives
    : (pthread_mutex_t for mutal exclusion).
    : Implementation must consist of a control program that:
    : 1) initializes the synchronization variable.
    : 2) creates and terminates customers and barber.
    : Control program must simulate the number of chairs(n) free, and
    : chairs full.
    : It must also ask for number(n) of customers. This is what I have
    : working so far...
    :
    : #include "myheader.h"
    : #define CHAIRS 5 // slots in the buffer.
    :
    : pthread_mutex_t the_mutex;
    : pthread_cond_t cond_baber, cond_customer;
    : int customers_waiting = 0;
    :
    : void *Customer(void *)
    : {
    : int i;
    :
    : for(i < 0; i < MAX; i++)
    : pthread_mutex_lock(&the_mutex); //lock the mutex
    : if(customers < MAX)
    : pthread_cond_wait(&cond_customers,&the_mutex);
    : customers++;
    : pthread_cond_signal(&cond_barber); // wake up barber
    : pthread_mutex_unlock(&the_mutex); // unlock mutex
    : }
    : pthread_exit(0);
    : }
    :
    : void *Barber(void *)
    : {
    : int i;
    :
    : for(i < 0; i < MAX; i++)
    : pthread_mutex_lock(&the_mutex);
    : while(customers == 0)
    : pthread_cond_wait(&cond_barber,&the_mutex);
    : customers--;
    : pthread_cond_signal(&cond_customer);
    : pthread_mutex_unlock(&the_mutex);
    : }
    : pthread_exit(0);
    : }
    :
    : /* My problem seems to be in control program */
    :
    : int main(int argc, int **argv)
    : {
    : int int_var = 0;
    : pthread_t bar,cust;
    :
    :
    : /**** Problem area: trying to get main to prompt for number ***/
    : /**** of customers, then output number of chairs empty of full ***/
    :
    : puts("Please enter number of customers:
    ");
    : scanf("%d", &int_var);
    :
    :
    : pthread_mutex_init(&the_mutex,0);
    :
    : pthread_cond_init(&cond_customer,0);
    : pthread_cond_init(&cond_barber,0);
    :
    : pthread_create(&bar,0,Barber,0):
    : pthread_create(&cust,0,Customer,0);
    :
    : pthread_join(bar,0);
    : pthread_join(cust,0);
    :
    : pthread_cond_destroy(&cond_customer)
    : pthread_cond_destroy(&cond_barber);
    : pthread_mutex_destroy(&the_mutex);
    : }
    :
    : The problem seems to be prompting user for number of customers.
    : I also need to figure out how to determine number of full and
    : empty chairs.
    : Any help would be deeply appreciated. Thanks
    :
    : Thanks anyway... but I figured it out already.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion