理发师问题 Version 0 |
|
👤 Author: by x20004 2016-11-03 19:56:40 |
Define n=5;
Semaphore mutex=1;
Semaphore customers=0;
Semaphore barbers=1;
Int waiting=0; //等候理发的顾客数
Void baber()
{
While(true
{
Wait(customers));//若无顾客,理发师睡眠
Wait(mutex);
Waiting- -;
Signal(mutex);
Signal(barbar);
}
Void customer()
{
wait(mutex);
If(waiting
{
Waiting++;
Signal(mutex);
Signal(customers);//如果理发师睡觉,叫醒理发师
Wait(barber);//理发师在理发,顾客等候;
}
Else
Signal(mutex);//