0% found this document useful (0 votes)
3 views1 page

Readers and Writhers CH 5.2

The document outlines a Readers/Writers solution that prioritizes writers using monitors. It describes the correctness constraints for accessing a database, where readers can only access it when no writers are present, and vice versa. The implementation includes state variables and condition variables to manage the access of readers and writers effectively.

Uploaded by

Anthony D
Copyright
© © All Rights Reserved
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)
3 views1 page

Readers and Writhers CH 5.2

The document outlines a Readers/Writers solution that prioritizes writers using monitors. It describes the correctness constraints for accessing a database, where readers can only access it when no writers are present, and vice versa. The implementation includes state variables and condition variables to manage the access of readers and writers effectively.

Uploaded by

Anthony D
Copyright
© © All Rights Reserved
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/ 1

2/28/2019

Another Readers/Writers Solution Readers/Writers Solution: Give Priority to Writers


Using Monitors Monitor ReadersWriters {
int nr=0; nw=0; wr=0; ww=0;
• Correctness Constraints: cond *okToRead, *okToWrite;
– Readers can access database when no writers
readEnter() {
– Writers can access database when no readers or
writers while ((nw + ww) > 0) { //Is it safe to read?
– Only one thread manipulates state variables at a wr++; //No, writers exist
time okToRead->Wait(); //sleep on cond var
• State variables : wr--; //No longer waiting
– int nr: Number of active readers; initially = 0 }
– int wr: Number of waiting readers; initially = 0 nr++; //mark that reader is in
– int nw: Number of active writers; initially = 0 }
– int ww: Number of waiting writers; initially = 0 readExit() {
– Condition okToRead = NIL nr--; //one reader is out
– Condition okToWrite = NIL if ((nr == 0) && (ww >0)) //No other active readers
okToWrite->Signal(); //Wake up one writer
1 } 2

1 2

Readers/Writers Solution: Give Priority to Writers


writeEnter() {
while (nw > 0 || nr > 0) { //Is it safe to write?
ww++; //No, active users exist
okToWrite->Wait(); //block
ww--; //No longer waiting
}
nw++; //mark that writer is in
}
writeExit() {
nw--; //No longer active

if (ww > 0) { //Give priority to writers


okToWrite->Signal(); //Wake up one writer
} else if (wr > 0) { //Otherwise, wake reader
okToRead->Broadcast(); //Wake all readers
}
}
} // end monitor 3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy