#include "thr_def.h"
#define CHAIRS 3
sema_t mutex = 1;
sema_t barber = 0;
sema_t customers = 0;
thread_t id0,id1,id2;
int waiting = 0;
void service(void)
{
puts("The barber is cutting custommer`s hair.");
thr_delay(100);
)
void get_service(thread_t id)
{
thr_delay (120);
printf("Customer%d got his hair cut.n",id);
}
void Barber(void)
{
while(1)
{
sema_pc (&customers);
sema_pb(&mutex);
waiting --;
sema_vb(&barber);
sema_vb(&mutex);
service();
thr_yield();
}
}
void Customer1(void)
{
push("Customer 1 enters the shop.");
sema_pb (&mutex);
if (waiting
{
waiting ++;
puts ("Customer 1 sits in the waiting room.");
sema_vc(&customers);
sema_vb(&mutex);
sema_pb(&barber);
get_service(id1);
}
else
{
printf("Customer %d leaves the shop.n",id1);
sema_vb(&mutex);
}
thr_delete(id1);
}
void Customer2(void)
{
puts ("Customer 2 enters the shop .");
sema_pb(&mutex);
if(waiting
{
waiting++;
puts("Customer2 sits in the waiting room.");
sema_vc(&customers);
sema_vb (&mutex);
sema_pb (&barber);
get_service (id2);
}
else
{
sema_vb(&mutex);
printf ("Customer %d leaves the shop .n",id2);
}
thr_delete(id2);
}
int main(void)
{
size_t STK = 3000;
parbegin();
thr_create(Barber , STK,&id0);
thr_create(Customer1, STK,&d1);
thr_create(Customer2, STK,&id2);
parend();
_exit(0);
}