The word atomically means that the two actions performed by TAS(x) (testing, i.e., returning the old value of xand setting, i.e., assigning true to x) are inseparable. Specifically it is not possible for two concurrent TAS(x) operations to both return false (unless thereis also another concurrent statement that sets x to false).
With TAS available implementing a critical section for any number of processes is trivial.
loop forever {
while (TAS(s)) {} ENTRY
CS
s = false EXIT
NCS
}
Sleep and wakeup
Remark: Tanenbaum does both busy waiting (as above) and blocking (process switching) solutions. We will only dobusy waiting, which is easier. Sleep and Wakeup are the simplest blocking primitives. Sleep voluntarily blocks the process and wakeup unblocks a sleepingprocess. We will not cover these.
Question: Explain the difference between busy waiting and blocking process synchronization.
Semaphores
Remark: Tannenbaum use the term semaphore only for blocking solutions. I will use the term for our busy waitingsolutions. Others call our solutions spin locks.
P and V and Semaphores
The entry code is often called P and the exit code V. Thus the critical section problem is to write P and V so that
loop forever
P
critical-section
V
non-critical-section
satisfies
- Mutual exclusion.
- No speed assumptions.
- No blocking by processes in NCS.
- Forward progress (my weakened version of Tanenbaum's last condition).
Note that I use indenting carefully and hence do not need (and sometimes omit) the braces {} used in languages like C or java.
A binary semaphore abstracts the TAS solution we gave for the critical section problem.
- A binary semaphore S takes on two possible values “open” and “closed”.
- Two operations are supported
- P(S) is
- while (S=closed) {}
- S<--closed<== This is NOT the body of the while
where finding S=open and setting S<--closed is atomic
- That is, wait until the gate is open, then run through and atomically close the gate
- Said another way, it is not possible for two processes doing P(S) simultaneously to both see S=open (unless a V(S) is also simultaneous with bothof them).
- V(S) is simply S<--open
The above code is not real, i.e., it is not an implementation of P. It is, instead, a definition of the effect P is to have.
To repeat: for any number of processes, the critical section problem can be solved by
loop forever
P(S)
CS
V(S)
NCS
The only specific solution we have seen for an arbitrary number of processes is the one just above with P(S) implemented viatest and set.
Remark : Peterson's solution requires each process to know its processor number. The TAS soluton does not.Moreover the definition of P and V does not permit use of the processor number. Thus, strictly speaking Peterson did not provide an implementation of P and V.He did solve the critical section problem.
To solve other coordination problems we want to extend binary semaphores.
- With binary semaphores, two consecutive Vs do not permit two subsequent Ps to succeed (the gate cannot be doubly opened).
- We might want to limit the number of processes in the section to 3or 4, not always just 1.