GATE / CS ARCHIVE

GATE CSE 2000 / QUESTION 20(b)

Demonstrate writer starvation

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

GATE 2000, Q20(b) Descriptive
Report issue

Synchronization & Concurrency

Question 20(b), 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. With correct boxes, can writers starve?
Question 20(b), 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. With correct boxes, can writers starve?
Question 20(b), 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. With correct boxes, can writers starve?
Tap to enlarge
Answer & solution

Answer: Yes. Waiting writers do not prevent new readers from entering. (no official key was published)

Consider two streams of readers whose reading intervals overlap. Before the last current reader exits, a new reader acquires mutex, observes W=0, increments R and begins reading. Arrange this repeatedly so R never becomes 0. A waiting writer may acquire mutex infinitely often, but each time it finds R>0, releases mutex and retries. It therefore never reaches W=1, even though every individual read finishes. A pending writer is not recorded anywhere: W means an active writer, not a waiting writer. Consequently new readers remain admissible throughout this schedule. This is starvation, not deadlock, since readers keep making progress. Even fairness in acquiring the bookkeeping semaphore would not repair this example because the writer gets turns and still cannot enter. Avoiding it requires a different admission policy, for example recording waiting writers and refusing new readers while a writer waits, together with suitable queue fairness; that change is outside the requested three boxes.

Original paper, page 10