2. Classical problems of Synchronization
• There are a number of classical problems of synchronization as examples of a
large class of concurrency-control problems. semaphores are used as solution for
synchronization, since that is the traditional way to present such solutions.
• However, actual implementations of these solutions could use mutex locks in
place of binary semaphores.
• These problems are used for testing nearly every newly proposed synchronization
scheme. The following problems of synchronization are considered as classical
problems:
1. Bounded-buffer (or Producer-Consumer) Problem,
2. Dining-Philosophers Problem,
3. Readers and Writers Problem,
3. Bounded-buffer (or Producer-Consumer) Problem:
• Bounded Buffer problem is also called producer consumer problem. This problem
is generalized in terms of the Producer-Consumer problem.
• There is one Producer in the producer-consumer problem, Producer is producing
some items, whereas there is one Consumer that is consuming the items
produced by the Producer. The same memory buffer is shared by both producers
and consumers which is of fixed-size.
• The task of the Producer is to produce the item, put it into the memory buffer,
and again start producing items. Whereas the task of the Consumer is to
consume the item from the memory buffer.
• Solution to this problem is, creating two counting semaphores “full” and “empty”
to keep track of the current number of full and empty buffers respectively.
Producers produce a product and consumers consume the product, but both use
of one of the containers each time.
5. • The producer should produce data only when the buffer is not full. In case it is
found that the buffer is full, the producer is not allowed to store any data into the
memory buffer.
• Data can only be consumed by the consumer if and only if the memory buffer is
not empty. In case it is found that the buffer is empty, the consumer is not
allowed to use any data from the memory buffer.
• Accessing memory buffer should not be allowed to producer and consumer at the
same time.
6. • If the producer is attempting to add an item at the same moment the
consumer is removing one, the outcome depends on whether the
producer’s check for available space happens before or after the
consumer decrements the counter.
• A similar race condition arises if the queue is empty and both
functions are called. This race condition could be fixed by wrapping
the accesses to the counter with a mutex.
7. • Dining-Philosophers Problem:
• The Dining Philosopher Problem states that K philosophers seated
around a circular table with one chopstick between each pair of
philosophers. There is one chopstick between each philosopher. A
philosopher may eat if he can pickup the two chopsticks adjacent to
him. One chopstick may be picked up by any one of its adjacent
followers but not both. This problem involves the allocation of limited
resources to a group of processes in a deadlock-free and starvation-
free manner.
8. Readers-writers problem
Consider a situation where we have a file shared between many people.
If one of the person tries editing the file, no other person should be reading or
writing at the same time, otherwise changes will not be visible to him/her.
However if some person is reading the file, then others may read it at the same
time.
Precisely in OS we call this situation as the readers-writers problem
• Problem parameters:
• One set of data is shared among a number of processes
• Once a writer is ready, it performs its write. Only one writer may write at a time
• If a process is writing, no other process can read it
• If at least one reader is reading, no other process can write
• Readers may not write and only read
9. Solution
Case Process 1 Process 2
Allowed/Not
Allowed
Case 1 Writing Writing Not Allowed
Case 2 Writing Reading Not Allowed
Case 3 Reading Writing Not Allowed
Case 4 Reading Reading Allowed
There are four Types of cases could happen here.
Here priority means, no reader should wait if the share is currently opened for reading.
Three variables are used: mutex, wrt, readcnt to implement solution
10. Writer process:
• Writer requests the entry to critical section.
• If allowed i.e. wait() gives a true value, it enters and performs
the write. If not allowed, it keeps on waiting.
• It exits the critical section.
Reader process:
1.Reader requests the entry to critical section.
2.If allowed:
1. it increments the count of number of readers inside the critical
section. If this reader is the first reader entering, it locks the wrt
semaphore to restrict the entry of writers if any reader is inside.
2. It then, signals mutex as any other reader is allowed to enter
while others are already reading.
3. After performing reading, it exits the critical section. When
exiting, it checks if no more reader is inside, it signals the
semaphore “wrt” as now, writer can enter the critical section.
3.If not allowed, it keeps on waiting.
11. The Dining Philosopher Problem –
• The Dining Philosopher Problem states that K philosophers seated around a circular table
with one chopstick between each pair of philosophers. There is one chopstick between each
philosopher. A philosopher may eat if he can pick up the two chopsticks adjacent to him. One
chopstick may be picked up by any one of its adjacent followers but not both.
12. Semaphore Solution to Dining Philosopher –
Each philosopher is represented by the following pseudocode:
process P[i]
while true do
{
THINK;
PICKUP(CHOPSTICK[i], CHOPSTICK[i+1 mod 5]);
EAT;
PUTDOWN(CHOPSTICK[i], CHOPSTICK[i+1 mod 5])
}
There are three states of the philosopher: THINKING, HUNGRY, and EATING.
The dining philosopher demonstrates a large class of
concurrency control problems hence it's a classic
synchronization problem.
13. 1. Suppose Philosopher P0 wants to eat, it will enter in Philosopher() function, and execute Wait( take_chopstickC[i] ); by doing
this it holds C0 chopstick and reduces semaphore C0 to 0, after that it execute Wait( take_chopstickC[(i+1) % 5] ); by doing this
it holds C1 chopstick( since i =0, therefore (0 + 1) % 5 = 1) and reduces semaphore C1 to 0
2. Similarly, suppose now Philosopher P1 wants to eat, it will enter in Philosopher() function, and execute Wait(
take_chopstickC[i] ); by doing this it will try to hold C1 chopstick but will not be able to do that, since the value of semaphore
C1 has already been set to 0 by philosopher P0, therefore it will enter into an infinite loop because of which philosopher P1 will
not be able to pick chopstick C1 whereas if Philosopher P2 wants to eat, it will enter in Philosopher() function, and execute
Wait( take_chopstickC[i] ); by doing this it holds C2 chopstick and reduces semaphore C2 to 0, after that, it executes Wait(
take_chopstickC[(i+1) % 5] ); by doing this it holds C3 chopstick( since i =2, therefore (2 + 1) % 5 = 3) and reduces semaphore C3
to 0.
14. • The drawback of the above solution of the dining philosopher
problem
• From the above solution of the dining philosopher problem, we have
proved that no two neighboring 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.
15.
16. What is Deadlock in Operating System (OS)?
Every process needs some resources to complete its execution. However, the
resource is granted in a sequential order.
The process requests for some resource.
OS grant the resource if it is available otherwise let the process waits.
The process uses it and release on the completion.
A Deadlock is a situation where each of the computer process waits for a resource
which is being assigned to some another process. In this situation, none of the
process gets executed since the resource it needs, is held by some other process
which is also waiting for some other resource to be released.
17. Let us assume that there are three processes P1,
P2 and P3. There are three different resources R1,
R2 and R3. R1 is assigned to P1, R2 is assigned to
P2 and R3 is assigned to P3.
After some time, P1 demands for R1 which is being used by P2. P1
halts its execution since it can't complete without R2. P2 also
demands for R3 which is being used by P3. P2 also stops its
execution because it can't continue without R3. P3 also demands for
R1 which is being used by P1 therefore P3 also stops its execution.
In this scenario, a cycle is being formed among
the three processes.
None of the process is progressing and they are
all waiting.
The computer becomes unresponsive since all
the processes got blocked.
18. Difference between Starvation and Deadlock
Sr. Deadlock Starvation
1
Deadlock is a situation where no process
proceeds
Starvation is a situation where the low priority
process got blocked and the high priority
processes proceed.
2 Deadlock is an infinite waiting. Starvation is a long waiting but not infinite.
3 Every Deadlock is always a starvation. Every starvation need not be deadlock.
4
The requested resource is blocked by the
other process.
The requested resource is continuously be used
by the higher priority processes.
5
Deadlock happens when Mutual exclusion,
hold and wait, No preemption and circular
wait occurs simultaneously.
It occurs due to the uncontrolled priority and
resource management.
19. Necessary conditions for Deadlocks
Mutual Exclusion
A resource can only be shared in mutually exclusive manner. It implies, if two process cannot use
the same resource at the same time.
Hold and Wait
A process waits for some resources while holding another resource at the same time.
No preemption
The process which once scheduled will be executed till the completion. No other process can be
scheduled by the scheduler meanwhile.
Circular Wait
All the processes must be waiting for the resources in a cyclic manner so that the last process is
waiting for the resource which is being held by the first process.