0% found this document useful (0 votes)
74 views10 pages

Lec 14 Reader Writer Problem and Monitor

Uploaded by

agrasen09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views10 pages

Lec 14 Reader Writer Problem and Monitor

Uploaded by

agrasen09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

OPERATING

SYSTEM:
ACSE0403A
OUTLINE

 1. Reader Writer Problem,


 2. Monitor
1. READER WRITER PROBLEM
 The readers-writers problem is a classical problem of process
synchronization, it relates to a data set such as a file that is shared
between more than one process at a time. Among these various
processes, some are Readers - which can only read the data set;
they do not perform any updates, some are Writers - can both read
and write in the data sets.
 The readers-writers problem is used for managing synchronization
among various reader and writer process so that there are no
problems with the data sets, i.e. no inconsistency is generated.
WHY READER WRITER PROBLEM?

 Let's understand with an example - If two or more than two readers want to access the file at the
same point in time there will be no problem. However, in other situations like when two writers or
one reader and one writer wants to access the file at the same point of time, there may occur some
problems, hence the task is to design the code in such a manner that if one reader is reading then
no writer is allowed to update at the same point of time, similarly, if one writer is writing no reader
is allowed to read the file at that point of time and if one writer is updating a file other writers
should not be allowed to update the file at the same point of time. However, multiple readers can
access the object at the same time.
SOLUTION FOR READER WRITER PROBLEM?
 The solution of readers and writers can
be implemented using binary
semaphores.
 In the above code of
reader, mutex and write are semaph
ores that have an initial value of 1,
whereas the readcount variable has
an initial value as 0.
Both mutex and write are common in
reader and writer process code,
semaphore mutex ensures mutual
exclusion and semaphore write
handles the writing mechanism.
MONITOR

A monitor is a synchronization mechanism that allows threads to


have:
• mutual exclusion – only one thread can execute the method at a
certain point in time, using locks
• cooperation – the ability to make threads wait for certain
conditions to be met, using wait-set
Solution to the Producer-Consumer problem using Monitors
monitor ProducerConsumer Producer();
condition full, empty; {
int count; while (TRUE)
{
procedure enter(); make_item(widget); // make a new
{ item
if (count == N) wait(full); // if buffer is full, ProducerConsumer.enter; // call enter
block function in monitor
put_item(widget); // put item in buffer }
count = count + 1; // increment count of }
full slots
if (count == 1) signal(empty); // if buffer was Consumer();
empty, wake consumer {
} while (TRUE)
procedure remove(); {
{ ProducerConsumer.remove; // call remove
if (count == 0) wait(empty); // if buffer is function in monitor
empty, block consume_item; // consume an item
remove_item(widget); // remove item from }
buffer }
count = count - 1; // decrement count of
ADVANTAGE AND LIMITATIONS OF MONITOR

Advantages
1. Mutual exclusion is automatic in monitors.
2. Monitors are less difficult to implement than semaphores.
3. Monitors may overcome the timing errors that occur when semaphores are used.
4. Monitors are a collection of procedures and condition variables that are combined in a
special type of module.
Limitations:
5. Monitors must be implemented into the programming language.
6. The compiler should generate code for them.
7. It gives the compiler the additional burden of knowing what operating system features
is available for controlling access to crucial sections in concurrent processes.
SEMAPHORE VS MONITOR
Thank
You

You might also like