Unit 3-1
Unit 3-1
1
Deadlock and Process Synchronization 0
• Deadlock
o Seven cases for deadlock
o Conditions for Deadlock
o Strategies for handling Deadlocks
5
o Starvation(Dining Philosophers Problem)
Unit
• Process Synchronization
3 o What is parallel Processing?
o Typical Multi processing configurations
o Process Synchronization Software-test and set, Wait &
Signal
5
o Semaphores
o Process Cooperation- Producers and consumers
Contents :
What is a deadlock?
Examples of Deadlock
Resource & Process
Types of Resources involved in a Deadlock
Seven cases of Deadlock
Deadlocks on File Requests
Deadlocks in Databases: Race Condition
Deadlocks in Dedicated Devices
Deadlocks in Multiple device allocation
Deadlocks in spooling
Deadlocks in Disk Sharing
Deadlocks in a Network
Conditions of a Deadlock
Deadlock Modeling : Resource allocation Graph
Strategies of Handling Deadlocks
Prevention
Avoidance
Detection
Recovery from Deadlock
Starvation: Dining Philosophers Problem
Process Management :
If there are many processes and very few resources in the system , and if the system if unable
to allocate these resources in a proper manner , it is a situation where there is no process
synchronization. This lack of process synchronization leads to two problems:
Deadlocks
Starvation
Deadlocks : A situation of standstill where Jobs have resources and are waiting for
other resources to continue their execution. These resources are being held by some
other processes who are also waiting for some resources to be made available
A process in a multiprogramming
system is said to be in a state of
deadlock if its waiting for a particular
event that will never occur.
Examples of deadlock
Process : Moving
cars & buses
Resource: Roads
Back
A Resource is something that can be used for a purpose.
Every device connected to a computer system is a hardware resource. Files and Programs
are the software resources.
A deadlock happens in operating system when two or more processes need some
resource to complete their execution that is held by the other process.
Process 1 and process 2 are in deadlock as each of them needs the resource held by the
other process to complete their execution, but neither of them is willing to release their
resources.
R1 Requests for
P1 P1 R1
Requests for
Non sharable Resources : Resources that cannot be shared by two processes at the same
time .
R2
P1 P2
R1
Non preemptable Resources : Resources that cannot be taken away from a process in-
between the execution to be given to another process.
If the resources are preepmtable. For example if
Assume a deadlock situation resource R1 can be preempted (taken away) from
process P1 and given to P2
R2
P1 P2
R1
Purchase Process (P1) requests for F1 without releasing F2. It does not get F1 as it is held by
P2.
Sales Process (P2) requests for F2 without releasing F1. It does not get F2 as F2 is held by P1
Record 2 T1
Record 5
User 1 locks T1 T3
T3
DB1 b. Table level locks : One table
T2
Consider a situation where there are two Process P1 and P2. These process need to update
two records R1 and R2. Sequence of events that happen are:
• P1 requests R1, gets R1 and locks it.
• P2 requests R2, gets R2 and locks it.
• P1 requests R2 which is locked by P2
• P2 requests R1 which is locked by P1.
Record 1
Record 1
Record 2
This situation of Deadlocks in Databases can be avoided.
How ?
Answer : We don’t use locks.
Record 1 (R1)
not locked
P1 writes back
R1 to database Here Process P1 writes first then
with new GPA P2 writes.
Jones The write operation of P1 is
GPA = 2 .5 P2 writes back R1
ADDR: Main St. to database overwritten by the write
with new ADDR operation of P2.
Here Only ADDR is updated
If we reverse the write operations :
Process P2 writes first then P2 writes.
The write operation of P2 is overwritten by the write operation of P1.
Record 1 (R1)
not locked
In both these situations either the changes made by P1 is reflected or the changes by
P2 are reflected
Both the processes don’t , complete their execution.
Which one will be complete (P1 or P2) depends on the order of execution of P1 and P2
This situation is known as RACE CONDITION where two processes compete but
which finishes finally is not known.
3. Deadlocks in Dedicated devices ( Devices are of the same type) :
Consider two processes P1 and P2 and two dedicated devices (non sharable devices) D1 and
D2.
P1 requests D1 and gets D1
P2 requests D2 and gets D2
P1 requests D2 but not given as it is with P2
P2 requests D1 but not given as it is with P1
D1
Back
3. Deadlocks in SPOOLING
Spooling is a concept where a Printer which is a Dedicated device is made to work like a
Shared device. Such devices are also called Virtual devices. This is done by installing a high
disk between the printer and the CPU.
User 1
User 2
Can accept only
File 2
Complete file with all pages
User 3
Buffer
User 4
The disk accepts the output for print from many users and queues it in the buffer till the printer is ready to
accept. The printer accepts a Job for printing only when the entire output is ready on Buffer.
For eg: User 1 has 34 pages to print, User 2 has 22, User 3 has 28 and User 4 has 15. All these pages are
coming to buffer first for being printed. Printer will accept the Job of a user when all pages of that job
have arrived in the buffer. Suppose in the buffer only 33 pages of User 1, 20 of User 2, 17 of User 3 and 10
of User 4 could come and the buffer is full. The printer can’t accept any job for printing as no Job has all its
pages in the buffer, the buffer cant accept more pages as the buffer is full. So nothing happens further .
Back
This is the situation of a DEADLOCK.
6. Deadlocks in DISK SHARING
Disks are shared devices. Different processes can read different areas of the disk (data stored
in tracks and cylinders) at the same time.
Consider a situation:
Process P1 requests to read data from cylinder 20.
The r/w arm starts moving to cylinder 20
At this time >>Process P2 requests to write data to cylinder 310
(as disk is a shared device)
The arm now starts moving to cylinder 310
Again
This continues and the arm remains in continuous motion between cylinder 20 Cylinder 310
and 310. It accepts requests from both the processes but none of the requests is
completed and so none of the Processes get completed. Cylinder 20
If lot of data traffic is there, the buffers of C1 and C2 are full, C1 cant accept more messages
as its buffer is full and needs to empty its buffer by sending messages to C2. But C2 cant
accept any messages from C1 as its buffer is full and needs to empty its buffer by sending
some messages to C1. C1 cant accept any messages as its buffer is full and this is a
situation of DEADLOCK between C1 and C2 and therefore there is a deadlock in the
Back
network.
Conditions for Deadlock
Deadlocks involve Processes and Resources.
But for a deadlock to occur one of the following four conditions have to occur:
1. Mutual Exclusion
2. Resource Holding
3. No Preemption
4. Circular Wait
Back
1. Mutual Exclusion
A deadlock occurs if there is Mutual Exclusion, i.e., the A resource is mutually exclusive.
A resource is mutually exclusive when it can be accessed by only one process.
Consider the situation:
Here resource R1 is allocated to P1. A deadlock
occurs because P2 requests for R1 and since R1
(which is with P1) is mutually exclusive it cannot
be shared with two processes P1 and P2 at the
same time. So a Deadlock Occurs.
Example of No Deadlock
2. Resource Holding
A deadlock occurs if there is Resource Holding i.e., A process holds a resource and requests
for another resource held by some other process, hoping that it would release the
resource.
Here process P1 has resource R1, it holds R1 and
requests for R2 (Resource Holding) hoping that
Process P2 will retreat (stop execution) and
release R2. A deadlock occurs because P2 does
not retreat and cannot release R2 as it needs R1
to finish its execution and R1 is held by P1. So a
Deadlock Occurs.
Requests for
Each process involved in the deadlock is waiting for another process to release a
resource and allow at least one process to continue and complete its execution.
In this case P1 waits for P2 to release R2 and P2 waits for P1 to release R1.
All the four conditions are required for a deadlock to occur. If any one of the
conditions is prevented from occurring , then Deadlock can be prevented to
happen.
DEADLOCK MODELLING
R1 Requests for
P1 P1 R1
Requests for
Back
Example 1
P1 has R1
P1 releases R1
P2 has R2
P2 releases R2
P3 has R3
P3 releases R3
R1 R1 R2 R2 R3 R1
P1 P1 P2 P2 P3 P3
NO DEADLOCK
Example 2
P1 requests and is given R1
P2 requests and is given R2
P3 requests and is given R3
P4 requests and is given R4
P1 requests for R2
P2 requests for R3
P3 requests for R4
P4 requests for R1
R1 R2 R3 R4
P1 P2 P3 P4
Complete DEADLOCK
Example 3
P1 requests and is allocated R1
P1 requests and is allocated R2
P2 requests R1
P3 requests and is allocated R3
P1 releases R1 which is allocated to P2
P3 requests R2
P1 releases R2 which is allocated to P3
R1 R2 R3
P1 P2 P3
NO DEADLOCK
Example 4
Process P1 has resource R, requests for resource S
Process P2 requests for resource T
Process P3 requests for resource S
Process P4 has resource U and requests for S & T
Process P5 has resource T and requests for V
Process P6 has resource W and requests for S
Process P7 has resource V and requests for U
P1 P2 P5 P7
S T V
W P6 P3 P4
Strategies for Handling Deadlocks
The Operating system uses three ways to Handle Deadlocks:
PREVENTION prevent one of the four conditions of deadlock from occurring
attack Mutual Exclusion
attack Resource Holding
attack no Preemption
attack circular wait
Back
1. PREVENTON (One of the four conditions to be prevented)
Deadlock occurs if one of the four conditions of deadlock is true. The four conditions are:
MUTUAL EXCLUSION
RESOURCE HOLDING
NO PREEMPTION
CIRCULAR WAIT
Deadlocks can be PREVENTED if one of the above four conditions can be prevented from
occurring. i.e.,
Prevent or Attack MUTUAL EXCLUSION
Prevent or Attack RESOURCE HOLDING
Prevent or Attack NO PREEMPTION
Prevent or Attack CIRCULAR WAIT
1. ATTACKING Mutual Exclusion
A deadlock occurs if there is Mutual Exclusion, i.e., the A resource is mutually exclusive.
A resource is mutually exclusive when it can be accessed by only one process.
Consider the situation:
Now we make the resource sharable (no
mutual exclusion) like spooling in
printers
R2
P1 P2
R1
Example of No Deadlock
But for some dedicated resources like printers, plotters etc PREEMPTION is not possible
4. ATTACKING Circular Wait
To avoid circular wait:
Every resource is given a number like 1, 2, 3, 4 etc
All the processes coming for execution can request for resources in the ascending order.
For example : If Process P1 needs resources 4 and 1 it will first request for 1 and then 4. If it
gets both it can start its execution, otherwise it has to wait
Suppose there are two process A and B Now we number the devices as :
There are two resources J and K J is number 1
Deadlock occurs if A has J and requests for K K is number 2
& B has K and requests for J Now if B has K (number 2)
It cannot request for J (number 1) as it
can request only in ascending order.
J
A bank has a fixed Capital amount and works on the following principles:
Customer cannot get loan more than the capital amount of the bank
Each customer will get a maximum credit limit when he opens the account
No customer can borrow money more than the credit limit
The sum of all the loans (taken by all customers) should not be more than
the capital amount.
To understand the situation assume that the bank has a capital amount of Rs. 1,00,000
There are three customers C1, C2, C3
C1 has a maximum credit limit of Rs. 40, 000
C2 has a maximum credit limit of Rs. 50, 000
C3 has a maximum credit limit of Rs. 80, 000
Suppose the bank has given some amount of loan to C2 and C3 and the situation is as:
C1 can ask for 40,000 more (remaining Credit) which is = amount remaining with bank
C2 can ask for 30,000 more (remaining Credit) which is < amount remaining with bank
C3 can also ask for 40,000 (remaining Credit) more which is = amount remaining with bank
The Bank is in a situation that it has enough money to satisfy the maximum requests of C1, C2
and C3. The bank is thus in a SAFE STATE .
Consider a situation where the bank has given loans as :
C1 can ask for 20,000 more (remaining Credit) ….. Bank has only 10,000
C2 can ask for 20,000 more (remaining Credit) …. Bank has only 10,000
C3 can ask for 40,000 (remaining Credit) ….. Bank has only 10,000
The Bank is in a situation that it cannot satisfy the maximum requests for any customer as it
only has Rs. 10,000 remaining. If it gives the remaining amount of 10,000 as loan to any
customer it can lead to a situation of deadlock as:
Suppose it gives 10,000 as loan to C1. At this time C2 also needs loan, and it is waiting
for C1 to pay back the amount as bank does not have capital left. But C1 can payback only
when it gets full amount of loan which it does not get as Bank does not have money.
The bank is thus in a UNSAFE STATE, as it cannot fulfill any customer request
Now we replace the customers by jobs and the money by the dedicated devices to
understand what can happen within a system when the operating system applies this
banking principle. Assume there are 10 devices in the system. And the allocation and
requirement is as:
J1 can ask for 4 more devices which can be given as total remaining devices are 4.
J2 can ask for 3 more which can also be given.
J3 can also ask for 4 which can be allocated to it.
If J1 gets all the devices it needs i.e., 4 the system is left with 0 remaining. ;If now J2
requests it has to wait. J1 will eventually complete as it has all what it wants and then
release the devices which can be taken by the waiting job J2.
The system is in a situation the requests from all the Jobs can be fulfilled. All the Jobs will
complete the execution smoothly without a deadlock.
This is the SAFE STATE in which the system is, as there is no deadlock.
Consider the second situation as :
As the system now has only 1 device remaining, it cannot fulfill the request of either J1, J2
or J3 as J1 needs 2 more devices, J2 needs 2 and J3 needs 4 devices.
If 1 device is allocated to J1 it cannot finish its execution as it needs 1 more.
It is waiting for J2 or J3 to finish and release devices but J2 and J3 cannot release devices as
they also need more devices held by J1. This is a situation of Deadlock.
For this, the system should identify the job which has the least number of remaining
devices requirement. The system should make sure that the number of devices available
with the system should always be equal or greater than the remaining device
requirement of this Job so that the Job can run to completion . This Job will release
resources which can be taken up by waiting Jobs and so all the jobs can run tio completion.
This is how a deadlock can be AVOIDED.
back
3. DETECTION
A directed graph or a resource allocation graph is used to detect a situation of circular
wait (cyclic formation) which indicates whether a deadlock exists or not.
The detection algorithm is used to detect a deadlock in the resource allocation graph.
Using this algorithm the directed graph is reduced as :
Steps:
1. Find a process that is currently using a resource and not waiting for any
other resource. Remove this process from the graph and release the
resources.
2. Find a process that is waiting for a resource which is not allocated to
any other processes. This process will then surely get this resource.
3. Go back to step 1 and continue till all lines connecting resources to
processes are removed.
R1 P2 P3 R1 R1 P2 P3
P2 P3
R2 R3 R3 P1 R2 R3
P1 P1 R2
NO DEADLOCK
P1 R2 R3 P1 R2 R3
Example: 1. Find a process that is currently using a resource and not waiting for any other
resource. Remove this process from the graph and release the resources.
2. Find a process that is waiting for a resource which is not allocated to any other
R1 P2 P3 processes . This process will then surely get this resource.
3. Go back to step 1 and continue till all lines connecting resources to processes are
removed.
P1 R2 R3
Step 1 Step 2
P3 is using a resource and not waiting for a resource P1 is waiting for R3 which is free
So we remove this from the graph, So P1 is given R3
R1 P2 P3 R1 P2 P3
P1 R2 R3 P1 R2 R3
Going back to Step 1 , This graph cannot be further reduced. This indicates
that a deadlock has been DETECTED.
RECOVERY from Deadlock
What happens a deadlock is detected?
There should be a way to recover from the deadlock.
Methods to RECOVER from a deadlock
1. Terminate all the jobs in the system. These jobs are resubmitted for execution later.
2. Terminate only those Jobs in the system which are involved in a deadlock.
3. Identify all the Jobs involved in the deadlocks. Terminate all these Jobs one by one to
see whether the termination of one Job can help in recovering from deadlock. If the
termination of any job helps in coming out of the deadlock its good, If not continue
terminating till deadlock is resolved.
4. A job is required to maintain snap shots or record its execution at many points. When
a deadlock is detected and the Jobs involved in the deadlock are identified, then
instead of terminating one Job completely, we start terminating (rollback) a Job can be
terminated till the last snapshot. Probably deadlock is resolved. If not go back further
to the next snap shot and so on till deadlock is resolved.
Execution starts
Snapshot 1
Job 1 Snapshot 2
Snapshot 3
Execution complete
5. Select a non deadlocked Job and terminate it. On terminating it releases all its
resources which can be given to the Jobs involved in deadlock. Probably the
deadlock can be resolved
6. Stop new jobs from entering the system. All the non deadlocked jobs can run to
completion, release all the resources and then these resources can be given to
Jobs involved in deadlock. This method can probably help to recover from a
deadlock.
There are certain factors considered when the above methods of recovery are
implemented:
High priority jobs is not selected as victim to help in recovering from a deadlock
Jobs which are about to complete are also not selected as victims
Those jobs are chosen as a victim which do not impact many other jobs
STARVATION
Situation
philosophers sit at a round table with
bowls of spaghetti
One fork is kept between each pair of
bowls
Eating is continuous… unlimited
Rules:
Each philosopher thinks and eats
alternatively
A philosopher can eat only if he has
both left and right forks
Each fork can be held by only one
philosopher at a time
After a philosopher has eaten he has
to keep the fork down ..then start
thinking
A philosopher can take the forks only
when they are available.
Processes P1
Resources
R5
R1 P2
P5
R4
R2
R3
P3
P4
Difference between Deadlock and Starvation
Deadlock Starvation
1. All processes keep waiting for 1. Some processes get executed
each other to complete and whereas some do not.
none of the processes get
executed
2. Resources are blocked by the 2. Resources are used by some
processes in the deadlock. They Processes (higher priority
cant be used by any process processes)
AGING
Aging is used to gradually increase the priority of a task so that at some point
of time it has the highest priority and can get resources to eventually execute
and not go into starvation.