State the Readers and Writers Problem and write its semaphore based solution. Also describe the algorithm. Can the producer consumer problem be considered as a special xase of Reader/Writer problem with a single Writer(the producer and a single Reader (consumer) Explain.)
readers
- writers : monitor begin readercout: interger; 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 startend; procedure endread;
begin
readercount: = readercount - 1; if readercount =0 then OKtowrite.signal; end
endread;
procedure
startwrite; begin if busy OR readercout !=0 then OKtowrite.wait; busy : = true;
end
startwrite
; procedure endwrite; begin busy:=false; if OKtoread.queue then OKtoread.signal
else
OKtowrite.signal;
end OKtowrite.sigal; end endwrite; begin(*initialization*) readercount =0;
busy:
false; end; end readers - writers;
The above code gives a
solution to the readers priority problem using monitors. For
proper synchronization reader
processes must call the startread procedure before
accessing the file and
call the endread when the read is finished. Likewise, writer
processes must call startwrite
before modifying the file and call endwrite when the write
is finished. The
monitor users the Boolean variables busy to indicate whether a writer is
active and readercount
to keep track of the number of active readers.
On invoking startread,
a reader process is blocked and placed in the queue of the
OKtoread condition
variable if busy is true Otherwise, the reader proceeds and performs
the following. The process
increments the readercount, and activated a waiting reader, if
present, thought the
OKtoread.signal operation. On the completion of access, a reader
invokes endread, where
readercount is decremented. When there are no active readers,
the last exiting reader
process performs the Oktowrite.signal operation to activate any
waiting
writer.
A writer, on invoking start
write, proceeds only when no other writer or readers are
active. The process
sets busy to true to indicate that a writer is active. On completion of
the access, a writer invokes
the end write procedure. The end write procedure sets busy to
false, indicating that
no writer is active, and checks the OK to read queue for the presence
of waiting readers. If
there is a waiting reader, the exiting writers queue. If a reader
otherwise it signals
the writer queue. If a reader is activated in end write procedure, it
increments the reader
count and executes the OK to read signal, thereby activating the next waiting reader in the queue. This process
continues until all the waiting readers have been activated, during
which processes trying to enter the monitor are blocked and join the monitor's
entry queue. But, after all the readers waiting on the OK to read condition
have been signed, any newly arrived readers will again access to the monitor
before any waiting writers. In summary, the readers priority monitor, while not
permitting a new writer to start when there is a reader waiting, permits any
number of readers to proceed, as long as there is at least one active reader.
State the Readers and Writers Problem and write its semaphore based solution. Also describe the algorithm. Can the producer consumer problem be considered as a special xase of Reader/Writer problem with a single Writer(the producer and a single Reader (consumer) Explain.)
Reviewed by enakta13
on
August 28, 2012
Rating: