Solution to the critical-section problem using locks.
- Consider having a single, shared (lock) variable, initially 0.
- When a process wants to enter its CS, it first tests the lock.
- If the lock is 0, the process sets it to 1 and enters the CS.
- If the lock is already 1, the process just waits until it becomes 0.
- Thus, a 0 means that no process is in its CS, and a 1 means that some process is in its CS.
- Unfortunately, this idea contains a fatal flaw;
- Suppose that one process reads the lock and sees that it is 0.
- Before it can set the lock to 1, another process is scheduled, runs, and sets the lock to 1.
- When the first process runs again, it will also set the lock to 1, and two processes will be in their CSs at the same time.