On Synchronizing Readers and Writers with Semaphores

On Synchronizing Readers and Writers with Semaphores

On Synchronizing Readers and Writers with Semaphores J. L. Keedy and J. Rosenberg Department of Computer Science, Monash University, Clayton, Victoria, 3168, Australia. K. Ramamohanarao Department of Computer Science, University of Melbourne, Parkville, Victoria, 3052, Australia A weakness in the reader priority solution proposed by Curtois, Heymans and Paraas for the problem of synchronizing Downloaded from https://academic.oup.com/comjnl/article/25/1/121/527286 by guest on 29 September 2021 concurrent readers and writers is described and an improvement is explained. The difficulties of solving complex synchronizing problems by using standard semaphore primitives, as illustrated by this example, lead us to propose that special-purpose synchronization techniques should be supported by a judicious combination of hardware/microcode and software routines. We then describe an efficient solution for the reader/writer problem which is easy to understand, to implement and to use. integer readcount; (initial value = 0) INTRODUCTION semaphore mutex, w; (initial value for both = 1) READER WRITER A common synchronization problem occurs when a P (mutex); resource can be used in two modes, sharably by any readcount '= readcount + 1; number of 'readers' or exclusively by one 'writer' at a if readcount = 1 then P(iv); time. Curtois, Heymans and Parnas proposed two V'(mutex); solutions to the problem,1 one based on reader priority, the other on writer priority. In the first case no reader reading is performed writing is performed should be kept waiting unless a writer has already obtained permission to use the resource. In the second V(mutex); case, once a writer is ready to write he is given permission readcount = readcount — 1; to do so as soon as possible. Both solutions use P and V if readcount = 0 then V(w); operations on semaphores as the primitive synchroniza- V (mutex); 2 tion mechanism. Figure 1. Although the problem, in both its forms, is easy to understand, the proposed solutions are by no means minimum delay for readers (Fig. 1). Inspection of the straightforward. In fact Curtois et al. made special solution shows that when a writer is in its writing region mention of the effort involved in reaching their solutions the queue associated with w will contain zero or more and of the unreasonable complexity of earlier versions of writers and zero or one reader, so that when the writer their solutions. We attest to similar experiences in our releases w it is arbitrary whether another writer or a own investigations of the problem, which included a reader will be awakened. (Curtois et a/.'s discussion of reformulation of the reader priority solution in an attempt the writer priority case makes clear that no assumptions to remove an apparent weakness in the original version. about priority are built into the V operation). That the w We shall explain this weakness and present our alterna- queue can hold more writers than readers suggests that in tive solution as an illustration of the nature of some of the many cases a writer process might be selected in difficulties involved in this approach to the solution of preference to a reader. In other words the solution does synchronization problems and then propose an alterna- not guarantee that readers will experience minimum tive technique which removes most of the difficulties. delay. AN EXAMINATION OF THE READER A revised reader priority solution PRIORITY SOLUTION Our solution, shown in Fig. 2 introduces an additional In this section we draw attention to a weakness in the binary semaphore extra which is claimed and released reader priority solution proposed by Curtois et al. and 1 only by writers. While a writer is in its writing region all suggest an improvement, thereby demonstrating some other writers are queued on extra, not on w. Thus when of the inadequacies of solving the problem using normal w is released the waiting reader, if any, will be awakened, semaphores. thereby removing the weakness in the original solution. As far as we can tell the solution appears to be correct A weakness in the reader priority solution (in some loose sense), but we have several reservations regarding both it and similar approaches to solving The aim of the.reader priority solution is to ensure complex synchronization problems. CCC-00KM620/02/0O25-O121 $02.50 © Heyden & Son Ltd, 1982 THE COMPUTER JOURNAL, VOL. 25, NO. 1,1982 121 J. L. KEEDY, J. ROSENBERG AND K. RAMAMOHANARAO integer readcount; (initial value = 0) p-insXr (sent, condition); semaphore mulex, w, extra; (initial value of each = 1) it condition = 'sem < 0' then suspend (semqueue). READER WRITER The P macro P (mutex); readcount '= readcount + 1; P (extra); v-instr (sem, condition) if readcount = 1 then P(w); P (w); if condition = 'sem <; 0' then activate (semqueue). V (mutex); The V macro p-instr and u-instr are indivisible hardware/microcoded instructions; reading is performed writing is performed suspend and activate are indivisible queuing routines in the process scheduler. The testing of condition values in both cases assumes that P (mutex); the decrement/increment occurs before the value of sem is tested. readcount '= readcount — 1; if readcount = 0 then V(M>) ; V (extra); V (mutex); Figure 3. P and V macros based on hardware/microcoded operations. Figure 2. An improved reader priority solution. vate; suspend). The practicality of implementing such a (1) The problems are usually easy to state and simple to mechanism has been demonstrated in the ICL2900 Series Downloaded from https://academic.oup.com/comjnl/article/25/1/121/527286 by guest on 29 September 2021 understand; the solutions are usually complex and it and in other recent systems.9 is not easy to understand them nor to gain confidence Our basic solution for the readers/writers problem can in their correctness. be regarded as an extension of this technique. We (2) It is easy to waste much time and effort in solving propose that indivisible hardware/microcoded instruc- apparently trivial problems, and it is easy to make tions are provided to establish a claim for reading, read- mistakes. p, and for writing, write-p, and to release from reading, (3) The solutions are at best approximations. Consider, read-v, and from writing, write-v. In addition we extend for example, what is meant by a phrase such as 'a the indivisible 'activate' and 'suspend' primitives to reader is ready to read'. Our commonsense tells us support this case. In the following subsections we shall that it refers to the point at which the reader begins describe the proposed new primitives in detail, showing the reader entry protocol, i.e. when it executes the P that they can be efficiently implemented. We shall then (mutex) operation. But then if a writer exits his attempt to justify why this appears preferable to previous writing region after the reader has executed the P approaches. (mutex), but before he has reached the P(w) operation, it is possible that another writer may reach the P(w) operation first and thus gain access to his writing The microcoded instructions region although a reader 'was ready to read'. Similar problems with the writer priority solution have been The structure of a 'readers/writers semaphore', a type of discussed in the literature. A common approach has variable on which read-p, read-v, write-p. and write-v been to redefine operations on semaphores, making instructions operate, is shown in Fig. 4. The boolean them more powerful.3"7 Despite claims to the contrary it is not always easy to arrive at nor to understand solutions using extended semaphore primitives intended for general purpose use. Nor is it Current Waiting Waiting Current always easy to implement the proposed extensions on Readers Readers Writers Writer real computers. In the following section we propose an alternative and more practical approach. Figure 4. Structure of a readers/writers semaphore. field current-writer indicates whether any writer is in its READER/WRITER SEMAPHORE PRIMITIVES writing region; the remaining integer fields count the number of processes waiting to read, currently reading The basic solution and/or waiting to write. The initial values of these fields are false and zero, as appropriate. Elsewhere, in connection with a different synchronization The indivisible hardware or microcoded operations on problem, we have described how P and V operations on these variables are outlined in a Pascal-like notation in semaphores can be efficiently implemented as two-part Fig. 5. Each procedure sets a local condition code operations: an indivisible hardware or microcode instruc- indicating whether a subsequent call must be made on tion (which decrements or increments a semaphore the queuing procedures (if true) or not (if false) in order variable and sets a condition code showing the result of to complete the required protocol. the operation) and an indivisible operation to suspend or It is the microcoded operations which determine when activate a process (which is typically a software routine 8 processes must be suspended and activated, and in implemented as part of the process scheduler). These consequence they determine the priority rules. Figure 5 operations are combined as shown in Fig. 3 to form actually describes the writer priority situation, but it is macro instructions which implement the P and V easily modified to reflect reader priority: read-v and write- operations. Notice that the suspend/activate operations P remain unchanged; in read-p the conditional expression must be commutative in the sense that each activate is reduced to if not current-writer then ...; the order of the wakes up exactly one process and that the sequence tests in write-v is changed so that waiting readers are (suspend; activate) has the same nett effect as <acti- given priority over waiting writers.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    5 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us