CS 3733 Operating Systems
Read Sections 8.2 of PUP and 6.4 of SG
P
which can only be accessed through
two atomic operations: wait (P) and
signal (V).
(Note: Tanenbaum calls these DOWN and UP.)
wait(int *SP): signal(int *SP):
while (*SP <= 0) ; *SP = *SP + 1;
*SP = *SP - 1;
Remember: these are atomic.
int S;
while (1) {
wait(&S);
critical section
signal(&S);
}
S1 before process 2 executes S_2.
Process 1 executes:
S1; signal(&sync);Process 2 executes:
wait(&sync); S2;How should
sync be initialized?
P_1 code:
wait(&S);
...
signal(&Q);
P_2 code:
wait(&Q);
...
signal(&S);
S and Q are initialized
to 0.
Skill: Understand semaphores.