Question
//Lab Assignment Monitors // STATE VARIABLES // Number of active readers; initially = 0 int NReaders = 0; // Number of waiting readers; initially =
//Lab Assignment Monitors
// STATE VARIABLES
// Number of active readers; initially = 0
int NReaders = 0;
// Number of waiting readers; initially = 0
int WaitingReaders = 0;
// Number of active writers; initially = 0
int NWriters = 0;
// Nmber of waiting writers; initially = 0
int WaitingWriters = 0;
Condition canRead = NULL;
Condition canWrite = NULL;
Void BeginWrite()
{
// A writer can enter if there are no other
// active writers and no readers are waiting
if (NWriters == 1 || NReaders > 0) {
++WaitingWriters;
wait(CanWrite);
--WaitingWriters;
}
NWriters = 1;
}
Void EndWrite()
{
NWriters = 0;
// Checks to see if any readers are waiting
if (WaitingReaders)
Signal(CanRead);
Else
Signal(CanWrite);
}
Void BeginRead()
{
// A reader can enter if there are no writers active or waiting, so we can have many readers //active all at once
if (NWriters == 1 || WaitingWriters > 0) {
++WaitingReaders;
// Otherwise, a reader waits (maybe many do)
Wait(CanRead);
--WaitingReaders;
}
++NReaders;
Signal(CanRead);
}
Void EndRead()
{
// When a reader finishes, if it was the last reader,
// it lets a writer in (if any is there).
if (--NReaders == 0)
Signal(CanWrite);
}
Suppose the above monitor is used to control the access to a shared file among a number of readers and writers. Assume that the sequence of arrivals of processes that require access to the shared resource be: R1, W1, R2, R3, W2. Here R1, R2, R3, W1, and W2 are processes that are in existence concurrently. For simplicity, assume further the following:
- These arrivals are 1 second apart.
- Each of the monitor procedure takes 1 second to complete its execution (not counting the time in queue).
- Each operation, read or write, takes 5 seconds.
- All processes that require access to the shared file will follow the 3-step procedure:
- Reader: call BeginRead(); Read(); call EndRead().
- Write: call BeginWrite(); Write(); call EndWrite()
YOUR task is to complete the table below and answer the following questions.
Note: The time durations provided above are to enable you to determine the approximate timing of events. Leave an entry in the table below blank when there is no change
Fill up the whole table, U need more rows
Time in | Process | Procedure | Variables | Condition variable queues | Process removed | ||
Second |
| executed | NWriters | NReaders | CanRead | CanWrite | from any queue |
Initially | None | None | 0 | false | empty | empty | None |
0 | R1 | Begin Read | 1 | False | Empty | Empty | None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Algorithms premises
Can Readers access the data along with some active Writers?
Is it true that writers can access database only when there are no readers or
writers?
How many threads can manipulate the state variables at a time?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started