Reading: PUP Section 8.2)
PROCESS 1: PROCESS 2: while(TRUE) { while(TRUE) { while (turn != 0) ; while (turn != 1) ; ...critical section... ...critical section... turn = 1; turn = 0; ...non-critical section... ...non-critical section... } }
turn
can be either 0 or 1 initially.
The shared integer array flag[2]
has both entries initialized to 0.
int turn; int flag[2]; Process 0: Process 1: flag[0]=1; flag[1]=1; turn = 1; turn = 0; while (flag[1]==1 & (turn=1)); while (flag[0]==1 & (turn=0)); ...critical section... ...critical section... flag[0] = 0; flag[1] = 0;
P
which can only be accessed through
two atomic operations: wait (P)
and
signal (V)
.
(Note: Tanenbaum calls these DOWN
and UP
.)
wait(S): signal(S): while (S <= 0) ; S = S + 1; S = S - 1;
wait(mutex) critical section signal(mutex) remainder section