NivaarExam PrepOfficial exam papers ↗

25-Comp-A5 Operating Systems · May 2018

Question 1 of 7: Critical Section, Monitors, Real-Time Systems

Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)

Notes on this paper

17-COMP A-5 Operating Systems — National Examinations, May 2018. 3 hours, closed book (one approved pocket calculator only). Candidates were instructed to answer any five of the seven questions; all seven are answered below as a complete study resource.

Reference texts: Silberschatz, Galvin & Gagne, Operating System Concepts (10th ed.) — process synchronization/monitors (ch. 6–7), CPU scheduling (ch. 5), deadlocks (ch. 8), memory management/paging (ch. 9–10), mass-storage/file-system implementation and disk scheduling (ch. 11–12), protection (ch. 14); Tanenbaum, Modern Operating Systems (5th ed.), corroborating chapters on synchronization, scheduling, memory and file systems.

Question 1: Critical Section, Monitors, Real-Time Systems (20 marks)

Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.

Given. Two processes P0, P1 share boolean flags guard0, guard1 (both initially false). Each process raises its own flag, checks the other's flag, and — only if it finds the other's flag raised — retreats (lowers its own flag, busy-waits for the other's flag to clear, then re-raises its own) before proceeding into the critical section (CS).

Find. Every distinct design flaw, each with a concrete triggering interleaving.

Approach. Model each process as a small state machine of atomic actions (set/check guard0/guard1, enter/exit CS) and exhaustively search every possible interleaving — a model-checking approach — for (i) reachable states in which both processes are inside the CS simultaneously, and (ii) the design smell the question itself calls out (harm even without incorrect results).

  1. Flaw 1 — mutual exclusion CAN be violated. The retreat protocol commits to entering the CS as soon as it has once observed the other flag clear, without re-checking at the instant of actual entry. An exhaustive search over all interleavings finds a reachable state where both processes are simultaneously inside the CS. The table below is the shortest such interleaving:
    Interleaving that puts both processes in the CS at once
    #Eventguard0guard1
    0Initial stateFF
    1P0 sets guard0 = trueTF
    2P1 sets guard1 = trueTT
    3P0 checks guard1 (true) → takes the retreat branchTT
    4P1 checks guard0 (true) → takes the retreat branchTT
    5P0 retreats: guard0 = falseFT
    6P1 retreats: guard1 = falseFF
    7P0's busy-wait sees guard1 = false → exits waitFF
    8P1's busy-wait sees guard0 = false (P0 hasn't re-raised it yet) → exits waitFF
    9P0 re-raises guard0 = true and enters CS unconditionallyTF
    10P1 re-raises guard1 = true and enters CS unconditionallyTT
    Both processes retreated in lockstep (steps 3–6), so both flags read false at the exact moment each busy-wait checks the other (steps 7–8); each process then re-raises its own flag and walks straight into the CS with no further check — landing both inside at step 10. $$\boxed{\text{Mutual exclusion is violated: this interleaving puts P0 and P1 in the CS simultaneously}}$$
  2. Flaw 2 — busy-waiting (a design flaw, not a correctness bug, per the question's own wording). This occurs at two places — P0's while (guard1) ; and P1's while (guard0) ; — but is the same underlying design defect in both, so it is explained once: a process that finds the CS occupied spins on the CPU consuming cycles instead of blocking and yielding the processor to other ready work. On a uniprocessor this can waste an entire time slice with no useful progress, and under priority scheduling a low-priority waiter spinning while a high-priority holder is delayed elsewhere is the classic setup for priority inversion.
  3. Flaw 3 — no bounded-waiting guarantee. The protocol has no shared tie-breaking variable (compare with Peterson's solution, which adds exactly one turn variable for this reason). Nothing in the design bounds how many times, in a row, one process's retreat/re-raise can happen to win the race over the other's whenever both attempt entry at nearly the same time; a scheduler could repeatedly resolve the race the same way, so bounded waiting is not established by this algorithm even on the runs where mutual exclusion happens to hold.
Final Results – Question 1(a)
FlawClassEvidence
Mutual exclusion violatedCorrectness10-step interleaving, both processes reach CS simultaneously (model-checked)
Busy-waiting (2 occurrences: P0 and P1)Design flaw, not incorrect resultswhile (guardX) ; in each process
No bounded-waiting guaranteeDesign flaw / fairnessNo turn/tie-break variable, unlike Peterson's solution

(b) The three critical-section requirements under a monitor. A monitor is a language-level construct that bundles shared data with the only procedures allowed to touch it, and the runtime guarantees that at most one thread is active inside the monitor at any time.

(c) Real-time systems. A real-time system is one whose correctness depends not only on the logical result of a computation but on the time by which that result is produced — a logically correct answer delivered after its deadline is treated as a failure. Hard real-time systems have deadlines that must never be missed: a missed deadline is a system failure with potentially catastrophic consequences (e.g. an anti-lock braking controller that must compute a new brake-pressure command within a few milliseconds of a wheel-speed sample, or a pacemaker's pacing-pulse timer). Soft real-time systems have deadlines whose occasional miss degrades quality of service but is not catastrophic: a missed deadline just makes the result less useful, not useless (e.g. a video-conferencing frame decoder that occasionally drops or delays a frame under load, or an online game's physics tick running a little late under heavy load) — the system still functions, just with visibly degraded, statistically bounded quality.

← Paper overview