Problem Description
Scene: 5 Philosophers, 5 forks, 5 Plates of pasta (spaghetti is slippery and requires two forks to pick up) Everyone revolves around the table, thinking and eating.
The Philosopher's mode of activity is: either put down the left and right hand knife and fork to think, or pick up a knife and fork to start eating (knife and fork to pick up, must take two, and can only be held in turn, first left hand to take on, after his right hand to take the right, left side to take the left). It has only these two alternating states.
The question for philosophers is how to arrange the logical logic of the philosophers ' actions to ensure that at least two people can get two forks to eat at the same time without a "deadlock", "Hunger", "wait" condition. It is important to note that everyone wants to eat the time is random, want to think about the timing is also random, this uncontrolled, can not be "you" to arrange the philosophers who eat first, which after eating, they are not under your control, but you have to give them a character, or the way of thinking, to ensure their independent thinking, self-solve problems.
Deadlock : We all want to eat at the same time, the results at the same time picked up the fork on the left side, found at the same time there is no fork, and then each bosom selfish, deadlocked hope someone can put down his left hand side fork, and then Rob, began to eat pasta, the results are not put ...
Hunger : Everyone at the same time want to eat, the results at the same time to pick up the fork on the left hand, found at the same time there is no fork, and then are very generous, the result we put down the left hand side fork, and then we found a fork, and at the same time began to pick up the fork in the left hand, but also put
Wait: Suppose the idea of taking a fork is an "atomic" operation, that is, not simultaneous, non-disruptive, and then once someone wants to take it, it evolves into Professor X, then uses the ability to control the other person in a state of rigidity, and then start to enjoy, and then put down the fork to think, immediately lose the ability , so the remaining four returned to normal, then 5 people again want to take a fork, evolved into a professor, the cycle. But in this way, a person to eat, the other four dry look at AH. How to say there are also 5 forks, 5 dishes of pasta, up to two people at the same time to eat. Workaround
1) Waiter Solution
Introducing a restaurant waiter, the philosopher must pass his permission to pick up the fork. Because the waiter knows which fork is being used, he can make a judgment to avoid deadlocks.
In order to demonstrate this solution, it is assumed that philosophers sequentially label A to E. If A and C are eating, there are four forks in use. b sits between a and C, so no two forks are available, and between D and E there is a free fork. Suppose at this point D wants to eat something. If he picks up the fifth fork, a deadlock may occur. On the contrary, if he asks for the waiter's approval, the waiter will let him wait. In this way, we can guarantee that the next time when two forks are free, there must be a philosopher who can successfully get a pair of forks, thus avoiding deadlocks.
2) Resource Grading solution
Another simple solution is to assign a partial or hierarchical relationship to a resource (this is a fork) and to agree that all resources are obtained in this order, released in reverse order, and that no two unrelated resources are required at the same time for the same job. In the question of dining philosophers, resources (fork) according to a certain rule number 1 to 5, each work unit (philosopher) always first pick up the left and right side numbered fork, and then get the higher number. After using the fork, he always first put down the higher-numbered fork, and then put down the lower number. In this case, when four philosophers pick up their lower-numbered forks at the same time, only the higher-numbered forks remain on the table, so that the fifth philosopher cannot use any fork. Moreover, only one philosopher can use a higher numbered fork, so he can dine with two forks. When he had finished, he would put down the higher-numbered fork and put down the lower-numbered fork, so that another philosopher would pick up the one behind and start eating.
Although resource ratings can avoid deadlocks, this strategy is not always practical, especially if the list of required resources is not known beforehand. For example, if a unit of work is holding resources 3 and 5, and you decide that resource 2 is required, you must first release 5, then Release 3 before you get 2, and then you must retrieve 3 and 5 sequentially. For computer programs that require access to a large number of database records, it is not practical to run efficiently if you need to release high-numbered records before accessing new records.
This method is often a practical solution to practical computer science problems, which can be solved by assigning constants to hierarchical locks and forcing the order of locks to be obtained.
3) Chandy/misra Solution
1984, K. Mani Chandy and J. Misra proposes another solution to the problem of dining philosophers, allowing arbitrary numbers of users (number P1, ..., Pn) to contend with any number of resources. Unlike Dicos's solution, the numbers here can be arbitrary.
1. For each of the philosophers competing for a resource, take a new fork and give the lower-numbered philosopher. Each fork is "clean" or "dirty". At first, all the forks were dirty.
2. When a philosopher wants to use a resource (that is, to eat), he must get it from the neighbor he competes with. He sends a request to every fork he doesn't currently have.
3. When the philosopher with the fork receives the request, if the fork is clean, he keeps it, or he wipes it and hand over the fork.
4. When a philosopher eats something, his fork gets dirty. If another philosopher had previously requested the fork, he would clean it and hand over the fork.
This solution allows for great parallelism and is suitable for any large problem.