# The synchronization problem of the classical process--The Philosophers Dine

Source: Internet
Author: User
Tags semaphore

Description of the Philosopher's meal

Proposed by Dijkstra and solved the problem of philosophers ' Dining (the dinning philosophers problem) is a classic synchronization problem. The problem is that there are five philosophers sharing a round table, sitting in the surrounding five chairs, with five bowls and five chopsticks on the table, and their lifestyle is alternating thinking and dining. At ordinary times, a philosopher to think, hungry will try to use his or her closest to his chopsticks, only he got two chopsticks to eat, eat and continue to think.

1 using recorded signals to solve the problem of philosophers ' dining

After analysis, the table chopsticks are critical resources, in a period of time only allow a philosopher to use, in order to achieve the exclusive use of chopsticks, you can use a semaphore to represent a chopstick, by the five semaphores constitute an array of semaphores.

The description is as follows:

`1Var chopstick:array[0,...,4] of Semapthore;2 all semaphores are initialized to 1, and the activities of the philosopher of the first place can be described as:3 Repeat4Wait (Chopstick[i]);//apply for a critical resource use right (get the chopsticks on the left)5Wait (chopstick[(i+1) MoD5]);//applying for a critical resource use right (get the chopsticks on the left)6     ...7Eat//meal operation after successful resource application8     ...9Signal (Chopstick[i]);//Release a critical resource (put back the chopsticks on hand)TenSignal (chopstick[(i+1) MoD5])//Release a critical resource (put back on the right hand side of the chopsticks) One     ... A Think -Untilfalse`

But this approach can cause deadlocks, for example: If five philosophers are hungry at the same time, while picking up the chopsticks on the left, five semaphores chopstick are 0, and when they try to pick up the chopsticks on the right, they will enter the infinite wait. There are several workarounds to this deadlock problem.

(1) at most, only four philosophers are allowed to take the left chopstick at the same time, ultimately guaranteeing that at least one philosopher can dine, and at the end of the run he can release the two chopsticks he used, so that more philosophers can dine.

(2) Only when the Philosopher's left and right two chopsticks are available, he is allowed to take chopsticks to dine.

(3) The philosopher of the odd number first takes his left chopsticks and then goes to the chopsticks on the right, while the even number philosopher is the opposite. According to this rule, the philosopher No. 1 and 2nd will compete for 1th chopsticks, and 3 and 4th philosophers compete for the No. 3rd chopsticks. That is, five philosophers compete for the odd number of chopsticks, after the acquisition, then to compete with even number of chopsticks, there will always be a philosopher can get two chopsticks and dine.

2. Solving the problem of philosophers ' dining by using and semaphore mechanism

In the question of dining philosophers, it is required that each philosopher obtain two critical resources (chopsticks) before eating, which is essentially the and synchronization problem described earlier, so the most concise solution can be obtained by using and semaphore mechanism.

Described below

`1Var chopstick Array of semapthore:= (1,1,1,1,1);2 Processi3 Repeat4 think;5Sswait (chopstick[(i+1) MoD5],chopstick[i]);6 Eat7Ssignal (chopstick[(i+1) MoD5],chopstick[i]);8Untilfalse;`

The synchronization problem of the classical process--The Philosophers Dine

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.