进程理发师问题linux,进程同步的经典问题3——理发师问题

出处:<> by Andrew S. Tanenbaum

Another classical IPC problem takes place in a barber shop. The barber shop has one barber, one barber chair, and nchairs for waiting customers, if any, to sit on. If there are no customers present, the barber sits down in the barber chair and falls asleep, as illustrated in Fig. 2-35. When a customer arrives, he has to wake up the sleeping barber. If additional customers arrive while the barber is cutting a customer’s hair, they either sit down (if there are empty chairs) or leave the shop (if all chairs are full). The problem is to program the barber and the customers without getting into race conditions. This problem is similar to various queueing situations, such as a multiperson helpdesk with a computerized call waiting system for holding a limited number of incoming calls.

(未贴,可google)

Figure 2-35. The sleeping barber.

Our solution uses three semaphores: customers, which counts waiting customers (excluding the customer in the barber chair, who is not waiting), barbers, the number of barbers (0 or 1) who are idle, waiting for customers, and mutex, which is used for mutual exclusion. We also need a variable, waiting, which also counts the waiting customers. It is essentially a copy of customers. The reason for having waitingis that there is no way to read the current value of a semaphore. In this solution, a customer entering the shop has to count the number of waiting customers. If it is less than the number of chairs, he stays; otherwise, he leaves.

Our solution is shown in Fig. 2-36. When the barber shows up for work in the morning, he executes the procedure barber,causing him to block on the semaphore customersbecause it is initially 0. The barber then goes to sleep, as shown in Fig. 2-35. He stays asleep until the first customer shows up.

#define CHAIRS 5            /* # chairs for waiting customers */

typedef int semaphore;  /* use your imagination */

semaphore customers = 0;   /* # of customers waiting for service */

semaphore barbers = 0;        /* # of barbers waiting for customers */

semaphore mutex = 1;   /* for mutual exclusion */

int waiting = 0;               /* customers are waiting (not being cut) */

void barber(void)

{

white (TRUE) {

down(&customers);      /* go to sleep if # of customers is 0 */

down(&mutex);          /* acquire access to 'waiting' */

waiting = waiting − 1; /* decrement count of waiting customers */

up(&barbers);          /* one barber is now ready to cut hair */

up(&mutex);            /* release 'waiting' */

cut_hair();            /* cut hair (outside critical region) */

}

}

void customer(void)

{

down(&mutex);              /* enter critical region */

if (waiting < CHAIRS) {  /* if there are no free chairs, leave */

waiting = waiting + 1; /* increment count of waiting customers */

up(&customers);        /* wake up barber if necessary */

up(&mutex);            /* release access to 'waiting' */

down(&barbers);        /* go to sleep if # of free barbers is 0 */

get_haircut();         /* be seated and be serviced */

} else {

up(&mutex);            /* shop is full; do not wait */

}

}

Figure 2-36. A solution to the sleeping barber problem.

When a customer arrives, he executes customer, starting by acquiring mutexto enter a critical region. If another customer enters shortly thereafter, the second one will not be able to do anything until the first one has released mutex. The customer then checks to see if the number of waiting customers is less than the number of chairs. If not, he releases mutexand leaves without a haircut.

If there is an available chair, the customer increments the integer variable, waiting. Then he does an up on the semaphore customers, thus waking up the barber. At this point, the customer and barber are both awake. When the customer releases mutex, the barber grabs it, does some housekeeping, and begins the haircut.

When the haircut is over, the customer exits the procedure and leaves the shop. Unlike our earlier examples, there is no loop for the customer because each one gets only one haircut. The barber loops, however, to try to get the next customer. If one is present, another haircut is given. If not, the barber goes to sleep.

As an aside, it is worth pointing out that although the readers and writers and sleeping barber problems do not involve data transfer, they are still belong to the area of IPC because they involve synchronization between multiple processes.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值