Shared Memory Lock Variable (Address 0x1000)
LOCK =0
Lock Free
A
Process A
Software Variable
idle
process_a.c
void use_shared_resource() {
// ── 1. ACQUIRE LOCK ──
while (LOCK == 1) { /* spin */ }
LOCK = 1; // claim
// ── 2. CRITICAL SECTION ──
// (Safe to use resource here)
// ── 3. RELEASE LOCK ──
LOCK = 0;
}
Private Register
Ra
—
B
Process B
Software Variable
idle
process_b.c
void use_shared_resource() {
// ── 1. ACQUIRE LOCK ──
while (LOCK == 1) { /* spin */ }
LOCK = 1; // claim
// ── 2. CRITICAL SECTION ──
// (Safe to use resource here)
// ── 3. RELEASE LOCK ──
LOCK = 0;
}
Private Register
Rb
—
The Promised FixStep 1 of 13
After seeing the Printer Spooler disaster, a programmer proposes a fix: a shared LOCK variable. If LOCK = 0, the resource is free. If LOCK = 1, someone is using it. Simple, right?