一个理发店有一张可以坐N人的沙发和n个理发椅和n个理发师和一个收银员。没有顾客时,理发师睡觉,当一个顾客走进理发店时,如果沙发被坐满,则离开,否则,如果所有理发师正在为其他顾客理发,便在沙发上坐下等待。如果理发师正在睡觉,顾客需将他唤醒。理发完后顾客需付费直到收银员收费才能离开;
用类C语言编写程序
int count = 0; //the number of customers sit on sofaint chair = n;bool mutex = 1; // control the countbool mutex2 = 1; //control the cashier only deal with a customerint bnum = n; // the number of resting barberint cnum = 0; // the number of the customer sit in chairs int payment = 0;int receipt = 0; void barber() { wait(cnum); haircut for the customer; signal(bnum);}void customer() { wait(mutex); if(count >= N) { // the sofa is full signal(mutex); leave; } else { count++; signal(mutex); wait(chair); // wait for the empty chairs count--; // leave the sofa signal(cnum); wait(bnum); //wait for the barber haircut; signal(chair); wait(mutex2); //wait for the cashier signal(payment); pay; wait(receipt); leave; }}void cashier() { wait(payment); receive payment; signal(receipt); signal(mutex2);}