Operating Systems · Critical Section Solutions
Lock Variable Mechanism
Overview
The lock variable is the simplest software synchronization mechanism for the critical-section problem.
It uses a single shared variable as a "vacant/occupied" tag on the critical section, and it matters because it shows both the basic entry/exit structure of a solution and the busy-waiting cost — while also failing to guarantee mutual exclusion.
Key concepts
- Lock variable: a shared variable
lockacting as a tag on the critical section —lock = 0means the critical section is vacant (a process may enter),lock = 1means it is occupied (a process must wait).
Despite the name, it is not an actual lock; it behaves like a tag hung on a door knob showing "vacant" or "occupied". - Software mechanism: no hardware support is needed and it is implemented in user mode.
- User mode: no support or interference from the operating system is required; the synchronization code is written entirely by the programmer.
- Kernel mode: the solution needs help from operating-system code.
- Busy waiting: a process that cannot enter keeps "knocking" — it keeps checking the lock in a loop and consumes CPU time while doing no useful work.
- Process count: it works for more than two processes — in fact, for any number of processes — unlike several other solutions that work only for two processes.
How it works
The program has an entry section above the critical section, then the critical section, then an exit section after it. Initially, lock = 0.
- The entry section is a
whileloop with an empty body (a semicolon). As long aslockis 1, the process stays in an infinite loop. - Since only two values (0 and 1) are used,
while (lock != 0)can equivalently be written aswhile (lock == 1). - When
lockbecomes 0, the loop breaks. - The process sets
lock = 1so that others are stopped from entering, then enters the critical section. - After finishing, it sets
lock = 0to release the lock.
int lock = 0; // shared variable: 0 = vacant, 1 = occupied
while (lock != 0); // line 1: entry section — wait while occupied
lock = 1; // line 2: mark the critical section occupied
// ---- critical section ---- line 3
lock = 0; // line 4: exit section — mark vacant / release
Even though the name says "lock", it does not actually lock the critical section: the critical section remains open, and anyone can enter it.
Dressing-room analogy: there is one dressing room with a door knob but no real lock, and a tag is hung on the knob. Initially the tag says "vacant" (lock = 0). A person who wants to enter first checks the knob/tag:
- If the tag says vacant (
lock = 0), he removes it and replaces it with an occupied tag (lock = 1), then enters the room. - Anyone else who comes checks the knob, sees the occupied tag, and stops himself from entering — but he keeps knocking. This knocking is exactly the busy-waiting loop.
This is like a dressing room in a mall or a Zara showroom where there is only a curtain and no lock: anyone can enter while a person is inside.
Drawing the diagram…
Formulas
lock = 0 → critical section vacant, a process may enter
lock = 1 → critical section occupied, a process must wait
entry section: while (lock != 0); then lock = 1;
exit section: lock = 0;
lockis the shared variable; 0 and 1 are its only two values.
Worked examples
Processes P1 and P2 use the same four-line code. Initially lock = 0.
| Step | Process | Instruction executed | Value of lock | Effect |
|---|---|---|---|---|
| 1 | P1 | line 1: while (lock != 0); | 0 | 0 != 0 is false, so P1 comes out of the loop |
| 2 | P1 | line 2: lock = 1; | 1 | P1 marks the critical section occupied |
| 3 | P1 | line 3: enters critical section | 1 | P1 is inside the critical section |
| 4 | P1 | P1 is preempted; P2 gets the CPU | 1 | P2 starts executing |
| 5 | P2 | line 1: while (lock != 0); | 1 | 1 != 0 is true, so P2 stays in the infinite loop |
| 6 | P2 | line 1 again and again | 1 | CPU is executing P2, but P2 does no useful work; it only waits |
| 7 | P1 | finishes line 3, executes line 4: lock = 0; | 0 | P1 releases the lock and exits the critical section |
| 8 | P2 | line 1: while (lock != 0); | 0 | 0 != 0 is false, so P2 breaks out of the loop |
| 9 | P2 | line 2: lock = 1; | 1 | P2 marks the critical section occupied |
| 10 | P2 | line 3: enters critical section | 1 | P2 is now inside the critical section |
While P2 is stuck at line 1, the CPU is busy trying to execute P2, but P2 has nothing useful to do — it simply keeps waiting for P1 to come out.
This is busy waiting: P2 is given the CPU to execute, but it gets nothing done; it stays in the loop, "busy" doing waiting for the other process to come out of the critical section.
Comparison
| Point | User mode | Kernel mode |
|---|---|---|
| Meaning | No operating-system support is needed; code is written by the programmer | Help from operating-system code is needed |
| Interference from OS | No OS interference | Requires entering OS code |
| Point | Lock variable solution |
|---|---|
| Number of processes supported | More than two — any number of processes |
| Waiting type | Busy waiting (process keeps checking in a loop) |
| Implementation | Software, user mode, no hardware support |
| Actual locking | Not a real lock; only a tag-like indicator |
Revision summary
lock = 0: critical section vacant — a process may enter.lock = 1: critical section occupied — a process must wait.- Entry section:
while (lock != 0);followed bylock = 1;. Exit section:lock = 0;. - It is a software, user-mode mechanism — no hardware or OS support is required.
- It is a busy-waiting solution: the waiting process remains in an empty
whileloop and consumes CPU while waiting. - It works for any number of processes, not only two.
- The name is misleading: it is not an actual lock.
The critical section is still open, and anyone can enter it. - Trap: because the check of
lockand the settinglock = 1are two separate operations, a process can be preempted between them; another process can then also seelock = 0, and both may enter the critical section, so mutual exclusion is not guaranteed. - Architectural neutrality (the solution should run on all platforms) is a secondary requirement.
Previous-year exam insights
- A software lock-variable / flag solution fails because the test of the flag and the set of the flag are not atomic: a preemption between them lets two processes both observe the flag free and both enter the critical section.
Asked inGATE 2007 - Busy-waiting lock solutions built on atomic instructions (Test-and-Set, Swap, Fetch-and-Add) do guarantee mutual exclusion, but they are not starvation-free: an unlucky process may keep losing the race and wait indefinitely.
Asked inGATE 2009GATE 2012 - Test-and-Set (and Swap) are hardware instructions that provide mutual exclusion; semaphores are software/OS-level constructs, so 'hardware implementation providing mutual exclusion' refers to Test-and-Set.
Asked inISRO 2020 - A lock counter built with Fetch-and-Add can overflow and wrap back to 0 (or to a value indicating 'available') while the lock is actually held, so such a lock is not correct unless overflow is prevented.
Asked inGATE 2012 - Any correct critical-section solution must satisfy three requirements: mutual exclusion, progress (no deadlock — a process outside the CS must not block others), and bounded waiting (no starvation).
Asked inGATE 2009GATE 2012 - Busy waiting wastes CPU cycles but is acceptable on multiprocessors; on a uniprocessor a busy-waiting process can be preempted, so spinning is wasteful and blocking (sleep/wakeup) locks are preferred.