0% found this document useful (0 votes)
21 views

7-Semaphore Example Readers and Writers

Uploaded by

Sidhesh mishra
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

7-Semaphore Example Readers and Writers

Uploaded by

Sidhesh mishra
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

CS 537 Notes, Section #7: Semaphore

Example: Readers and Writers.

Semaphore usage generally falls into two classes:

1. Uniform resource usage, simple first-in-first-out scheduling: use semaphores for


everything. This is usually the case. Use one semaphore for each constraint in the
system.
2. More complex patterns of resource usage: interaction between different users of a
resource, or changing priorities: semaphores cannot capture the scheduling all by
themselves. Must use state variables to record information about priorites,
resource state. In this case, semaphores get used for two things:
o One semaphore for mutual exclusion on the state variables.
o One semaphore for each class of waiting; used just as a convenience to
make a process wait. In the worst case, one semaphore per process.

Whenever possible, cast problems into the first class. This usually can be done.

Unfortunately, sometimes a resource is shared by different classes of users; that


is, they use the resource in different ways. Potentially the different kinds of usage
interact. For example, consider a shared database with readers and writers. It is
safe for any number of readers to access the database simultaneously, but each
writer must have exclusive access. Example: checking account (statement-
generators are readers, tellers are writers).

o Note that writers are actually readers too.


o In this case, the constraints are too complicated to be solved single-
handedly with semaphores.
o Constraints:
 Readers can only proceed if there are no active or waiting writers
(use semaphore OKToRead).
 Writers can only proceed if there are no active readers or writers
(use semaphore OKToWrite).
 Only one process may manipulate internal state variables at once
(use semaphore Lock).
o Scheduling: writers get preference.
o State variables:
 AR = number of active readers.
 WR = number of waiting readers.
 AW = number of active writers.
 WW = number of waiting writers.
AW is always 0 or 1. AR and AW may not both be non-zero.

o Initialization:
 semaphore OKToRead = new semaphore(0);
 semaphore OKToWrite = new semaphore(0);
 semaphore = new semaphoreLock(1);
 int AR = 0, WR = 0, AW = 0, WW = 0;

Reader Process: Writer Process:


Lock.P(); Lock.P();
if ((AW+WW) == 0) { if ((AW+AR+WW) == 0) {
OKToRead.V(); OKToWrite.V();
AR++; AW++;
} else { } else {
WR++; WW++;
} }
Lock.V(); Lock.V();
OKToRead.P(); OKToWrite.P();

// --read the necessary data-- // --write the necessary data--

Lock.P(); Lock.P();
AR--; AW--;
if ((AR == 0) and (WW > 0)) { if (WW>0) {
OKToWrite.V(); OKToWrite.V();
AW++; AW++;
WW--; WW--;
} } else {
Lock.V(); while (WR>0) {
OKToRead.V();
AR++;
WR--;
}
}
Lock.V();

Examples:

o Reader enters and leaves system.


o Writer enters and leaves system.
o Two readers enter system.
o Writer enters system and waits.
o Reader enters system and waits.
o Readers leave system, writer continues.
o Writer leaves system, last reader continues and leaves.
Questions:

o In case of conflict between readers and writers, who gets priority?


o Is the WW necessary in the writer's first if?
o Can OKToRead ever get greater than 1? What about OKToWrite?
o Is the first writer to execute Lock.P() guaranteed to be the first writer to
access the data?

Copyright © 1997, 2002 Barton P. Miller

You might also like