0% found this document useful (0 votes)
66 views

Classical Syncronization Issues

This document discusses three classic concurrency problems: the readers-writers problem, dining philosophers problem, and sleeping barber problem. For the readers-writers problem, it describes the challenges of having concurrent processes accessing shared data and the need for writers to have exclusive access. It also summarizes the first readers-writers problem and solution. For the dining philosophers problem, it outlines the scenario and challenges of multiple philosophers needing to simultaneously access shared chopsticks. Finally, for the sleeping barber problem, it describes the scenario of customers arriving for haircuts when the barber is sleeping and the need for synchronization between the barber and customers.

Uploaded by

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

Classical Syncronization Issues

This document discusses three classic concurrency problems: the readers-writers problem, dining philosophers problem, and sleeping barber problem. For the readers-writers problem, it describes the challenges of having concurrent processes accessing shared data and the need for writers to have exclusive access. It also summarizes the first readers-writers problem and solution. For the dining philosophers problem, it outlines the scenario and challenges of multiple philosophers needing to simultaneously access shared chopsticks. Finally, for the sleeping barber problem, it describes the scenario of customers arriving for haircuts when the barber is sleeping and the need for synchronization between the barber and customers.

Uploaded by

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

Sri Lanka Institute Of Information Technology

Operating Systems
Reading Assignment
3rd Year
1st Semester

Submitted by – Guruge D.L.S.

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.

 database and concurrent processes


 Reader threads: those that only read
 Writer threads: those that read and write (update)
 multiple Readers –no problem
 multiple Writers or Readers and Writers –possibility of lots of chaos

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.

There are variations of Readers-Writers problem.

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.

Semaphore mutex, wrt;

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);

wait(mutex); readcount ++;

……… if(readcount==1)

Remove an item from buffer to nextc wait(wrt);

………. signal(mutex);

Signal(mutex); ………

Signal(empty); Reading is performed.

…………… ……….

Consume the item in nextc wait(mutex);

………. readcount --;

}while(1); if(readcount==0)

signal(wrt);

signal(mutex);
2. Dining Philosophers Problem

 5 philosophers –think and eat


 circular table
 5 chairs
 1 bowl of rice in the middle
 5 single chopsticks
 when 1 philosopher thinks, she does not interact with her colleagues
 once in a while, the philosopher get hungry and tries to use the 2 chopsticks that are the closest to
her – but so do others
 a philosopher may pick up only 1 chopstick at a time ( as long as another philosophers has not
picked it up)
 When a philosopher has both chopsticks, she eats until she releases her chopsticks
 When she has finished eating, she puts down her chopsticks and begins thinking

The Dining Philosophers problem is considered a classic synchronization (concurrency control)


problem –and we shall use it to represent the need to allocate several resources among several
processes in a deadlock-free and starvation-free manner.

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.

Thus shared data are,

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.

 Allow at most 4 Philosophers to be sitting simultaneously at the table


 Allow a Philosopher to pick up her chopsticks only if both chopsticks are available (she must
pick them up in her critical section.
 Use an asymmetric solution e.g. odd Philosophers pick up left chopstick first and even
numbered Philosophers pick up right chop stick first and even numbered Philosophers pick
up right chopstick first

Each of the above solutions prevents deadlock by placing restrictions on the Philosopher.

Structure of the philosopher

Do{

Wait( chopstick [i]);

Wait( chopstick [i + 1 % 5 ]);

……………

Eat

……………..

Signal (chopstick [i]);

Signal( chopstick [i + 1 % 5 ]);

………..

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.

#define CHAIRS 5 /* # chairs for waiting customers */

typedef int semaphore, /* use your imagination */

semaphore customers = 0, /* # of customers waiting for service */

semaphore barbers = 0, /* # of barbers waiting for customers */

semaphore mutex = 0, /* for mutual exclusion */

int waiting = 0, /* customer are waiting (not being cut) */

void barber(void) {

while (TRUE) {

down(&customers); /* go to sleep if # of customers is 0 */

down(&mutex); /* acquire access to "waiting' */

waiting = waiting - 1; /* decrement count of waiting customers */

up(&barbers); /* one barber is now ready to cut hair */

up(&mutex); /* release 'waiting' */

cut_hair(); /* cut hair (outside critical region */

}}
void customer(void) {

down(&mutex); /* enter critical region */

if (waiting < CHAIRS) { /* if there are no free chairs, leave */

waiting = waiting + 1; /* increment count of waiting customers */

up(&customers); /* wake up barber if necessary */

up(&mutex); /* release access to 'waiting' */

down(&barbers); /* go to sleep if # of free barbers is 0 */

get_haircut(); /* be seated and be served */

} else {

up(&mutex); /* shop is full; do not wait */ }

}
References

[1] Operating Systems Concepts: Sixth Edition, Silberschatz, Galvin, Gagne.

[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

You might also like