Rajasthan Technical University, Kota: Subject-Operating System

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

Rajasthan Technical University, Kota

Department of Computer Science & Engineering

Subject- Operating System


Course Instructor: Vikas Jain

09/06/2021 1
Classical Problems of Synchronization
1. The Bounded Buffer Problem (Producer-Consumer Problem)

2. The Readers-Writers Problem

3. The Dining Philosophers Problem

4. Sleeping Barber Problem

09/06/2021 2
Producer and Consumer Solution Using Semaphore

• do{ do{
• //produce an item wait(full);
• wait(empty); wait(mutex);
wait(mutex); // remove item from
• //place in buffer buffer
signal(mutex);
• signal(mutex);
signal(empty);
signal(full);
// consumes item
• }while(true) }while(true)

09/06/2021 3
The Readers-Writers Problem

• It relates to a data set which is shared database such as such bank balances, or
airline/railway seats that is shared between more than one process at a time.

• There are two classes of users:


• Readers -- never modify database.
• Writers -- read and modify database

• In these applications, it should be allowed that:

• many readers at same time.


• only one writer at same time.
09/06/2021 4
 Constraints

• Readers can access database  when no writers.(R-W)


• Writers can access database when no readers.(W-R) 
• Writers can access database when no writers.(W-W)
• Two readers access the shared data simultaneously.(R-R)

09/06/2021 5
The solution

• If a process is performing some write operation, then no other process should be


allowed to perform the read or the write operation i.e. no other process should
be allowed to enter into the critical.

• If a process is performing some read operation only, then another process that is
demanding for reading operation should be allowed to read the file and get into
the critical section because the read operation doesn't change anything in the
file. So, more than one reads are allowed.

• But if a process is reading a file and another process is demanding for the write
operation, then it should not be allowed.

09/06/2021 6
The Dining Philosophers Problem
• The dining philosopher's problem is the classical problem of synchronization.

• The dining philosophers problem states that there are 5 philosophers sharing a
circular table and they eat and think alternatively.

• There is a bowl of rice for each of the philosophers and 5 chopsticks.

• A philosopher needs both their right and left chopstick to eat.

• A hungry philosopher may only eat if there are both chopsticks available.
Otherwise a philosopher puts down their chopstick and begin thinking again.

09/06/2021 7
09/06/2021 8
Solution of Dining Philosophers Problem

• A solution of the Dining Philosophers Problem is to use a semaphore


to represent a chopstick.

• A chopstick can be picked up by executing a wait operation on the


semaphore and released by executing a signal semaphore

09/06/2021 9
The drawback of the solution
• No two neighbouring philosophers can eat at the same point in time.

• The drawback of the above solution is that this solution can lead to a deadlock
condition.

• This situation happens if all the philosophers pick their left chopstick at the same
time, which leads to the condition of deadlock and none of the philosophers can
eat.

09/06/2021 10
Resolving the Deadlock
• All Philosophers reach for their left fork first, except one who first
reaches for his right fork.

09/06/2021 11

You might also like