Most contemporary computer architectures offer hardware support for process synchronization in the form of specialized machine instructions.
The test-and-set instruction (TS) copies a variable into a register and sets the variable to zero in one indivisible machine cycle. Test-and-set has the form TS(R, x) where R is a register and x is a memory location and performs the following operations:
- Copy x into R
- Set x to 0
A lock is a synchronization barrier through which only one process can pass at a time. TS allows an easy implementation of a lock.
A binary semaphore can take only the values 0 or 1. Pb and Vb are the simplified P and V operations that manipulate binary semaphores. These are designed for simpler problems where you don’t need a full semaphore, to make it faster/easier to work with.
Pb and Vb on the binary semaphore sb can be implemented directly using the TS instruction:
- Vb(sb):
sb = 1
- Pb(sb):
do TS(R,sb) while R == 0
Busy-waiting is the act of repeatedly executing a loop while waiting for some condition to change. Busy-waiting consumes CPU resources and should be avoided whenever possible. The implementation of Pb(sb) using TS is very simple but suffers from the drawback of busy-waiting.
The Implementation of Semaphores was originally found on Access 2 Learn