Given the following code of a monitor that is designed to solve the readers/writ
ID: 3861405 • Letter: G
Question
Given the following code of a monitor that is designed to solve the readers/writers problem in FCFS order while maintaining exclusive write and concurrent read:
RW : monitor;
begin
readercount : integer;
busy : boolean;
FCFS_Q, write_Q : condition;
procedure startread;
begin
if busy OR FCFS_Q.queue OR write_Q.queue then FCFS_Q.wait;
readercount := readercount + 1;
FCFS_Q.signal;
end startread;
procedure endread;
begin
readercount := readercount -1;
if readercount = 0 then
if write_Q.queue then write_Q.signal
else FCFS_Q.signal;
end endread;
procedure startwrite;
begin
if busy OR readercount ¹ 0 OR FCFS_Q.queue then FCFS_Q.wait;
if readercount ¹ 0 then write_Q.wait;
busy := true;
end startwrite;
procedure endwrite;
begin
busy := false;
if FCFS_Q.queue then FCFS_Q.signal;
end endwrite;
begin /* Main program of the monitor, used for initialization */
readercount := 0;
busy := false;
end;
end RW;
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 execute (not counting the time in queue). For simplicity, you may assume 1 second even when only part of the procedure gets executed.
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 RW.startread; read; call RW.endread.
Write: call RW.startwrite; write; call RW.endwrite.
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.
Time in
Process
Procedure
Variables
Condition variable queues
Process removed
Second
executed
readercount
busy
FCFS_Q
write_Q
from any queue
Initially
None
None
0
false
empty
empty
None
0
R1
startread
1
Given the following code of a monitor that is designed to solve the readers/writers problem in FCFS order while maintaining exclusive write and concurrent read:
RW : monitor;
begin
readercount : integer;
busy : boolean;
FCFS_Q, write_Q : condition;
procedure startread;
begin
if busy OR FCFS_Q.queue OR write_Q.queue then FCFS_Q.wait;
readercount := readercount + 1;
FCFS_Q.signal;
end startread;
procedure endread;
begin
readercount := readercount -1;
if readercount = 0 then
if write_Q.queue then write_Q.signal
else FCFS_Q.signal;
end endread;
procedure startwrite;
begin
if busy OR readercount ¹ 0 OR FCFS_Q.queue then FCFS_Q.wait;
if readercount ¹ 0 then write_Q.wait;
busy := true;
end startwrite;
procedure endwrite;
begin
busy := false;
if FCFS_Q.queue then FCFS_Q.signal;
end endwrite;
begin /* Main program of the monitor, used for initialization */
readercount := 0;
busy := false;
end;
end RW;
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 execute (not counting the time in queue). For simplicity, you may assume 1 second even when only part of the procedure gets executed.
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 RW.startread; read; call RW.endread.
Write: call RW.startwrite; write; call RW.endwrite.
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.
Time in
Process
Procedure
Variables
Condition variable queues
Process removed
Second
executed
readercount
busy
FCFS_Q
write_Q
from any queue
Initially
None
None
0
false
empty
empty
None
0
R1
startread
1
Time in
Process
Procedure
Variables
Condition variable queues
Process removed
Second
executed
readercount
busy
FCFS_Q
write_Q
from any queue
Initially
None
None
0
false
empty
empty
None
0
R1
startread
1
Given the following code of a monitor that is designed to solve the readers/writers problem in FCFS order while maintaining exclusive write and concurrent read:
RW : monitor;
begin
readercount : integer;
busy : boolean;
FCFS_Q, write_Q : condition;
procedure startread;
begin
if busy OR FCFS_Q.queue OR write_Q.queue then FCFS_Q.wait;
readercount := readercount + 1;
FCFS_Q.signal;
end startread;
procedure endread;
begin
readercount := readercount -1;
if readercount = 0 then
if write_Q.queue then write_Q.signal
else FCFS_Q.signal;
end endread;
procedure startwrite;
begin
if busy OR readercount ¹ 0 OR FCFS_Q.queue then FCFS_Q.wait;
if readercount ¹ 0 then write_Q.wait;
busy := true;
end startwrite;
procedure endwrite;
begin
busy := false;
if FCFS_Q.queue then FCFS_Q.signal;
end endwrite;
begin /* Main program of the monitor, used for initialization */
readercount := 0;
busy := false;
end;
end RW;
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 execute (not counting the time in queue). For simplicity, you may assume 1 second even when only part of the procedure gets executed.
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 RW.startread; read; call RW.endread.
Write: call RW.startwrite; write; call RW.endwrite.
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.
Time in
Process
Procedure
Variables
Condition variable queues
Process removed
Second
executed
readercount
busy
FCFS_Q
write_Q
from any queue
Initially
None
None
0
false
empty
empty
None
0
R1
startread
1
Explanation / Answer
readers-writers : monitor;
begin
readercount : integer;
busy : boolean;
OKtoread, OKtowrite : condition;
procedure startread;
begin
if busy then OKtoread.wait;
readercount := readercount + 1;
OKtoread.signal;
(* once one reader can start, they all can *)
end startread;
procedure endread;
begin
readercount := readercount - 1;
if readercount = 0 then OKtowrite.signal;
end endread;
procedure startwrite;
begin
if busy OR readercount != 0 then OKtowrite.wait;
busy := true;
end startwrite;
procedure endwrite;
begin
busy := false;
if OKtoread.queue then OKtoread.signal
else OKtowrite.signal;
end endwrite;
begin (* initialization *)
readercount := 0;
busy := false;
end;
end readers-writers;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.