Parnas' Solution to the Readers/Writers
m1, m2, m3, r, w : semaphore := 1
nreaders, nwriters : integer := 0
Reader Process {
P(m3);
P(r);
P(m1);
nreaders++;
if (nreaders == 1) then P(w) fi;
V(m1)
V(r);
V(m3);
... reading ...;
P(m1);
nreaders--;
if (nreaders == 0) then V(w) fi;
V(m1);
}
Writer Process {
P(m2);
nwriters++;
if (nwriters == 1) then P(r) fi;
V(m2);
P(w);
... writing ...;
V(w);
P(m2);
nwriters--;
if (nwriters == 0) then V(r) fi;
V(m2)
}
- Note that "… reading …" is not immediately preceded by a
P
operation. But note the P(w)
.
- Make sure you understand the need for
m3
- Does the above provide "priority" to writers?
1 References
- Courtois, Pierre-Jacques, Frans Heymans, and David Lorge
Parnas. "Concurrent control with “readers” and “writers”."
Communications of the ACM 14.10 (1971): 667-668.
- Courtois, Pierre-Jacques, F. Heymans, and David Lorge
Parnas. "Comments on “A comparison of two synchronizing concepts by
PB Hansen”." Acta Informatica 1.4 (1972): 375-376.