volatile (in comp.lang.c)
Eugene D. Brooks III
brooks at maddog.llnl.gov
Thu Jun 2 16:03:14 AEST 1988
>> "A Fast Mutual Exclusion Algortithm"
>> Leslie Lamport, Nov 14 1985.
>> Report #5, DEC Systems Research Centre
>>
Having been pestered by several people to post the code for Lamport's
algorithm to the net so people don't have wait for his paper, which you
really ought to read if you want to discuss this item, here is an expression
of the algorithm in C fragments. The "shared" is meant to indicate the
status of the storage of the memory cells. I hopefully have not blown it,
please don't start up your flame machines if I did. READ LESLIE'S PAPER!
#define TRUE 1
#define FALSE 0 /* Zero so b[] starts out FALSE. */
#define PROCUNDEF -1
#define MAXPROC 8 /* How many processors you have. */
shared int x = PROCUNDEF;
shared int y = PROCUNDEF;
shared int b[MAXPROC];
/* Code fragment to get locked access to critical region.
PROCNUM is a unique identifier for the cpu executing the code.
*/
tryagain :
b[PROCNUM] = TRUE;
x = PROCNUM;
if(y != PROCUNDEF) {
b[PROCNUM] = FALSE;
while(y != PROCUNDEF);
goto tryagain;
}
y = PROCNUM;
if(x != PROCNUM) {
b[PROCNUM] = FALSE;
for(j = 0; j < MAXPROC; j += 1) {
while(b[j] != FALSE);
}
if(y != PROCNUM) {
while(y != PROCUNDEF);
goto tryagain;
}
}
/* Code fragment to unlock critical region.
*/
y = PROCUNDEF;
b[PROCNUM] = FALSE;
brooks at maddog.llnl.gov, brooks at maddog.UUCP
More information about the Comp.lang.c
mailing list