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

Unit 3-1

unit 3 bca sem3

Uploaded by

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

Unit 3-1

unit 3 bca sem3

Uploaded by

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

UNIT 3

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

Deadlock : A state in which


progress is impossible

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 Process is a active unit of work or a Job under execution

Deadlock : A state in which progress is impossible

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.

A resource is always shown in a small


rectangular box like R1

A process is always shown in a circle


like P1
A resource is always shown in a small rectangular box like R1

A process is always shown in a circle like P1

When a resource is allocate to a process it is shown as


R1 Is allocated to
P1 P1
Is allocated to R1
or

When a process requests for a resource it is shown as

R1 Requests for
P1 P1 R1
Requests for

There has to be a cyclic formation


of allocation of resources to
processes for a deadlock to occur
Deadlocks occur when Jobs use resources which are NON SHARABLE or non-preemptable.
For example non sharable or non-preemptable files , printers etc.

Non sharable Resources : Resources that cannot be shared by two processes at the same
time .

Assume a deadlock situation If the resources are sharable. For


example if resource R1 can be shared

R2

P1 P2

R1

Here R1 is shared so can be


This is a situation of deadlock as no
allocated to Process P1 and P2 at
process can continue. Here the
the same time i.e., sharable
resources are not sharable .
resource
Deadlocks occur when Jobs use resources which are NON SHARABLE or NON PREEMPTABLE.
For example non sharable or non-preemptable files , printers etc.

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

This is a situation of deadlock as


no process can continue. Here the Here R1 is preempted from P1 and
resources are non preemptable now can be allocated to Process P1
Seven Cases of Deadlock
1. Deadlocks on File Requests : For eg: consider a company with two application
programs, purchase (P1) and sales (P2), running at the same time . Both need two files,
inventory (F1) and suppliers (F2), to read and write transactions. Consider a situation:
 Purchase Process (P1) accesses the supplier file (F2)  P1 has F2
 Sales Process (P2)accesses the inventory file (F1)  P2 has F1

 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

This is a situation of Deadlock • If jobs requests and hold files


throughout their execution,
deadlock can occur.
• This deadlock will remain until
one of the two Processes is
closed or forcibly removed and
its file is released. Only then
can the other program
continue and the system
comes out of deadlock.
2. Deadlocks on Databases : Deadlock can occur if two processes
access and lock records in database.
Locking is a technique by which data only be accessed by the user
who has locked it. Other users will be denied access to the locked
data. This results in Data Integrity.
DB1 Three levels of locks are used:
a. Database level locks : Entire database (including all tables and rows within
T1 them) is locked by a user. Performance of the system is reduced as other
T3
users of this database have to wait.
T2
User 2 tries to access T2 but User 1 locks Record 1 of T1
is denied access DB1
Record 1 T1

Record 2 T1

Record 5
User 1 locks T1 T3
T3
DB1 b. Table level locks : One table
T2

of the database (all rows within User 2 requests for Record 5


T1 the table is locked by a user. of T1 and gets access
T3 c. Record level locks : One table of the
T2 database (all rows within the table is locked
User 2 tries to access and is by a user. Performance of system highly
given access
improves as there is faster access
Assume here system has record level locks: Records are locked for execution and then
released when execution gets over.

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.

Consider the situation where no locks are used to avoid deadlocks.

Record 1 (R1)
not locked

Process 1 (P1) Process 1 (P2)

P1 P2 Both P1 & P2 have R1:


P1 updates GPA value
P2 updates ADDR value

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

Process 1 (P1) Process 1 (P2)

P1 P2 Both P1 & P2 have R1:


P1 updates GPA value
P2 updates ADDR value

P2 writes back The write operation of P2 is


2.5
Main St.
R1 to database overwritten by the write
with new ADDR operation of P1.
P1 writes back Jones
GPA = 3.0
R1 to database Here Only GPA is updated
ADDR: 5th Ave
with new GPA
P1 requests R1 and is given R1 P1 requests R1 and is given R1
P2 requests R1 and is given R1 P2 requests R1 and is given R1
P1 & P2 update R1 P1 & P2 update R1
P1 writes P2 writes
P2 writes P1 writes
Here Process P2 writes first then P1 Here Process P1 writes first then P2
writes. writes.
The write operation of P2 is The write operation of P1 is
overwritten by the write operation of overwritten by the write operation
P1. of P2.

Here Only ADDR is updated Here Only GPA is updated

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

If the order is P1 then P2  Process P2 gets completed, P1 gets lost


If the order is P2 then P1  Process P1 gets completed, P2 gets lost

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

Neither Jobs can continue as each


is waiting for the other to finish
P2 and release the device, which will
P1 never happen.
P1 waits for P2 to release D2, P2
cannot release D2 as it needs D1
D2
` which is with P1 and P1 cannot
release D1 as it needs D2 …..

This is a situation of Deadlock.


4. Deadlocks in Multiple type of devices:

P1 requests Tape drive and gets it


P2 requests Printer and gets it
P3 requests Plotter and gets it

P1 requests Printer which is blocked with P2


P2 requests plotter which is blocked with P3
P3 requests tape drive which is blocked with P1

None of the Jobs can continue their execution


further as each process needs a resource held by
the other process

This is a situation of DEADLOCK.

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

P1 Read records at Disk


cylinder 20 I/O
control
Channel
P2 Write to file at unit
cylinder 310
Back
7. DEADLOCKS IN A NETWORK : A network can get deadlocked if the flow of data on the
network is not controlled through an appropriate protocol.
Consider a network having 7 computers on its different nodes:

Node C1 receives messages from node C2, C6,C7


C1 can send messages only to C2

Node C2 receives messages from C1, C3 and C4


C2 can send messages to C1 and C3

If C6 and C7 wish to communicate with C2 they


do so through node C1 by buffering it in the
output queue of C1.

If C3 and C4 wish to communicate to C1 they do


so through node C2 by buffering it in the output
queue of C2.

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.

Process : Moving cars Resource: Roads


& buses

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.

Let us make Resource R1 NOT MUTUALLY


R2 EXCLUSIVE
Here resource R1 is first allocated to P1. P2
requests for R1 and since R1 is mutually exclusive
P1 P2
(can be shared with two processes) R1 is
allocated to P2 also at the same time. So No
Deadlock Occurs.
R1

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.

R2 Let us remove the possibility of RESOURCE HOLDING

Here process P1 has resource R1, it holds R1 and


requests for R2. R2 is held by P2. At this point if
P1
Process P2 retreats it will release R2, which can
be taken up by P1. So No Deadlock Occurs.
R1
3. No Preemption
A deadlock occurs when a resource cannot be preempted (taken away) from a Process in
between execution, so that it can be given to some other process who needs it.

Here Process P1 has resource R1. It requests for


resource R2.
Process P2 has R2 and requests for R1 which is
with P1.

R2 cannot be preempted from P2 and given to


P1as there is a condition of No preemption.So a
Deadlock Occurs.

Let us remove the condition of NO PREEMPTION


R2
If Resource R2 can be preempted, it is
temporarily taken away from P2.
P1 P2

R2 can now be given to P1 as it has requested for


P1. As P1 now has both R1 and R2 it can finish
R1 the execution, release R1 and R2, and be out of
the system. No DEADLOCK occurs now.
4. Circular Wait
All the three conditions of Mutual Exclusion, Resource Holding and No preemption lead to
the fourth condition of Circular Wait.

Is allocated to Requests for Is allocated to


R1 P1 R2 P2

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

Using a Resource Allocation Graph or a Directed Graph

A resource is always shown in a small rectangular box like R1

A process is always shown in a circle like P1

When a resource is allocate to a process it is shown as


R1 Is allocated to
P1 P1
Is allocated to R1

When a process requests for a resource it is shown as

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

 AVOIDANCE : avoid the deadlock if there are chances of it to occur

 DETECTION : If deadlock occurs, Detect it and then recover from it

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

SOME RESOURCES CANNOT BE MADE MUTUALLY EXCLUSIVE like : CPU


So it is at times not possible to prevent the condition of Mutual Exclusion.
1. ATTACKING 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.

Let us remove the possibility of RESOURCE HOLDING


R2
Every process should request for all the resources
waiting
it needs before starting execution. If it gets it can
P1 P2 start its execution or else it has to wait.
If P1 is running with two resources R1 and R2.
Now if P2 needs R1, R2 and R3, it has to wait
R1 as R1 and R2 are busy. So NO DEADLOCK
Drawback is: Not all Jobs know before starting which resources they will need.
3. ATTACKING NO PREEMPTION
Preemption is only possible in resources which can be preempted without losing the
execution in process. In some resources like CPU (as in Round Robin scheduling) the
execution of the process is saved before the CPU is PREEMPTED and given to another
process

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

If B cannot request for J Deadlock will


A B
not be possible as there cannot be any
cyclic or circular formation.
SO NO CIRCULAR WAIT
K
back
2. AVOIDANCE
If Deadlocks cannot be Prevented , they can be avoided, is the system knows how the
processes will requests for the resources.
This is implemented by the concept of BANKER’s ALGORITHM which states that:

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:

Customer Loan Amount Maximum Credit Remaining Credit


C1 0 40,000 40,000
C2 20,000 50,000 30,000
C3 40,000 80,000 40,000

Total amount of loan : 60,000


Total Capital Amount : 1,00,000
Total Remaining with Bank is (1,00,000 - 60,000) = 40,000

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 :

Customer Loan Amount Maximum Credit Remaining Credit


C1 20,000 40,000 20,000
C2 30,000 50,000 20,000
C3 40,000 80,000 40,000

Total amount of loan : 90,000


Total Capital Amount : 1,00,000
Total Remaining with Bank is (1,00,000 - 90,000) = 10,000

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:

Job Devices Allocated Maximum devices required Remaining can need


J1 0 4 4
J2 2 5 3
J3 4 8 4

Total number of devices allocated : 6


Total Devices in the system : 10
Total devices Remaining = 4

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 :

Job Devices Allocated Maximum devices required Remaining can need


J1 2 4 2
J2 3 5 2
J3 4 8 4

Total number of devices allocated : 9


Total Devices in the system : 10
Total devices Remaining = 1

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.

This is the UNSAFE STATE in which the system is, as there is a


deadlock.
The system should allocate the resources in such a way that it never goes to an unsafe
state.

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.

If there are lines left this indicates that a deadlock exists.


Example: 1. Find a process that is currently using a resource and not waiting for any other
R1 P2 P3 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.

If there are lines left this indicates that a deadlock exists.


P1 R2 R3

Step 1 Step 2 Step 3: back to step 1


P3 has for R3, not waiting for any P1 is waiting for R3 which is free P1 has for R2, R3, not waiting for
other resource, so remove P3 So P1 is given R3 any other resource, so remove P1

R1 P2 P3 R1 R1 P2 P3
P2 P3

R2 R3 R3 P1 R2 R3
P1 P1 R2

P1 has for R2, R3, not waiting for


Step 2 P1 is waiting for R2 which is free Step 3: back to step 1 any other resource, so remove P1
So P1 is given R2
R1 P2 P3 R1 P2 P3

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.

If there are lines left this indicates that a deadlock exists.

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)

3. More than two processes are


involved in deadlock 3. A single process can also go
into starvation.
4. Deadlock can be prevented by
preventing the four conditions 4. Aging can help to remove
of Deadlock starvation

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.

You might also like