Classical Syncronization Issues
Classical Syncronization Issues
Operating Systems
Reading Assignment
3rd Year
1st Semester
9/9/2014
Table of Contents
1. Readers-Writers Problem....................................................................................................................3
1.1 The solution for First Readers Writers problem...........................................................................4
2. Dining Philosophers Problem...............................................................................................................6
2.1 Solution for Dining Philosophers Problem.........................................................................................7
3. Sleeping Barber Problem.....................................................................................................................8
References.................................................................................................................................................11
1. Readers-Writers Problem
A data object to be shared among several concurrent processes. Some of these processes may only want
to read the content of the shared object, whereas others may want to update the shared object. We
distinguish between these two types of processes by referring to those processes that are interested in only
reading as readers and to the rest as writers. Obviously, if two readers access the shared data object
simultaneously, no adverse effect will result. However if a writer and some other process access the
shared object simultaneously, chaos may ensure.
To ensure that chaos does not arise, require that Writers have “exclusive” access to shared database –This
concept is what gives rise to the Readers-Writers problem , which is the basis to test many other
synchronization problems.
First R-W Problem –requires that no Reader be kept waiting unless a Writer has already obtained
permission to use shared database obtained permission to use shared database.
Second R-W Problem – once a Writer is ready, that Writer must perform its write, right away –i.e., if a
Writer is waiting to access, then no Reader can access.
A solution to either problem may result in starvation. In the first case writers may starve. In the second
case readers may starve.
1.1 The solution for First Readers Writers problem
In the solution the reader process share the following data structure.
int readcount;
Both mutex and wrt are initializes to 1, and readcount is initialized to 0. The semaphore wrt is same to
both reader and writer processes. The mutex semaphore is used to ensure mutual exclusion when the
variable readcount is updated. The readcount variable keeps track of how many processes are currently
reading the object. The semaphore wrt acts as a mutual exclusion semaphore as for the writers. It is also
used by the first or the last reader that enters or exits the critical section. It is not used by readers who
enter or exit while other readers are in their critical sections.
If a writer is in its critical section and n readers are waiting, then one reader is queued on wrt, and n-1
readers are queued on mutex.. And when a writer executes signal (wrt), we may resume the execution of
either the waiting readers or a single waiting writer. This selection is made by the scheduler.
Writer Process
Wait(wrt);
Writing is performed
……………
Signal(wrt);
Consumer process Reader Process
do{
wait(full); wait(mutex);
……… if(readcount==1)
………. signal(mutex);
Signal(mutex); ………
…………… ……….
}while(1); if(readcount==0)
signal(wrt);
signal(mutex);
2. Dining Philosophers Problem
One simple solution is to represent each chopstick with a semaphore. A Philosopher tries to grab the
chopstick by executing an wait() operation on the semaphore and releases the chopstick by executing
an signal() operation on the semaphore.
Semaphore chopstick[5];
Where all elements of chopstick are initializes to 1. This solution guarantees that no 2 neighboring
Philosophers are eating simultaneously, however it must be rejected as it has the possibility of
creating a deadlock. If all five philosophers become hungry simultaneously, and each grab her left
chopstick. All the elements of the chopstick is now 0.When each philosopher try to grab her right
chopstick she will be delayed forever. Several possible remedies for this problem are as follows.
2.1 Solution for Dining Philosophers Problem.
Each of the above solutions prevents deadlock by placing restrictions on the Philosopher.
Do{
……………
Eat
……………..
………..
Think
………….
} while(1);
3. Sleeping Barber Problem
Another classical IPC problem takes place in a barber shop. The barber shop has one barber, one barber
chair, and n chairs for waiting customers, if any, to sit on. If there are no customers present, the barber
sits down in the barber chair and falls asleep. When a customer arrives, he has to wake up the sleeping
barber. If additional customers arrive while the barber is cutting a customer's hair, they either sit down (if
there are empty chairs) or leave the shop (if all chairs are full). The problem is to program the barber and
the customers without getting into race conditions. This problem is similar to various queuing situations,
such as a multi person helpdesk with a computerized call waiting system for holding a limited number of
incoming calls.
The solution uses three semaphores, customers, which counts waiting customers (excluding the customer
in the barber chair, who is not waiting), barbers, the number of barbers (0 or 1) who are idle, waiting for
customers, and mutex, which is used for mutual exclusion. We also need a variable, waiting, which also
counts the waiting customers. The reason for having waiting is that there is no way to read the current
value of a semaphore. In this solution, a customer entering the shop has to count the number of waiting
customers. If it is less than the number of chairs, he stays; otherwise, he leaves.
Our solution is shown . When the barber shows up for work in the morning, he executes the procedure
barber, causing him to block on the semaphore customers because it is initially 0. The barber then goes to
sleep. He stays asleep until the first customer shows up.
When a customer arrives, he executes customer, starting by acquiring mutex to enter a critical region. If
another customer enters shortly thereafter, the second one will no be able to do anything until the first one
has released mutex. The customer then checks to see if the number of waiting customers is less than the
number of chairs. If not, he releases mutex and leaves without a haircut.
If there is an available chair, the customer increments the integer variable, waiting. Then he does an Up
on the semaphore customers, thus waking up the barber. At this point, the customer and the barber are
both awake. When the customer releases mutex, the barber grabs it, does some housekeeping, and begins
the haircut.
When the haircut is over, the customer exits the procedure and leaves the shop. There is no loop for the
customer because each one gets only one haircut. The barber loops, however, to try to get the next
customer. If one is present, a haircut is given. If not, the barber goes to sleep.
As an aside, it is worth pointing out that although the readers and writers and sleeping barber problems do
not involve data transfer, they still belong to the area of IPC because they involve synchronization
between multiple processes.
void barber(void) {
while (TRUE) {
}}
void customer(void) {
} else {
}
References
[2]https://www.google.lk/url?
sa=t&rct=j&q=&esrc=s&source=web&cd=4&cad=rja&uact=8&ved=0CC8QFjAD&url=http%3A%2F
%2Fcaig.cs.nctu.edu.tw%2Fcourse
%2FOS10%2FOS_SleepingBarber_F10.pdf&ei=kWIMVMW0Ms6zuAScv4GoAQ&usg=AFQjCNHaFY
Jfk4yYFRKHhPzZptw37d20SA