Eisenberg & McGuire algorithm
The Eisenberg & McGuire algorithm is an algorithm for solving the critical sections problem, a general version of the dining philosophers problem. It was described in 1972 by Murray A. Eisenberg and Michael R. McGuire.
Algorithm
All the n-processes share the following variables:
enum pstate = {IDLE, WAITING, ACTIVE};
pstate flags[n];
int turn;
The variable turn is set arbitrarily to a number between 0 and n−1 at the start of the algorithm.
The flags variable for each process is set to WAITING whenever it intends to enter the critical section. flags takes either IDLE or WAITING or ACTIVE.
Initially the flags variable for each process is initialized to IDLE.
1 repeat {
2
3 /* announce that we need the resource */
4 flags[i] := WAITING;
5
6 /* scan processes from the one with the turn up to ourselves. */
7 /* repeat if necessary until the scan finds all processes idle */
8 index := turn;
9 while (index != i) {
10 if (flags[index] != IDLE) index := turn;
11 else index := (index+1) mod n;
12 }
13
14 /* now tentatively claim the resource */
15 flags[i] := ACTIVE;
16
17 /* find the first active process besides ourselves, if any */
18 index := 0;
19 while ((index < n) && ((index = i) || (flags[index] != ACTIVE))) {
20 index := index+1;
21 }
22
23 /* if there were no other active processes, AND if we have the turn
24 or else whoever has it is idle, then proceed. Otherwise, repeat
25 the whole sequence. */
26 } until ((index >= n) && ((turn = i) || (flags[turn] = IDLE)));
27
28 /* Start of CRITICAL SECTION */
29
30 /* claim the turn and proceed */
31 turn := i;
32
33 /* Critical Section Code of the Process */
34
35 /* End of CRITICAL SECTION */
36
37 /* find a process which is not IDLE */
38 /* (if there are no others, we will find ourselves) */
39 index := (turn+1) mod n;
40 while (flags[index] = IDLE) {
41 index := (index+1) mod n;
42 }
43
44 /* give the turn to someone that needs it, or keep it */
45 turn := index;
46
47 /* we're finished now */
48 flags[i] := IDLE;
49
50 /* REMAINDER Section */
gollark: Simply attain 2928838383 friends.
gollark: Well, as they say, hyperbolic secant plus one.
gollark: GEORGE has existed for ages.
gollark: I see.
gollark: That's DD/MM/YYYY right?
See also
References
External links
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.