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.
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 ratings0% 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.
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