Use your textbook, your notes, local on-line Unix documentation, your brain, or your psychic powers, but don't use other people, books, or Internet resources.
void enter_region( int process )
{
int other1, other2;
other1 = (process+1) % N;
other2 = (process+2) % N;
interested[process] = TRUE;
turn = process;
while( turn==process &&
(interested[other1]==TRUE || interested[other2]==TRUE) )
;
}
void leave_region( int process )
{
interested[process] = FALSE;
}
The following is Peterson's pseudo-code for his own n-process solution, from the 1981 paper cited in Tanenbaum. There are n processes numbered 1 through n. Q[] and TURN[] are shared variables, while j and k are local to each process.
/* Code for process i */ /* Enter region */ for j := 1 to n-1 do begin Q[i] := j; TURN[j] := i; wait until (for each k<>i, Q[k] < j) or TURN[j] <> i end; /* Critical section */ Critical section; /* Leave region */ Q[i] := 0;
For this question, you should use open() and read() rather than fopen() and the standard C I/O library functions.