1. Gyanastra
  2. Operating Systems
  3. Critical Section Solutions
  4. Lock Variable Mechanism
Free lesson, no account needed 8 min read 11 practice questions after it
Asked in past papers4 times
  • GATE CSE201220092007
  • ISRO2020

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 lock acting as a tag on the critical section — lock = 0 means the critical section is vacant (a process may enter), lock = 1 means 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.

  1. The entry section is a while loop with an empty body (a semicolon). As long as lock is 1, the process stays in an infinite loop.
  2. Since only two values (0 and 1) are used, while (lock != 0) can equivalently be written as while (lock == 1).
  3. When lock becomes 0, the loop breaks.
  4. The process sets lock = 1 so that others are stopped from entering, then enters the critical section.
  5. After finishing, it sets lock = 0 to 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.

Figure. The lock variable. A process spins while lock is 1 (busy waiting), sets lock to 1, runs its critical section, and sets lock back to 0. Because testing the lock and setting it are two separate steps, two processes can both read 0 before either writes 1, so mutual exclusion is not guaranteed.

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;
  • lock is 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.

StepProcessInstruction executedValue of lockEffect
1P1line 1: while (lock != 0);00 != 0 is false, so P1 comes out of the loop
2P1line 2: lock = 1;1P1 marks the critical section occupied
3P1line 3: enters critical section1P1 is inside the critical section
4P1P1 is preempted; P2 gets the CPU1P2 starts executing
5P2line 1: while (lock != 0);11 != 0 is true, so P2 stays in the infinite loop
6P2line 1 again and again1CPU is executing P2, but P2 does no useful work; it only waits
7P1finishes line 3, executes line 4: lock = 0;0P1 releases the lock and exits the critical section
8P2line 1: while (lock != 0);00 != 0 is false, so P2 breaks out of the loop
9P2line 2: lock = 1;1P2 marks the critical section occupied
10P2line 3: enters critical section1P2 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

PointUser modeKernel mode
MeaningNo operating-system support is needed; code is written by the programmerHelp from operating-system code is needed
Interference from OSNo OS interferenceRequires entering OS code
PointLock variable solution
Number of processes supportedMore than two — any number of processes
Waiting typeBusy waiting (process keeps checking in a loop)
ImplementationSoftware, user mode, no hardware support
Actual lockingNot 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 by lock = 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 while loop 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 lock and the setting lock = 1 are two separate operations, a process can be preempted between them; another process can then also see lock = 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.
Pages 1–2 of 2