the computer journal-1982-keedy-121-5

Upload: talat-numan

Post on 09-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 The Computer Journal-1982-Keedy-121-5

    1/5

    On Synchronizing Readers and Writers withSemaphores

    J. L. Keedy and J. R osenbergDepartment of Computer Science, Monash U niversity, Clayton, Victoria,3168, Australia.

    K. RamamohanaraoDepartment of Computer Science, University of Melbourne, Parkville, Victoria, 3052, Australia

    A w eakness in the reader priority solution proposed by Curtois, Heym ans and Para as for the problem of synchronizingconcurrent readers and writers is described and an improvement is explained. The difficulties of solving complexsynchronizing problems by using standard semaphore primitives, as illustrated by this example, lead u s to propo se thatspecial-purpose synchronization techniques should be supported by a judicious combination of hardware/microcodeand software routines. W e then describe an efficient solution for the reader/writer problem which is easy to understand,to implement and to use .

    INTRODUCTION

    A common synchronization problem occurs when aresource can be used in two modes, sharably by anynumber of 'readers' or exclusively by one 'writer' at atime. Curtois, Heymans and Parnas proposed twosolutions to the problem,1 one based on reader priority,the other on writer priority. In the first case no readershould be kept waiting unless a writer has alreadyobtained permission to use the resource. In the secondcase, oncea writer is ready to write heis given permissionto do so as soon as possible. Both solutions use P and V

    operations on semaphores as the primitive synchroniza-tion mechanism.2

    Although the problem, in both its forms, is easy tounderstand, the proposed solutions are by no meansstraightforward. In fact Curtoiset al. made specialmen tion of the effort involved in reaching their solutionsand of the unreasonable complexity of earlier versions oftheir solutions. We attest to similar experiences in ourown investigations of the problem, which included areformulation ofthe reader priority solutionin an attemptto remove an apparent weakness in the original version.We shall explain this weakness and present our alterna-tive solution as an illustration of the na ture of some of thedifficulties involved in this approach to the solution of

    synchronization problems and then propose an alterna-tive technique which removes most ofthe difficulties.

    AN EXAMINATION OF THE READERPRIORITY SOLUTION

    In this section we draw attention to a weakness in thereader priority solution proposed by Curtoiset al. andsuggest an improvement,1 thereby demonstrating someof the inadequacies of solving the problem using normalsemaphores.

    A weakness in the reader priority solution

    The aim of the.reader priority solution is to ensure

    integer readcount; (initial value = 0)semaphore mutex,w; (initial value for both = 1)

    WRITER

    writing is performed

    READERP (mutex);readcount '= readcount + 1;if readcount = 1 then P(iv);V'(mutex);

    reading is performed

    V(mutex);readcount = readcount 1;if readcount = 0 then V(w);V (mutex);

    Figure 1.

    minimum delay for readers (Fig. 1). Inspection of thesolution shows that whena writer is in its writing regionthe queue associated withw will contain zero or morewriters and zero or one reader, so that when the writerreleases w it is arbitrary whether another writer or areader will be awakened. (Curtoiset a/.'s discussion ofthe writer priority case makes clear that no assumptionsabout priority are built into the V operation). Th at thewqueue can hold more writers than readers suggests that inmany cases a writer process might be selected inpreference to a reader. In other words the solution doesnot guarantee that readers will experience minimumdelay.

    A revised reader priority solution

    Our solution, shown in Fig. 2 introduces an additionalbinary semaphoreextra which is claimed and releasedonly by writers. While a writer is in its writing region allother writers are queued onextra, not on w. Thus whenw is released the waiting reader, if any, will be awakened,thereby removing the weakness in the original solution.

    As far as we can tell the solution app ears to be correct(in some loose sense), but we have several reservationsregarding both it and similar approaches to solvingcomplex synchronization problems.

    CCC-00KM 620/02/0O25-O121 $02.50 Heyden & Son Ltd, 1982 THE COMPUTER JOURNAL, VOL. 25, NO. 1,19821 2 1

    b y g u e s t on

    J an

    u ar y 1 7

    ,2

    0 1 1

    c om

    j nl . ox f or d

    j o ur n

    al s . or g

    D

    ownl o

    a d e d f r om

    http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/
  • 8/8/2019 The Computer Journal-1982-Keedy-121-5

    2/5

    J. L. KEEDY, J. ROSENBERG A ND K . RAMAMOHANARAO

    integer readcount; (initial value = 0)semaphore mulex, w, extra; (initial value of each = 1)READER WRITERP (mutex);readcount '= readcount + 1; P (extra);if readcount = 1 then P(w); P (w);V (mutex);

    reading is performed writing is performed

    P (mutex);readcount '= readcount 1;if readcount = 0 then V(M>);V (mutex);

    V (extra);

    Figure 2 . An improved reader priority solution.

    (1) Th e problem s are usually easy to state and simple tounderstand; the solutions are usually complex and itis not easy to understand them nor to gain confidencein their correctness.

    (2) It is easy to waste much time and effort in solvingapparently trivial problems, and it is easy to make

    mistakes.(3) The solutions are at best approximations. Consider,for example, what is meant by a phrase such as 'areader is ready to read'. Our commonsense tells usthat it refers to the point at which the reader beginsthe re ader e ntry protocol, i.e. when it executes theP(mutex) operation. But then if a writer exits hiswriting region after the reader has executed theP(mutex), but before he has reached theP(w) operation,it is possible that another writer may reach theP(w)operation first and thus gain access to his writingregion although a reader 'was ready to read'. Similarproblems with the writer priority solution have beendiscussed in the literature. A common approach has

    been to redefine operations on semaphores, makingthem more powerful.3"7 Despite claims to thecontrary it is not always easy to arrive at nor tounderstand solutions using extended semaphoreprim itives intend ed for general purpose use. Nor is italways easy to implem ent the p roposed extensions onreal computers. In the following section we proposean alternative and m ore practical approach.

    p-insXr (sent, condition);

    it condition = 'sem < 0 ' then suspend (semqueue).

    The P macro

    v-instr (sem, condition)if condition = 'sem

  • 8/8/2019 The Computer Journal-1982-Keedy-121-5

    3/5

  • 8/8/2019 The Computer Journal-1982-Keedy-121-5

    4/5

    J . L . K E E D Y , J . R O S E N B E R G A N D K . R A M A M O H A N A R A O

    macro read-c la im (x : reader-writer-semaphore);var c-c: boolean;begin read-p (x ,c-c);

    if c-c then suspend (reader-queue)end;

    macro read-release (x: reader-writer-semaphore);var c-c: boolean;begin read-v (x,c-c);

    if c-c then act iva te (writer-queue, 1)end;

    macro wri te-c la im (x : reader-writer-semaphore);var c-c: boolean;begin wri te-p (x, c-c);

    if c-c then suspend (writer-queue)end;

    macro write-release (x: reader-writer-semaphore);var c-c: boolean;

    rcount: 0 . . . # processes;begin write-v (x, c-

  • 8/8/2019 The Computer Journal-1982-Keedy-121-5

    5/5

    ON SYNCHRONIZING READERS AND WRITERS WITH SEMAPHORES

    to include an additional boolean variable in eachreader/writer semaphore, initialized to indicate whetherreader or writer priority is to be applied to the pa rticularresource controlled by the semaphore. This will be testedin the read-p and write-v operations(see 'The MicrocodedInstruc tions'); very little additional m icrocodeis requiredto cover both cases. The great benefit is that the caller

    need not be aware ofthe priority requirements and codethem into his program (which is necessary with thestandard semaphore solution). Instead the same code canbe linkedto different objects which operate with differentpriority requirements.

    At present we are developing new versions both of the

    MONADS Operating System and its supporting hard-ware, incorporating the proposed technique. Initialresults confirm our view tha t it is easy to implem ent andto use, and that it provides an efficient solution for thesynchronization of readers and writers.

    Acknowledgments

    Figure 1 is reprinted from Curtois et al.x

    by permission of theAssociation for Computing Machinery. The work described in thispaper was supported by grant F77/15337 from the Australian R esearchGrants Committee and by grant SC 18/79 from the Monash SpecialResearch Fund.

    REFERENCES

    1. P. J. Courtois, F. Heymans and D. L. Parnas, Concurrent controlwith 'readers' and 'writers'. Communications of the ACM 14(No. 10) , 667-66 8 (1 971) .

    2. E. W. Dijkstra, Cooperating sequential processes, in Program-ming Lanuages, ed. by F. Genuys, Academic Press, London(1968) .

    3. V. G. Cerf, Multiprocessors, Semaphores and a Graph Model ofComputation, ENG-7226, Computer Science Department,University of California, Los Angeles ( 197 2).

    4. H. Vantiborgh and A. van Lamsweerde, On an extension toDijkstra's semaphore primitives.Information Processing Letters1 , 1 8 1 - 1 8 6 ( 1 9 7 2 ) .

    5. P. Wodon. Still Another Tool for Controlling CooperatingAlgorithms, Department of Computer Science, Carnegie Me llonUniversity (1972).

    6. T. Agerwala, Some Extended Semaphore Primitives. AdaInformatica 8, 201-22 0 (1977 ) .

    7. R. Conradi, Some comments on 'concurrent readers andwriters'. Ada Informatica 8, 335-340 (1977) .

    8. J. L. Keedy, K. R amamohanarao and J. Rosenberg, OnImplemen ting Semaphores wi th sets. The Computer Journal 22(No. 2) , 146 -150(1979) .

    9. J. L. Keedy, An outline of the ICL 2900 series systemarchitecture. Australian Computer Journal 9 (No. 2) , 53-62(1977) .

    10. Digital Equipment Corporation, VAX11-780 ArchitectureHandbook. Digital Equipment Corporation (1977).

    11 . Hewlett Packard, 2100A Computer Reference Manual. Hewlet tPackard Company 021 00-9 0001 (December 197 1).

    12. B. H. Liskov, The design of the Venus operating system.Communications of the AC M "\S (No. 3) , 144-149 (19 72) .

    13. J. L. Keedy and J. Rosenberg, On the Handling of the LowLevel System Processes, MONADS Report No. 6, Departmentof Computer Science, Monash University, Melbourne (1979).

    14. J. Rosenberg, The concept of a hardware kernel and itsimplem entation on a minicomputer. PhD . Thesis, MonashUniversity, Melbourne (1 979).

    15. P. J. Denning and T. D. Dennis, On minimizing contention atsemaphores. ACM Operating Systems Review 14 (No. 2) , 9-16(1980).

    Received February 1981

    H eyd en & Son Ltd, 1982

    Heyden & Son Ltd, 1982 THE COMPUTER JOURNAL, VOL. 25. NO. 1,1982 1 2 5

    b y g u e s t on

    J an

    u ar y 1 7

    ,2

    0 1 1

    c om

    j nl . ox f or d

    j o ur n

    al s . or g

    D

    ownl o

    a d e d f r om

    http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/http://comjnl.oxfordjournals.org/