PROCESS SYNCHRONIZATION AND DEADLOCK
MANAGEMENT
Revision of unit2
• Process
• Process management
• Attributes of a process
• Hoe does process look like int the memory
• States of a process
• PCB
• Scheduling
Independent and Cooperating Processes
• Independent Process: Independent Processes are those processes whose task is not dependent
on any other processes.
• Cooperating Process: Cooperating Processes are those processes that depend on other
processes or processes. They work together to achieve a common task in an operating system.
These processes interact with each other by sharing the resources such as CPU, memory, and I/O
devices to complete the task.
Semaphores
•Semaphore is simply a variable that is non-negative and
shared between threads.
•A semaphore is a signaling mechanism, and a thread that is
waiting on a semaphore can be signaled by another thread.
It uses two atomic operations, 1)wait, and 2) signal for the
process synchronization.
Characteristic of Semaphore
•It is a mechanism that can be used to provide
synchronization of tasks.
•It is a low-level synchronization mechanism.
•Semaphore will always hold a non-negative integer value.
•Semaphore can be implemented using test operations and
interrupts, which should be executed using file descriptors.
Example of Semaphore
Shared var mutex:
Process I , semaphore =0
begin
.
.
P(mutex); p =Wait
execute CS; ===release CS
Semaphore = 1
V(mutex); V= signal
.
.
End;
Example of Semaphore
Some point regarding P and V operation
•P operation is also called wait, sleep or down operation
and V operation is also called signal, wake-up or up
operation.
•Both operations are atomic and semaphore(s) is always
initialized to one.
•Here atomic means that variable on which read, modify
and update happens at the same time/moment with no
pre-emption i.e. in between read, modify and update no
other operation is performed that may change the variable.
•A critical section is surrounded by both operations to
implement process synchronization.See below image.critical
section of Process P is in between P and V operation.
Some point regarding P and V operation
Some point regarding P and V operation
•Let there be two processes P1 and P2 and a semaphore s is
initialized as 1.
•Now if suppose P1 enters in its critical section then the
value of semaphore s becomes 0.
•Now if P2 wants to enter its critical section then it will wait
until s > 0, this can only happen when P1 finishes its critical
section and calls V operation on semaphore s.
•This way mutual exclusion is achieved. Look at the below
image for details which is Binary semaphore.
Other Synchronization Problems
1. Dining Philosophers Problem
2. Producer – Consumer Problem
3. Readers – Writers Problem
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.
Dining Philosophers Problem
• Let's understand the Dining Philosophers Problem with
the below code, we have used fig 1 as a reference to
make you understand the problem exactly.
• The five Philosophers are represented as P0, P1, P2, P3,
and P4 and five chopsticks by C0, C1, C2, C3, and C4.
Dining Philosophers Problem
Dining Philosophers Problem
Dining Philosophers Problem
Void Philosopher
{
while(1)
{
take_chopstick[i];
take_chopstick[ (i+1) % 5] ;
. .
. EATING THE NOODLE
.
put_chopstick[i] );
put_chopstick[ (i+1) % 5] ;
.
. THINKING
}
Dining Philosophers Problem
•Suppose Philosopher P0 wants to eat, it will enter in
Philosopher() function, and execute take_chopstick[i]; by
doing this it holds C0 chopstick after that it
execute take_chopstick[ (i+1) % 5]; by doing this it holds C1
chopstick( since i =0, therefore (0 + 1) % 5 = 1)
•Similarly suppose now Philosopher P1 wants to eat, it will
enter in Philosopher() function, and
execute take_chopstick[i]; by doing this it holds C1
chopstick after that it execute take_chopstick[ (i+1) %
5]; by doing this it holds C2 chopstick( since i =1, therefore
(1 + 1) % 5 = 2)
•But Practically Chopstick C1 is not available as it has
already been taken by philosopher P0, hence the above
code generates problems and produces race condition.
Dining Philosophers Problem - Solution
1. wait( S )
{
while( S <= 0) ;
S--;
}
2. signal( S )
{
S++;
}
Producer Consumer Problem
•The Producer-Consumer problem is a classic problem this is
used for multi-process synchronization i.e. synchronization
between more than one processes.
•In the producer-consumer problem, there is one Producer
that is producing something and there is one Consumer that
is consuming the products produced by the Producer. The
producers and consumers share the same memory buffer
that is of fixed-size.
•The job of the Producer is to generate the data, put it into
the buffer, and again start generating data. While the job of
the Consumer is to consume the data from the buffer.
What's the problem here?
The following are the problems that might occur in the
Producer-Consumer:
1.The producer should produce data only when the buffer is
not full. If the buffer is full, then the producer shouldn't be
allowed to put any data into the buffer.
2.The consumer should consume data only when the buffer
is not empty. If the buffer is empty, then the consumer
shouldn't be allowed to take any data from the buffer.
3.The producer and consumer should not access the buffer
at the same time.
Pseudo code for producer
do {
.
. PRODUCE ITEM
.
wait(empty);
wait(mutex);
.
. PUT ITEM IN BUFFER
.
signal(mutex);
signal(full);
} while(1);
Mutex , Empty , full
are semaphores
Initialized to
1
Initialized to n ( count
empty space)
Initialized
to 0,
Counts full
space
Pseudo code for producer
•In the above code, mutex, empty and full are semaphores.
• Here mutex is initialized to 1, empty is initialized to n
(maximum size of the buffer) and full is initialized to 0.
•The mutex semaphore ensures mutual exclusion. The
empty and full semaphores count the number of empty and
full spaces in the buffer.
•After the item is produced, wait operation is carried out on
empty. This indicates that the empty space in the buffer has
decreased by 1. Then wait operation is carried out on mutex
so that consumer process cannot interfere.
•After the item is put in the buffer, signal operation is
carried out on mutex and full. The former indicates that
consumer process can now act and the latter shows that the
buffer is full by 1
Pseudo code for Consumer
do {
wait(full);
wait(mutex);
. .
. REMOVE ITEM FROM BUFFER
.
signal(mutex);
signal(empty);
.
. CONSUME ITEM
.
} while(1);
Pseudo code for Consumer
•The wait operation is carried out on full. This indicates that
items in the buffer have decreased by 1. Then wait
operation is carried out on mutex so that producer process
cannot interfere.
•Then the item is removed from buffer. After that, signal
operation is carried out on mutex and empty. The former
indicates that consumer process can now act and the latter
shows that the empty space in the buffer has increased by
1.
Process Deadlocks - Introduction to
Deadlock
•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.
Process Deadlocks - Introduction to
Deadlock
•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.
Process Deadlocks - Introduction to
Deadlock
Process Deadlocks - Introduction to
Deadlock
•In the above diagram, the process 1 has resource 1 and
needs to acquire resource 2.
•Similarly process 2 has resource 2 and needs to acquire
resource 1.
•Process 1 and process 2 are in deadlock as each of them
needs the other’s resource to complete their execution but
neither of them is willing to relinquish their resources.
Deadlocks vs Starvation
•Starvation occurs if a process is indefinitely postponed.
• This may happen if the process requires a resource for
execution that it is never allotted or if the process is never
provided the processor for some reason.
Deadlocks vs Starvation
Some of the common causes of starvation are as follows −
1.If a process is never provided the resources it requires for
execution because of faulty resource allocation decisions, then
starvation can occur.
2.A lower priority process may wait forever if higher priority
processes constantly monopolize the processor.
3.Starvation may occur if there are not enough resources to
provide to every process as required.
4.If random selection of processes is used then a process may
wait for a long time because of non-selection.
Fundamental Causes of Deadlocks
1. Mutual Exclusion: There should be a resource
that can only be held by one process at a time.
In the diagram below, there is a single
instance of Resource 1 and it is held by
Process 1 only.
Fundamental Causes of Deadlocks
2. Hold and Wait:
• A process can hold multiple resources and still request
more resources from other processes which are holding
them.
• In the diagram given below, Process 2 holds Resource 2
and Resource 3 and is requesting the Resource 1 which
is held by Process 1.
Fundamental Causes of Deadlocks
3. No Preemption
• A resource cannot be preempted from a process by
force. A process can only release a resource voluntarily.
• In the diagram below, Process 2 cannot preempt
Resource 1 from Process 1. It will only be released
when Process 1 relinquishes it voluntarily after its
execution is complete.
Fundamental Causes of Deadlocks
4. Circular Wait
• A process is waiting for the resource held by the second
process, which is waiting for the resource held by the
third process and so on, till the last process is waiting
for a resource held by the first process.
• This forms a circular chain. For example: Process 1 is
allocated Resource2 and it is requesting Resource 1.
Similarly, Process 2 is allocated Resource 1 and it is
requesting Resource 2. This forms a circular wait loop.
Deadlock Handling Strategies
1. Deadlock Prevention
2. Deadlock Detection
3. Deadlock Avoidance
Deadlock Handling Strategies
Deadlock Prevention
•It is very important to prevent a deadlock before it can
occur. So, the system checks each transaction before it is
executed to make sure it does not lead to deadlock.
• If there is even a slight chance that a transaction may lead
to deadlock in the future, it is never allowed to execute.
Deadlock Prevention
.
Deadlock Prevention
We can prevent a Deadlock by eliminating any of the
above four conditions.
Eliminate Mutual Exclusion: It is not possible to dis-
satisfy the mutual exclusion because some resources,
such as the tape drive and printer, are inherently non-
shareable.
Deadlock Prevention
.
Eliminate Hold and wait: Allocate all required resources
to the process before the start of its execution, this way
hold and wait condition is eliminated but it will lead to
low device utilization.
Deadlock Prevention
.
Eliminate Hold and wait: Allocate all required resources
to the process before the start of its execution, this way
hold and wait condition is eliminated but it will lead to
low device utilization.
Deadlock Prevention
.
Eliminate No Preemption : Preempt resources from the
process when resources are required by other high-priority
processes.
Eliminate Circular Wait : Each resource will be assigned a
numerical number. A process can request the resources to
increase/decrease. order of numbering. For Example, if the P1
process is allocated R5 resources, now next time if P1 asks for
R4, R3 lesser than R5 such a request will not be granted, only a
request for resources more than R5 will be granted.
Detection and Recovery: Another approach to dealing with
deadlocks is to detect and recover from them when they occur.
This can involve killing one or more of the processes involved
in the deadlock or releasing some of the resources they hold.
Deadlock Handling Strategies
Deadlock Detection:
•A deadlock can be detected by a resource scheduler as it
keeps track of all the resources that are allocated to
different processes.
•After a deadlock is detected, it can be resolved using the
following methods −
•All the processes that are involved in the deadlock are
terminated. This is not a good approach as all the progress
made by the processes is destroyed.
•Resources can be preempted from some processes and
given to others till the deadlock is resolved.
Wait For Graph Deadlock Detection in
Distributed System
• Deadlocks are a fundamental problem in distributed
systems.
• A process may request resources in any order and
a process can request resources while holding
others.
• A Deadlock is a situation where a set of processes
are blocked as each process in a Distributed
system is holding some resources and that acquired
resources are needed by some other processes.
Wait For Graph Deadlock Detection in
Distributed System
Example:
If there are three processes p1,p2 and p1 are
acquiring r1 resource and that r1 is needed by p2
which is acquiring another resource r2 and that is
needed by p1. Here cycle occurs. It is called a
deadlock.
Wait For Graph Deadlock Detection in
Distributed System
Here in the above graph we found a cycle from P1 to
P2 and again to P1. So we can say that the system is
in a deadlock state.
The Problem of deadlocks has been generally studied
in distributed systems under the following models :
•The system has only reusable resources.
•Processes are allowed only exclusive access to
resources.
•There is only one copy of each resource.
Wait For Graph Deadlock Detection in
Distributed System
Wait-for-Graph Algorithm: It is a variant of the
Resource Allocation graph. In this algorithm, we
only have processes as vertices in the graph. If the
Wait-for-Graph contains a cycle then we can say the
system is in a Deadlock state. Now we will discuss
how the Resource Allocation graph will be converted
into Wait-for-Graph in an Algorithmic Approach. We
need to remove resources while converting from
Resource Allocation Graph to Wait-for-Graph.
•Resource Allocation Graph: Contains Processes
and Resources.
•Wait-for-Graph: Contains only Processes after
removing the Resources while conversion from
Resource Allocation Graph.
Algorithm:
Step 1: Take the first process (Pi) from the resource
allocation graph and check the path in which it is
acquiring resource (Ri), and start a wait-for-graph with
that particular process.
Step 2: Make a path for the Wait-for-Graph in which
there will be no Resource included from the current
process (Pi) to next process (Pj), from that next
process (Pj) find a resource (Rj) that will be acquired by
next Process (Pk) which is released from Process (Pj).
Step 3: Repeat Step 2 for all the processes.
Step 4: After completion of all processes, if we find a
closed-loop cycle then the system is in a deadlock
state, and deadlock is detected.
Algorithm:
Now we will see the working of this Algorithm with an
Example.
Consider a Resource Allocation Graph with 4
Processes P1, P2, P3, P4, and 4 Resources R1, R2,
R3, R4. Find if there is a deadlock in the Graph using
the Wait for Graph-based deadlock detection
algorithm.
Algorithm:
Algorithm:
Step 1:
First take Process P1 which is waiting for Resource R1,
resource R1 is acquired by Process P2, Start a Wait-for-
Graph for the above Resource Allocation Graph.
Algorithm:
Step 2: Now we can observe that there is a path from
P1 to P2 as P1 is waiting for R1 which is been acquired
by P2. Now the Graph would be after removing
resource R1 looks like.
Algorithm:
Step 3: From P2 we can observe a path from P2 to
P3 as P2 is waiting for R4 which is acquired by P3.
So make a path from P2 to P3 after removing
resource R4 looks like.
Algorithm:
.
Step 4: From P3 we find a path to P4 as it is waiting for
P3 which is acquired by P4. After removing R3 the
graph looks like this.
Algorithm:
.
Step 5: Here we can find Process P4 is waiting for R2
which is acquired by P1. So finally the Wait-for-Graph is
as follows:
Algorithm:
.
Step 6:
• Finally In this Graph, we found a cycle as the
Process P4 again came back to the Process P1
which is the starting point (i.e., it’s a closed-loop).
• So, According to the Algorithm if we found a closed
loop, then the system is in a deadlock state. So here
we can say the system is in a deadlock state.
Deadlock Handling Strategies
Deadlock Avoidance
•It is better to avoid a deadlock rather than take measures
after the deadlock has occurred.
•The wait for graph can be used for deadlock avoidance.
• This is however only useful for smaller databases as it can
get quite complex in larger databases.
Bankers Algorithm-Deadlock avoidance
• The banker’s algorithm is a resource allocation and deadlock
avoidance algorithm that tests for safety by simulating the allocation
for the predetermined maximum possible amounts of all resources,
then makes an “s-state” check to test for possible activities, before
deciding whether allocation should be allowed to continue.
• Available
It is a 1-d array of size ‘m’ indicating the number of available
resources of each type.
• Max
It is a 2-d array of size ‘n*m’ that defines the maximum
demand of each process in a system.
• Allocation
It is a 2-d array of size ‘n*m’ that defines the number of
resources of each type currently allocated to each process.
• Need
It is a 2-d array of size ‘n*m’ that indicates the remaining
resource need of each process.
Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ]
Safety Algorithm
The algorithm for finding out whether or not a system is in a safe state
can be described as follows:
1) Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
2) Find an i such that both
a) Finish[i] = false
b) Needi <= Work
if no such i exists goto step (4)
3) Work = Work + Allocation[i]
Finish[i] = true
goto step (2)
4) if Finish [i] = true for all i
then the system is in a safe state
Considering a system with five processes P0 through P4 and three resources of
type A, B, C. Resource type A has 10 instances, B has 5 instances and type C has 7
instances. Suppose at time t0 following snapshot of the system has been taken:
• Need [i, j] = Max [i, j] – Allocation [i, j]
So, the content of Need Matrix is:
Available 3 2 2

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  • 1.
    PROCESS SYNCHRONIZATION ANDDEADLOCK MANAGEMENT
  • 2.
    Revision of unit2 •Process • Process management • Attributes of a process • Hoe does process look like int the memory • States of a process • PCB • Scheduling
  • 3.
    Independent and CooperatingProcesses • Independent Process: Independent Processes are those processes whose task is not dependent on any other processes. • Cooperating Process: Cooperating Processes are those processes that depend on other processes or processes. They work together to achieve a common task in an operating system. These processes interact with each other by sharing the resources such as CPU, memory, and I/O devices to complete the task.
  • 4.
    Semaphores •Semaphore is simplya variable that is non-negative and shared between threads. •A semaphore is a signaling mechanism, and a thread that is waiting on a semaphore can be signaled by another thread. It uses two atomic operations, 1)wait, and 2) signal for the process synchronization.
  • 5.
    Characteristic of Semaphore •Itis a mechanism that can be used to provide synchronization of tasks. •It is a low-level synchronization mechanism. •Semaphore will always hold a non-negative integer value. •Semaphore can be implemented using test operations and interrupts, which should be executed using file descriptors.
  • 6.
    Example of Semaphore Sharedvar mutex: Process I , semaphore =0 begin . . P(mutex); p =Wait execute CS; ===release CS Semaphore = 1 V(mutex); V= signal . . End;
  • 7.
  • 8.
    Some point regardingP and V operation •P operation is also called wait, sleep or down operation and V operation is also called signal, wake-up or up operation. •Both operations are atomic and semaphore(s) is always initialized to one. •Here atomic means that variable on which read, modify and update happens at the same time/moment with no pre-emption i.e. in between read, modify and update no other operation is performed that may change the variable. •A critical section is surrounded by both operations to implement process synchronization.See below image.critical section of Process P is in between P and V operation.
  • 9.
    Some point regardingP and V operation
  • 10.
    Some point regardingP and V operation •Let there be two processes P1 and P2 and a semaphore s is initialized as 1. •Now if suppose P1 enters in its critical section then the value of semaphore s becomes 0. •Now if P2 wants to enter its critical section then it will wait until s > 0, this can only happen when P1 finishes its critical section and calls V operation on semaphore s. •This way mutual exclusion is achieved. Look at the below image for details which is Binary semaphore.
  • 11.
    Other Synchronization Problems 1.Dining Philosophers Problem 2. Producer – Consumer Problem 3. Readers – Writers Problem
  • 12.
    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.
  • 13.
    Dining Philosophers Problem •Let's understand the Dining Philosophers Problem with the below code, we have used fig 1 as a reference to make you understand the problem exactly. • The five Philosophers are represented as P0, P1, P2, P3, and P4 and five chopsticks by C0, C1, C2, C3, and C4.
  • 14.
  • 17.
  • 20.
    Dining Philosophers Problem VoidPhilosopher { while(1) { take_chopstick[i]; take_chopstick[ (i+1) % 5] ; . . . EATING THE NOODLE . put_chopstick[i] ); put_chopstick[ (i+1) % 5] ; . . THINKING }
  • 21.
    Dining Philosophers Problem •SupposePhilosopher P0 wants to eat, it will enter in Philosopher() function, and execute take_chopstick[i]; by doing this it holds C0 chopstick after that it execute take_chopstick[ (i+1) % 5]; by doing this it holds C1 chopstick( since i =0, therefore (0 + 1) % 5 = 1) •Similarly suppose now Philosopher P1 wants to eat, it will enter in Philosopher() function, and execute take_chopstick[i]; by doing this it holds C1 chopstick after that it execute take_chopstick[ (i+1) % 5]; by doing this it holds C2 chopstick( since i =1, therefore (1 + 1) % 5 = 2) •But Practically Chopstick C1 is not available as it has already been taken by philosopher P0, hence the above code generates problems and produces race condition.
  • 22.
    Dining Philosophers Problem- Solution 1. wait( S ) { while( S <= 0) ; S--; } 2. signal( S ) { S++; }
  • 24.
    Producer Consumer Problem •TheProducer-Consumer problem is a classic problem this is used for multi-process synchronization i.e. synchronization between more than one processes. •In the producer-consumer problem, there is one Producer that is producing something and there is one Consumer that is consuming the products produced by the Producer. The producers and consumers share the same memory buffer that is of fixed-size. •The job of the Producer is to generate the data, put it into the buffer, and again start generating data. While the job of the Consumer is to consume the data from the buffer.
  • 25.
    What's the problemhere? The following are the problems that might occur in the Producer-Consumer: 1.The producer should produce data only when the buffer is not full. If the buffer is full, then the producer shouldn't be allowed to put any data into the buffer. 2.The consumer should consume data only when the buffer is not empty. If the buffer is empty, then the consumer shouldn't be allowed to take any data from the buffer. 3.The producer and consumer should not access the buffer at the same time.
  • 26.
    Pseudo code forproducer do { . . PRODUCE ITEM . wait(empty); wait(mutex); . . PUT ITEM IN BUFFER . signal(mutex); signal(full); } while(1); Mutex , Empty , full are semaphores Initialized to 1 Initialized to n ( count empty space) Initialized to 0, Counts full space
  • 27.
    Pseudo code forproducer •In the above code, mutex, empty and full are semaphores. • Here mutex is initialized to 1, empty is initialized to n (maximum size of the buffer) and full is initialized to 0. •The mutex semaphore ensures mutual exclusion. The empty and full semaphores count the number of empty and full spaces in the buffer. •After the item is produced, wait operation is carried out on empty. This indicates that the empty space in the buffer has decreased by 1. Then wait operation is carried out on mutex so that consumer process cannot interfere. •After the item is put in the buffer, signal operation is carried out on mutex and full. The former indicates that consumer process can now act and the latter shows that the buffer is full by 1
  • 28.
    Pseudo code forConsumer do { wait(full); wait(mutex); . . . REMOVE ITEM FROM BUFFER . signal(mutex); signal(empty); . . CONSUME ITEM . } while(1);
  • 29.
    Pseudo code forConsumer •The wait operation is carried out on full. This indicates that items in the buffer have decreased by 1. Then wait operation is carried out on mutex so that producer process cannot interfere. •Then the item is removed from buffer. After that, signal operation is carried out on mutex and empty. The former indicates that consumer process can now act and the latter shows that the empty space in the buffer has increased by 1.
  • 30.
    Process Deadlocks -Introduction to Deadlock •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.
  • 31.
    Process Deadlocks -Introduction to Deadlock •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.
  • 32.
    Process Deadlocks -Introduction to Deadlock
  • 33.
    Process Deadlocks -Introduction to Deadlock •In the above diagram, the process 1 has resource 1 and needs to acquire resource 2. •Similarly process 2 has resource 2 and needs to acquire resource 1. •Process 1 and process 2 are in deadlock as each of them needs the other’s resource to complete their execution but neither of them is willing to relinquish their resources.
  • 34.
    Deadlocks vs Starvation •Starvationoccurs if a process is indefinitely postponed. • This may happen if the process requires a resource for execution that it is never allotted or if the process is never provided the processor for some reason.
  • 35.
    Deadlocks vs Starvation Someof the common causes of starvation are as follows − 1.If a process is never provided the resources it requires for execution because of faulty resource allocation decisions, then starvation can occur. 2.A lower priority process may wait forever if higher priority processes constantly monopolize the processor. 3.Starvation may occur if there are not enough resources to provide to every process as required. 4.If random selection of processes is used then a process may wait for a long time because of non-selection.
  • 37.
    Fundamental Causes ofDeadlocks 1. Mutual Exclusion: There should be a resource that can only be held by one process at a time. In the diagram below, there is a single instance of Resource 1 and it is held by Process 1 only.
  • 38.
    Fundamental Causes ofDeadlocks 2. Hold and Wait: • A process can hold multiple resources and still request more resources from other processes which are holding them. • In the diagram given below, Process 2 holds Resource 2 and Resource 3 and is requesting the Resource 1 which is held by Process 1.
  • 39.
    Fundamental Causes ofDeadlocks 3. No Preemption • A resource cannot be preempted from a process by force. A process can only release a resource voluntarily. • In the diagram below, Process 2 cannot preempt Resource 1 from Process 1. It will only be released when Process 1 relinquishes it voluntarily after its execution is complete.
  • 40.
    Fundamental Causes ofDeadlocks 4. Circular Wait • A process is waiting for the resource held by the second process, which is waiting for the resource held by the third process and so on, till the last process is waiting for a resource held by the first process. • This forms a circular chain. For example: Process 1 is allocated Resource2 and it is requesting Resource 1. Similarly, Process 2 is allocated Resource 1 and it is requesting Resource 2. This forms a circular wait loop.
  • 41.
    Deadlock Handling Strategies 1.Deadlock Prevention 2. Deadlock Detection 3. Deadlock Avoidance
  • 42.
    Deadlock Handling Strategies DeadlockPrevention •It is very important to prevent a deadlock before it can occur. So, the system checks each transaction before it is executed to make sure it does not lead to deadlock. • If there is even a slight chance that a transaction may lead to deadlock in the future, it is never allowed to execute.
  • 43.
    Deadlock Prevention . Deadlock Prevention Wecan prevent a Deadlock by eliminating any of the above four conditions. Eliminate Mutual Exclusion: It is not possible to dis- satisfy the mutual exclusion because some resources, such as the tape drive and printer, are inherently non- shareable.
  • 44.
    Deadlock Prevention . Eliminate Holdand wait: Allocate all required resources to the process before the start of its execution, this way hold and wait condition is eliminated but it will lead to low device utilization.
  • 45.
    Deadlock Prevention . Eliminate Holdand wait: Allocate all required resources to the process before the start of its execution, this way hold and wait condition is eliminated but it will lead to low device utilization.
  • 46.
    Deadlock Prevention . Eliminate NoPreemption : Preempt resources from the process when resources are required by other high-priority processes. Eliminate Circular Wait : Each resource will be assigned a numerical number. A process can request the resources to increase/decrease. order of numbering. For Example, if the P1 process is allocated R5 resources, now next time if P1 asks for R4, R3 lesser than R5 such a request will not be granted, only a request for resources more than R5 will be granted. Detection and Recovery: Another approach to dealing with deadlocks is to detect and recover from them when they occur. This can involve killing one or more of the processes involved in the deadlock or releasing some of the resources they hold.
  • 47.
    Deadlock Handling Strategies DeadlockDetection: •A deadlock can be detected by a resource scheduler as it keeps track of all the resources that are allocated to different processes. •After a deadlock is detected, it can be resolved using the following methods − •All the processes that are involved in the deadlock are terminated. This is not a good approach as all the progress made by the processes is destroyed. •Resources can be preempted from some processes and given to others till the deadlock is resolved.
  • 48.
    Wait For GraphDeadlock Detection in Distributed System • Deadlocks are a fundamental problem in distributed systems. • A process may request resources in any order and a process can request resources while holding others. • A Deadlock is a situation where a set of processes are blocked as each process in a Distributed system is holding some resources and that acquired resources are needed by some other processes.
  • 49.
    Wait For GraphDeadlock Detection in Distributed System Example: If there are three processes p1,p2 and p1 are acquiring r1 resource and that r1 is needed by p2 which is acquiring another resource r2 and that is needed by p1. Here cycle occurs. It is called a deadlock.
  • 50.
    Wait For GraphDeadlock Detection in Distributed System Here in the above graph we found a cycle from P1 to P2 and again to P1. So we can say that the system is in a deadlock state. The Problem of deadlocks has been generally studied in distributed systems under the following models : •The system has only reusable resources. •Processes are allowed only exclusive access to resources. •There is only one copy of each resource.
  • 51.
    Wait For GraphDeadlock Detection in Distributed System Wait-for-Graph Algorithm: It is a variant of the Resource Allocation graph. In this algorithm, we only have processes as vertices in the graph. If the Wait-for-Graph contains a cycle then we can say the system is in a Deadlock state. Now we will discuss how the Resource Allocation graph will be converted into Wait-for-Graph in an Algorithmic Approach. We need to remove resources while converting from Resource Allocation Graph to Wait-for-Graph. •Resource Allocation Graph: Contains Processes and Resources. •Wait-for-Graph: Contains only Processes after removing the Resources while conversion from Resource Allocation Graph.
  • 52.
    Algorithm: Step 1: Takethe first process (Pi) from the resource allocation graph and check the path in which it is acquiring resource (Ri), and start a wait-for-graph with that particular process. Step 2: Make a path for the Wait-for-Graph in which there will be no Resource included from the current process (Pi) to next process (Pj), from that next process (Pj) find a resource (Rj) that will be acquired by next Process (Pk) which is released from Process (Pj). Step 3: Repeat Step 2 for all the processes. Step 4: After completion of all processes, if we find a closed-loop cycle then the system is in a deadlock state, and deadlock is detected.
  • 53.
    Algorithm: Now we willsee the working of this Algorithm with an Example. Consider a Resource Allocation Graph with 4 Processes P1, P2, P3, P4, and 4 Resources R1, R2, R3, R4. Find if there is a deadlock in the Graph using the Wait for Graph-based deadlock detection algorithm.
  • 54.
  • 55.
    Algorithm: Step 1: First takeProcess P1 which is waiting for Resource R1, resource R1 is acquired by Process P2, Start a Wait-for- Graph for the above Resource Allocation Graph.
  • 56.
    Algorithm: Step 2: Nowwe can observe that there is a path from P1 to P2 as P1 is waiting for R1 which is been acquired by P2. Now the Graph would be after removing resource R1 looks like.
  • 57.
    Algorithm: Step 3: FromP2 we can observe a path from P2 to P3 as P2 is waiting for R4 which is acquired by P3. So make a path from P2 to P3 after removing resource R4 looks like.
  • 58.
    Algorithm: . Step 4: FromP3 we find a path to P4 as it is waiting for P3 which is acquired by P4. After removing R3 the graph looks like this.
  • 59.
    Algorithm: . Step 5: Herewe can find Process P4 is waiting for R2 which is acquired by P1. So finally the Wait-for-Graph is as follows:
  • 60.
    Algorithm: . Step 6: • FinallyIn this Graph, we found a cycle as the Process P4 again came back to the Process P1 which is the starting point (i.e., it’s a closed-loop). • So, According to the Algorithm if we found a closed loop, then the system is in a deadlock state. So here we can say the system is in a deadlock state.
  • 61.
    Deadlock Handling Strategies DeadlockAvoidance •It is better to avoid a deadlock rather than take measures after the deadlock has occurred. •The wait for graph can be used for deadlock avoidance. • This is however only useful for smaller databases as it can get quite complex in larger databases.
  • 62.
    Bankers Algorithm-Deadlock avoidance •The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating the allocation for the predetermined maximum possible amounts of all resources, then makes an “s-state” check to test for possible activities, before deciding whether allocation should be allowed to continue.
  • 63.
    • Available It isa 1-d array of size ‘m’ indicating the number of available resources of each type. • Max It is a 2-d array of size ‘n*m’ that defines the maximum demand of each process in a system. • Allocation It is a 2-d array of size ‘n*m’ that defines the number of resources of each type currently allocated to each process. • Need It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each process. Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ]
  • 64.
    Safety Algorithm The algorithmfor finding out whether or not a system is in a safe state can be described as follows: 1) Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively. Initialize: Work = Available Finish[i] = false; for i=1, 2, 3, 4….n 2) Find an i such that both a) Finish[i] = false b) Needi <= Work if no such i exists goto step (4) 3) Work = Work + Allocation[i] Finish[i] = true goto step (2) 4) if Finish [i] = true for all i then the system is in a safe state
  • 65.
    Considering a systemwith five processes P0 through P4 and three resources of type A, B, C. Resource type A has 10 instances, B has 5 instances and type C has 7 instances. Suppose at time t0 following snapshot of the system has been taken:
  • 66.
    • Need [i,j] = Max [i, j] – Allocation [i, j] So, the content of Need Matrix is:
  • 68.