CEG 7370: Distributed Computing Principles

Readers-Writers

This web page is organized in way that is useful during my lecture, instead of ppt slides.

Problem Modeling

reader[i:1..M]::
do true →
  ‹nr := nr+1›
  ... read ...  (*)
  ‹nr := nr-1›
od
writer[i:1..N]::
do true →
  ‹nw := nw+1›
  ... write ... (*)
  ‹nw := nw-1›
od

Required invariant at (*) RW:

(nr = 0 or nw = 0) and nw ≤ 1

Modify the atomic statements so that the above is guaranteed. Additionally, all the familiar requirements (progress, no turns, ...) must be satisfied.

Await-based Solution

[without priority for writers]
reader[i:1..M]::
do true →
  ‹await nw == 0 → nr := nr+1›
  ... read ...  (*)
  ‹nr := nr-1›
od
writer[i:1..N]::
do true →
  ‹await nr = 0 and nw = 0 → nw := nw+1›
  ... write ... (*)
  ‹nw := nw-1›
od

References

  1. Gregory R. Andrews, Concurrent Programming: Principles and Practice, Benjamin/Cummings, 1991. The book is now in its fifth printing. Chapter 4.

Copyright © 2012 Prabhaker Mateti