GATE / CS ARCHIVE

GATE CSE 2000 / QUESTION 20(a)

Complete readers-writers entry protocol

Operating Systems · Synchronization & Concurrency · Full GATE CSE 2000 paper

GATE 2000, Q20(a) Descriptive
Report issue

Synchronization & Concurrency

Question 20(a), GATE 2000: R=W=0, binary semaphore mutex=1. Reader acquires mutex, and if W==0 increments R then executes box 1; otherwise executes box 2 and retries. After reading it acquires mutex, decrements R and releases mutex. Writer acquires mutex; if box 3 is true it releases mutex and retries; otherwise it sets W=1, releases mutex, writes, then acquires mutex, sets W=0 and releases mutex. Fill boxes 1,2,3.
Question 20(a), GATE 2000: R=W=0, binary semaphore mutex=1. Reader acquires mutex, and if W==0 increments R then executes box 1; otherwise executes box 2 and retries. After reading it acquires mutex, decrements R and releases mutex. Writer acquires mutex; if box 3 is true it releases mutex and retries; otherwise it sets W=1, releases mutex, writes, then acquires mutex, sets W=0 and releases mutex. Fill boxes 1,2,3.
Tap to enlarge
Answer & solution

Answer: (1) signal(mutex); (2) signal(mutex); (3) R>0 || W!=0. (no official key was published)

The complete intended protocol is:
Reader() {
L1: wait(mutex);
  if (W == 0) {
    R = R + 1;
    signal(mutex);                 // box 1
  } else {
    signal(mutex);                 // box 2
    goto L1;
  }
  /* read shared data */
  wait(mutex);
  R = R - 1;
  signal(mutex);
}
Writer() {
L2: wait(mutex);
  if (R > 0 || W != 0) {           // box 3
    signal(mutex);
    goto L2;
  }
  W = 1;
  signal(mutex);
  /* write shared data */
  wait(mutex);
  W = 0;
  signal(mutex);
}

wait/signal must implement a genuine atomic binary semaphore with appropriate memory visibility. Merely reading and writing an ordinary flag is not a substitute. All tests and updates of R,W occur under mutex, so checking admission and registering the admitted reader/writer are atomic relative to competitors. A writer enters only when no reader and no writer is active, and W=1 prevents later admissions until it finishes. Multiple readers are allowed while W=0. Both reader branches must release mutex; retaining it would block exits or retries and can deadlock. The mutex protects admission bookkeeping, not the whole read/write operation; R and W reserve access while mutex is released. Safety holds, but fairness is not guaranteed.

Original paper, page 10