Xref: utzoo comp.lang.c:10533 comp.arch:5061 Path: utzoo!utgpu!water!watmath!clyde!att!pacbell!lll-tis!lll-winken!maddog!brooks From: brooks@maddog.llnl.gov (Eugene D. Brooks III) Newsgroups: comp.lang.c,comp.arch Subject: Re: volatile (in comp.lang.c) Message-ID: <8088@lll-winken.llnl.gov> Date: 2 Jun 88 06:03:14 GMT References: <20345@pyramid.pyramid.com> <833@mcdsun.UUCP> <1988May23.003847.1114@utzoo.uucp> <21821@amdcad.AMD.COM> <206@gannet.cl.cam.ac.uk> <8061@lll-winken.llnl.gov> Sender: usenet@lll-winken.llnl.gov Reply-To: brooks@maddog.UUCP (Eugene D. Brooks III) Organization: Lawrence Livermore National Laboratory Lines: 51 >> "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@maddog.llnl.gov, brooks@maddog.UUCP