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

L15

The document describes the Readers/Writers problem, where multiple readers can access a shared database simultaneously but an active writer has exclusive access. It presents two solutions: 1) Readers have priority over writers. A reader must wait if a writer is active, but writers may starve. 2) Writers have priority over readers. A writer waits if any readers or writers are active, otherwise it has exclusive access. When finished, it wakes waiting writers then readers. Both solutions use semaphores to synchronize access between readers and writers, with the first giving priority to readers and the second to writers.

Uploaded by

Basant Kumar
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)
92 views2 pages

L15

The document describes the Readers/Writers problem, where multiple readers can access a shared database simultaneously but an active writer has exclusive access. It presents two solutions: 1) Readers have priority over writers. A reader must wait if a writer is active, but writers may starve. 2) Writers have priority over readers. A writer waits if any readers or writers are active, otherwise it has exclusive access. When finished, it wakes waiting writers then readers. Both solutions use semaphores to synchronize access between readers and writers, with the first giving priority to readers and the second to writers.

Uploaded by

Basant Kumar
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

The Readers/Writers Problem (Courtois, 1971)

Readers/Writers w/ Readers Priority (Using Semaphores)


Initialization: semaphore mutex = 1; /* for mutual exclusion */ semaphore write = 1; /* for mut. ex. & synch. */ int readers = 0; /* number of readers */

Models access to a database, such as an airline reservation system


q

Multiple readers (customers) want to read the database access schedules, flight availability, etc. Multiple writers (the airline) want to write the database update the schedules

Reader: P(mutex); readers++; if (readers == 1) P(write); V(mutex);

Writer: P(write); write database V(write);

It is acceptable to have multiple readers at the same time


q

But while a writer is writing to (updating) the database, no one else can access the database either to read or to write

read database
P(mutex); readers ; if (readers == 0) V(write); V(mutex);

Two versions:
q q q

Readers have priority over writers Writers have priority over readers In most solutions, the non-priority group can starve
Fall 1998, Lecture 15 2

Fall 1998, Lecture 15

Notes on R/W w/ Readers Priority (Using Semaphores)


n

Notes on R/W w/ Readers Priority (Using Semaphores) (cont.)


n

Reader:
q

Writer:
q

Needs mutually exclusive access while manipulating readers variable Does not need mutually exclusive access while reading database If this reader is the first reader, it has to wait if there is an active writer (which has exclusive access to the database)
n

If there is an active writer, this writer has to wait (the active writer has exclusive access to database) If there are active readers, this writer has to wait (readers have priority)
n

First reader did a P(write)

First reader did a P(write)


q

If other readers come along while the first one is waiting, they wait at the P(mutex) If other readers come along while the first one is actively reading the database, they can also get in to read the database When the last reader finishes, if there are waiting writers, it must wake one up

The writer only gets in to write to the database when there are no other active readers or writers When the writer finishes, it wakes up someone (either a reader or a writer its up to the CPU scheduler) If a reader gets to go next, then once it goes through the V(mutex) and starts reading the database, then all other readers waiting at the top P(mutex) get to get in and read the database as well

Fall 1998, Lecture 15

Fall 1998, Lecture 15

Readers/Writers w/ Writers Priority (Using Semaphores)


Reader: P(mutex); if (AW+WW > 0) WR++; else { V(OKToRead); AR++; } V(mutex); P(OKToRead); Writer: P(mutex); if (AW+AR > 0) WW++; else { V(OKToWrite); AW++; } V(mutex); P(OKToWrite);
n

Notes on R/W w/ Writers Priority (Using Semaphores) Reader:


q

If there are active or waiting writers, this reader has to wait (writers have priority) Otherwise, this reader can read (possibly along with other readers) When the last reader finishes, if there are waiting writers, it must wake one up

Writer:
q

read database
P(mutex); AR ; if (AR == 0 && WW > 0) { V(OKToWrite); AW++; WW ; } V(mutex);

write database
P(mutex); AW ; if (WW > 0) { V(OKToWrite); AW++; WW ; } else if (WR > 0) { V(OKToRead); AR++; WR ; } V(mutex);
Fall 1998, Lecture 15 6

If there are active readers or writers, this writer has to wait (everyone has to finish before writer can update database) Otherwise, this writer can write (and has exclusive access to database) When the writer finishes,
n

(first choice) if there are waiting writers, it must wake one up (writers have priority) (second choice) if there are waiting readers, it must wake one up
Fall 1998, Lecture 15

Readers/Writers w/ Writers Priority (Using Locks and CVs)


Reader: acquire(mutex); while (AW+WW > 0) { WR++; wait(OKToRead); WR ; } AR++; release(mutex); Writer: acquire(mutex); while (AW+AR > 0) { WW++; wait(OKToWrite); WW ; } AW++; release(mutex);

Readers/Writers w/ Writers Priority (Using Locks and CVs, Solution 2)


Reader: acquire(mutex); while (i <0) wait(access); i++; release(mutex); Writer: acquire(mutex); while (i != 0) wait(access); i ; release(mutex);

read database
acquire(mutex); i ; if (i == 0) signal(access); release(mutex);
n

write database
acquire(mutex); i = 0; brcast(access); release(mutex);

read database

write database

acquire(mutex); acquire(mutex); AR ; AW ; if (AR == 0 && if (WW > 0) WW > 0) signal(OKToWrite); signal(OKToWrite); else release(mutex); brcast(OKToRead); release(mutex);

Notes:
q q q q

access conveys right to access If i > 0, i counts number of active readers If i == 0, no one is accessing the data If i < 0, there is an active writer
Fall 1998, Lecture 15

Fall 1998, Lecture 15

You might also like