Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
123 views2 pages

MCP S10 Read04 ReadersWriters

Two classes of processes known as "readers" and "writers" may share a "critical section" the "readers'" may share the section with each other, but the "writers'" must have exclusive access. The problem has been solved using the "p" and "v" operations of dijkstra.

Uploaded by

Firzhan Naqash
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views2 pages

MCP S10 Read04 ReadersWriters

Two classes of processes known as "readers" and "writers" may share a "critical section" the "readers'" may share the section with each other, but the "writers'" must have exclusive access. The problem has been solved using the "p" and "v" operations of dijkstra.

Uploaded by

Firzhan Naqash
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Operating B.

Randell
Systems Editor

Concurrent Control
with "Readers" and
"Writers"
P.J. Courtois,* F. Heymans, and
D.L. Parnas*
MBLE Research Laboratory
Brussels, Belgium

The problem of the mutual exclusion of several Dijkstra [1], Knuth [2], and de Bruijn [3] have dis-
independent processes from simultaneous access to a cussed the problem of guaranteeing exclusive access to
"critical section" is discussed for the case where there a shared resource in a system of cooperating sequential
are two distinct classes of processes known as "readers" processes. The problem they deal with has been shown
and "writers." The "readers" may share the section to have a relatively simple solution using the " P " and
with each other, but the "writers" must have exclusive " V " operations of Dijkstra [4]. We discuss two related
access. Two solutions are presented: one for the case problems of practical significance in which we recognize
where we wish minimum delay for the readers; the other two classes of processes wishing to use the resource.
for the case where we wish writing to take place as early The processes of the first class, named writers, must
as possible. have exclusive access as in the original problem, but
Key Words and Phrases: mutual exclusion, critical processes of the second class, the readers, may share
section, shared access to resources the resource with an unlimited number of other readers.
CR Categories: 4.30, 4.32

Problem 1

We demand of our solution that no reader be kept


waiting unless a writer has already obtained permission
to use the resource; i.e. no reader should wait simply
because a writer is waiting for other readers to finish.
In this case the solution presented is quite simple, but
our experience has shown that it is not easily arrived at.
Numerous solutions, which have quite unreasonable
complexity, have been proposed. The following solution
resulted from several cycles a m o n g the authors in which
each simplified a solution shown him by another. We
present it in hope that others may be spared the effort
of solving again this rather c o m m o n problem. See Fig-
*Present address: Department of Computer Science, Car- ure 1.
negie-Mellon University, Pittsburgh, Pa 15213 Please notice that w functions as a mutual exclusion
Copyright © 1971, Association for Computing Machinery, Inc. semaphore for the writer but is only used by the first
General permission to republish, but not for profit, all or part
of this material is granted, provided that reference is made to this reader to enter the critical section and the last reader
publication, to its date of issue, and to the fact that reprinting to leave it. It is ignored by readers who enter or leave
privileges were granted by permission of the Association for while other readers are present, mutex ensures that only
Computing Machinery.
one reader will enter or leave at a time thereby elimi-
nating the possibility of ambiguity about which process

667 Communications October 1971


of Volume 14
the ACM Number 10
Fig. 1 Fig. 2

integer readcount ; (initial value = 0) integer readcount, writecount ; (initial value = 0)


semaphore mutex, w ; (initial value for both = 1) semaphore mutex 1, mutex 2, mutex 3, w, r ; (initial value = 1)

READER WRITER READER WRITER


P(mutex) ; P(mutex 3) ; P(mutex 2) ;
readcount := readeount + 1 ; P(r) ; writecount := writeeount + 1 ;
if readeount = 1 then P(w) ; P(mutex 1) ; if writeeount = 1 then P(r) ;
V(mutex) ; readeount = readeount + 1 ; V(mutex 2) ;
P(w); if readeount = 1 then P(w) ; P(w) ;
V(mutex 1) ;
reading is performed writing is performed V(r) ;
V(mutex 3) ;
P(mutex) ; V(w) ;
readeount := readeount -- 1 ; reading is done writing is performed
if readcount = 0 then V(w) ;
V(mutex) ; P(mutex 1) ; V(w) ;
readeount := readcount - 1 ; P(mutex 2) ;
if readcount = 0 then V(w) ; writecount := writeeount -- 1 ;
V(mutex 1) ; if writeeount = 0 then V(r) ;
V(mutex 2) ;

is r e s p o n s i b l e for a d j u s t i n g w. w will be positive if a n d t a n e o u s l y w a i t i n g for a V ( r ) to be d o n e b y a reader.


o n l y if t h e r e are no r e a d e r s a n d no writers present in In t h a t event we c o u l d n o t g u a r a n t e e p r i o r i t y to the
the critical section. writer, m u t e x 3 g u a r a n t e e s a r e a d e r exclusive access to
the b l o c k o f c o d e f r o m " P ( r ) " to " V ( r ) " inclusive. A s
a result t h e r e will be at m o s t o n e process ever waiting
Problem 2 at r, a n d t h e result o f a V is clear.

H e r e we r e t a i n the r e q u i r e m e n t t h a t writers m u s t
have exclusive access while r e a d e r s m a y share, b u t we Final Remarks
a d d the r e q u i r e m e n t t h a t once a writer is r e a d y to write,
he p e r f o r m s his " w r i t e " as s o o n as possible. A s o l u t i o n T h e r e a d e r will n o t e t h a t the a b o v e s o l u t i o n s d o n o t
to this p r o b l e m c a n n o t be a s o l u t i o n to P r o b l e m 1 be- g u a r a n t e e a FIFO discipline for the writers. T o p r o v i d e
cause to meet this r e q u i r e m e n t a r e a d e r who arrives such a g u a r a n t e e we m u s t either a s s u m e f u r t h e r p r o p e r -
after a writer has a n n o u n c e d t h a t he is r e a d y to write ties o f the V o p e r a t i o n o r m a k e use o f a n a r r a y o f n
m u s t wait even i f the w r i t e r is also w a i t i n g . F o r the first s e m a p h o r e s where n is t h e n u m b e r o f writers.
p r o b l e m it was possible t h a t a writer c o u l d wait in-
definitely while a s t r e a m o f r e a d e r s arrived. I n this
p r o b l e m we give p r i o r i t y to writers a n d allow r e a d e r s Acknowledgment. W e are grateful to A . N . H a b e r -
to wait indefinitely while a s t r e a m o f writers is w o r k i n g . m a n n o f C a r n e g i e - M e l l o n U n i v e r s i t y for h a v i n g s h o w n
O n general principles we r e q u i r e t h a t the s o l u t i o n give us an e r r o r in an earlier version o f this r e p o r t .
p r i o r i t y to writers w i t h o u t m a k i n g a n y a s s u m p t i o n s
a b o u t p r i o r i t y being built into the V routine. I n o t h e r
words, where several processes are waiting at a sema- References
phore, we c a n n o t predict which one will be a l l o w e d to
1. Dijkstra, E.W. Solution of a problem in concurrent
p r o c e e d as the result o f a V o p e r a t i o n . programming control. Comm. A C M 8, 9 (Sept. 1965), 569.
W e p r o p o s e the s o l u t i o n shown in F i g u r e 2. 2. Knuth, D.W. Additional comments on a problem in concurrent
T h e r e a d e r should first note t h a t the use o f m u t e x 1 programming control. Comm. A C M 9, 5 (May 1966), 321-322.
3. de Bruijn, N.G. Additional comments on a problem in
a n d w c o r r e s p o n d s exactly to the use o f m u t e x a n d w in concurrent programming control. Comm. A C M 10, 3 (Mar. 1967),
the s o l u t i o n to P r o b l e m 1. T h e s e m a p h o r e r is used to 137-138.
p r o t e c t the act o f entering the critical section in the 4. Dijkstra, E.W. The structure of the "THE"-multiprogramming
system. Comm. A C M 1 1 , 5 (May 1968), 341-346.
s a m e way t h a t w is used to p r o t e c t the s h a r e d r e s o u r c e
in P r o b l e m 1. T h e first writer to pass P ( r ) will b l o c k
r e a d e r s f r o m entering the section which m a n i p u l a t e s
m u t e x 1 a n d w. m u t e x 2 is used here for writers j u s t as
m u t e x is used for r e a d e r s in P r o b l e m I. m u t e x 3 is
necessary b e c a u s e o f o u r a b s o l u t e insistence o n p r i o r i t y
for writers. W i t h o u t m u t e x 3 we have the possibility
t h a t a writer a n d one o r m o r e r e a d e r s will be simul-

668 Communications October 1971


of Volume 14
the ACM Number 10

You might also like