0% found this document useful (0 votes)
7 views45 pages

OS Unit-3.1

Uploaded by

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

OS Unit-3.1

Uploaded by

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

Unit – 3

Inter process Communication


Topics to be covered
▪ Definitions
▪ Deadlock characteristics
▪ Deadlock ignorance
• Ostrich Algorithm
▪ Deadlock detection and recovery
▪ Deadlock avoidance
• Banker’s algorithm
▪ Deadlock prevention
What is Deadlock?
What is Deadlock?
▪ A set of processes is deadlocked if each process in the set is
waiting for an event that only another process in the set can
cause.
▪ Deadlocks are a set of blocked processes each holding a resource
and waiting to acquire a resource held by another process.

Process Process
Hold P1 Request

Request Hold
R1 DEADLOCK R2

P2 Hold
Request
Resource Resource
Preemptable and non-preemptable resource
▪ Preemptable:- Preemptive resources are those which can be
taken away from a process without causing any ill effects to the
process.
• Example:- Memory.
▪ Non-preemptable:- Non-pre-emptive resources are those which
cannot be taken away from the process without causing any ill
effects to the process.
• Example:- CD-ROM (CD recorder), Printer.
Deadlock v/s Starvation
Deadlock Starvation
All processes keep waiting for each other to High priority process keep executing and low
complete and none get executed. priority process are blocked.
Resources are blocked by the process. Resources are continuously utilized by the
higher priority process.
Necessary conditions are mutual exclusion, Priorities are assigned to the process.
hold and wait, no preemption, circular wait.
Also known as circular wait. Also known as lived lock.
It can be prevented by avoiding the It can be prevented by Aging.
necessary conditions for deadlock.

Aging is a scheduling technique used to avoid Starvation.


Conditions that lead to deadlock
1. ͏Mutual exclusion
• Each resource is either currently assigned to exactly one process
or is available.
2. Hold and wait
• Process currently holding resources granted earlier can request
more resources.
3. No preemption
• Previously granted resources cannot be forcibly taken away from
process.
4. Circular wait
• There must be a circular chain of 2 or more processes. Each
process is waiting for resource that is held by next member of the
chain.
▪ All four of these conditions must be present for a deadlock to
occur.
Strategies for dealing with deadlock
1. Just ignore the problem.
2. ͏Detection and recovery.
• Let deadlocks occur, detect them and take action.
3. Dynamic avoidance by careful resource allocation.
4. ͏Prevention, by structurally negating (killing) one of the four
required conditions.
Deadlock ignorance (Ostrich Algorithm)
▪ When storm approaches, an ostrich puts his head in the sand
(ground) and pretend (imagine) that there is no problem at all.
▪ Ignore the deadlock and pretend that deadlock never occur.
▪ Reasonable if
• deadlocks occur very rarely
• difficult to detect
• cost of prevention is high
▪ UNIX and Windows takes this approach
Deadlock detection for single resource (RAG)
• We are starting from node D.
R A B • Empty list L = ()
• Add current node so Empty list
C S D T E = (D).
• From this node there is one
outgoing arc to T so add T to
F U V list.
• So list become L = (D, T).
• Continue this step….so we get
W G
list as below
• L = (D, T, E)………… L = (D, T, E, V,
G, U, D)
• In the above step in list the
node D appears twice, so
deadlock.
Deadlock detection for single resource
▪ Algorithm for detecting deadlock for single resource
1. For each node, N in the graph, perform the following five steps
with N as the starting node.
i. Initialize L to the empty list, designate all arcs as unmarked.
ii. Add current node to end of L, check to see if node now appears in
L two times. If it does, graph contains a cycle (listed in L),
algorithm terminates.
iii. From given node, see if any unmarked outgoing arcs. If so, go to
step 4; if not, go to step 5.
iv. Pick an unmarked outgoing arc at random and mark it. Then
follow it to the new current node and go to step 2.
v. If this is initial node, graph does not contain any cycles, algorithm
terminates. Otherwise, dead end. Remove it, go back to previous
node, make that one current node, go to step 2.
Deadlock detection for multiple resource
Tap Tap
CD CD
e Plott Scan e Plott Scan
Rom A= Rom
E= Driv ers ners
s Driv ers ners
s
e e
4 2 3 1 2 1 0 0
total no of each resource no of resources that are
available (free)

Pro Tape CD Pro Tape CD


Plott Scan Plott Scan
ces Driv Rom ces Driv Rom
C= s e
ers ners
s R= s e
ers ners
s

P1 0 0 1 0 P1 2 0 0 1

P2 2 0 0 1 P2 1 0 1 1

P3 0 1 2 0 P3 2 1 0 0

no of resources held by each no of resources still needed by


process each process to proceed
Deadlock detection for multiple resource
Tap Tap
CD CD
e Plott Scan e Plott Scan
Rom A= Rom
E= Driv ers ners
s Driv ers ners
s
e e
4 2 3 1 0 0 0 0
total no of each resource no of resources that are
available (free)

Pro Tape CD Pro Tape CD


Plott Scan Plott Scan
ces Driv Rom ces Driv Rom
C= s e
ers ners
s R= s e
ers ners
s

P1 0 0 1 0 P1 2 0 0 1

P2 2 0 0 1 P2 1 0 1 1

P3 2 2 2 0 P3 0 0 0 0

no of resources held by each no of resources still needed by


process each process to proceed
Deadlock detection for multiple resource
Tap Tap
CD CD
e Plott Scan e Plott Scan
Rom A= Rom
E= Driv ers ners
s Driv ers ners
s
e e
4 2 3 1 2 2 2 0
total no of each resource no of resources that are
available (free)

Pro Tape CD Pro Tape CD


Plott Scan Plott Scan
ces Driv Rom ces Driv Rom
C= s e
ers ners
s R= s e
ers ners
s

P1 0 0 1 0 P1 2 0 0 1
DEADLOCK
P2 2 0 0 1 P2 1 0 1 1

P3 0 0 0 0 P3 0 0 0 0

no of resources held by each no of resources still needed by


process each process to proceed
Deadlock recovery
1. Recovery through pre-emption
• In this method resources are temporarily taken away from its
current owner and give it to another process.
• The ability to take a resource away from a process, have another
process use it, and then give it back without the process noticing
it is highly dependent on the nature of the resource.
• Recovering this way is frequently difficult or impossible.

Hold P1 Request
Hold

R1 R2

P2 Hold
Deadlock recovery (cont…)
2. Recovery through rollback
• PCB (Process Control Block) and resource state are periodically
saved at “checkpoint”.
• When deadlock is detected, rollback the preempted process up
to the previous safe state before it acquired that resource.
• Discard the resource manipulation that occurred after that
checkpoint.
• Start the process after it is determined it can run again.
A A A

F1 F2
R R

First Second B
Checkpoints Checkpoints
Deadlock recovery (cont…)
3. Recovery through killing processes
• The simplest way to break a deadlock is to kill one or more
processes.
• Kill all the process involved in deadlock
• Kill process one by one.
– After killing each process check for deadlock
» If deadlock recovered then stop killing more process
» Otherwise kill another process
Safe and unsafe states
▪ A state is said to be safe if it is not deadlocked and there is some
scheduling order in which every process can run to completion
even if all of them suddenly request their maximum number of
resources immediately.
▪ Total resources are 10 Proce Has Max
ss
▪ 7 resources already allocated
A 3 9
▪ So there are 3 still free
B 2 4
▪ A need 6 resources more to complete it. C 2 7
▪ B need 2 resources more to complete it. Free : 3
▪ C need 5 resources more to complete it.
Safe states
Proce Has Max Proce Has Max Proce Has Max
ss ss ss
A 3 9 A 3 9 A 3 9
4
B 2 4 2 B 4 4 B 0 -
C 2 7 C 2 7 C 2 7
Free : 3 Free : 1 Free : 5

Proce Has Max Proce Has Max Proce Has Max


ss ss ss
5
A 3 9 A 3 9 A 9 9
6
B 0 - 7 B 0 - B 0 -
C 7 7 C 0 - C 0 -
Free : 0 Free : 7 Free : 1
Unsafe states
Proce Has Max Proce Has Max Proce Has Max
ss ss ss
A 3 9 A 4 9 A 4 9
1
B 2 4 B 2 4 2 B 4 4
C 2 7 C 2 7 C 2 7
Free : 3 Free : 2 Free : 0

Proce Has Max


ss
4
A 4 9
B 0 -
C 2 7
Free : 4
Deadlock avoidance
▪ Deadlock can be avoided by allocating resources carefully.
▪ Carefully analyse each resource request to see if it can be safely
granted.
▪ Need an algorithm that can always avoid deadlock by making right
choice all the time (Banker’s algorithm).
▪ Banker’s algorithm for single resource
▪ Banker’s algorithm for multiple resource
Banker’s algorithm for single resource
▪ What the algorithm does is check to see if granting the request
leads to an unsafe state. If it does, the request is denied.
▪ If granting the request leads to a safe state, it is carried out.
▪ If we have situation as per figure
• then it is safe state Proce Has Max
• because with 10 free units ss
• one by one all customers can be served. A 0 6
B 0 5
C 0 4
D 0 7
Free : 10
Banker’s algorithm for single resource
Proce Has Max Proce Has Max Proce Has Max
ss ss ss
A 1 6 A 1 6 A 1 6
B 1 5 B 1 5 B 1 5
C 2 4 C 4 4 C 0 0
D 4 7 D 4 7 D 4 7
Free : 2 Free : 0 Free : 4

Proce Has Max Proce Has Max Proce Has Max


ss ss ss
A 1 6 A 1 6 A 1 6
B 1 5 B 1 5 B 5 5
C 0 - C 0 - C 0 -
D 7 7 D 0 - D 0 -
Free : 1 Free : 8 Free : 4
Banker’s algorithm for single resource
Proce Has Max Proce Has Max Proce Has Max
ss ss ss
A 1 6 A 6 6 A 0 -
B 0 - B 0 - B 0 -
C 0 - C 0 - C 0 -
D 0 - D 0 - D 0 -
Free : 9 Free : 4 Free : 10

• The order of execution is C, D, B, A. So if we can find proper order of execution then


there is no deadlock.
Banker’s algorithm for single resource
Proce Has Max
ss
A 1 6
B 2 5
C 2 4
D 4 7
Free : 1
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 1 0 2 0
total no of each resource resources hold Available (free) resources

Pro Tape CD Pro Tape CD


Plott Scan Plott Scan
ces Driv Rom ces Driv Rom

no of resources still needed by


ers ners ers ners
e s e s
no of resources held by each
s s

each process to proceed


P1 3 0 1 1 P1 1 1 0 0

P2 0 1 0 0 P2 0 1 1 2

P3 1 1 1 0 P3 3 1 0 0

P4 1 1 0 1 P4 0 0 1 0
process

P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 1 0 1 0
total no of each resource resources hold Available (free) resources

Pro Tape CD Pro Tape CD


Plott Scan Plott Scan
ces Driv Rom ces Driv Rom

no of resources still needed by


ers ners ers ners
e s e s
no of resources held by each
s s

each process to proceed


P1 3 0 1 1 P1 1 1 0 0

P2 0 1 0 0 P2 0 1 1 2

P3 1 1 1 0 P3 3 1 0 0

P4 1 1 1 1 P4 0 0 0 0
process

P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 2 1 2 1
total no of each resource resources hold Available (free) resources

Pro Tape CD Pro Tape CD


Plott Scan Plott Scan
ces Driv Rom ces Driv Rom

no of resources still needed by


ers ners ers ners
e s e s
no of resources held by each
s s

each process to proceed


P1 3 0 1 1 P1 1 1 0 0

P2 0 1 0 0 P2 0 1 1 2

P3 1 1 1 0 P3 3 1 0 0

P4 - - - - P4 0 0 0 0
process

P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 1 0 2 1
total no of each resource resources hold Available (free) resources

Pro Tape CD Pro Tape CD


Plott Scan Plott Scan
ces Driv Rom ces Driv Rom

no of resources still needed by


ers ners ers ners
e s e s
no of resources held by each
s s

each process to proceed


P1 4 1 1 1 P1 0 0 0 0

P2 0 1 0 0 P2 0 1 1 2

P3 1 1 1 0 P3 3 1 0 0

P4 - - - - P4 0 0 0 0
process

P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 5 1 3 2
total no of each resource resources hold Available (free) resources

Pro Tape CD Pro Tape CD


Plott Scan Plott Scan
ces Driv Rom ces Driv Rom

no of resources still needed by


ers ners ers ners
e s e s
no of resources held by each
s s

each process to proceed


P1 - - - - P1 0 0 0 0

P2 0 1 0 0 P2 0 1 1 2

P3 1 1 1 0 P3 3 1 0 0

P4 - - - - P4 0 0 0 0
process

P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 5 0 2 0
total no of each resource resources hold Available (free) resources

Pro Tape CD Pro Tape CD


Plott Scan Plott Scan
ces Driv Rom ces Driv Rom

no of resources still needed by


ers ners ers ners
e s e s
no of resources held by each
s s

each process to proceed


P1 - - - - P1 0 0 0 0

P2 0 2 1 2 P2 0 0 0 0

P3 1 1 1 0 P3 3 1 0 0

P4 - - - - P4 0 0 0 0
process

P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 5 2 3 2
total no of each resource resources hold Available (free) resources

Pro Tape CD Pro Tape CD


Plott Scan Plott Scan
ces Driv Rom ces Driv Rom

no of resources still needed by


ers ners ers ners
e s e s
no of resources held by each
s s

each process to proceed


P1 - - - - P1 0 0 0 0

P2 - - - - P2 0 0 0 0

P3 1 1 1 0 P3 3 1 0 0

P4 - - - - P4 0 0 0 0
process

P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 2 1 3 2
total no of each resource resources hold Available (free) resources

Pro Tape CD Pro Tape CD


Plott Scan Plott Scan
ces Driv Rom ces Driv Rom

no of resources still needed by


ers ners ers ners
e s e s
no of resources held by each
s s

each process to proceed


P1 - - - - P1 0 0 0 0

P2 - - - - P2 0 0 0 0

P3 4 2 1 0 P3 0 0 0 0

P4 - - - - P4 0 0 0 0
process

P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 6 3 4 2
total no of each resource resources hold Available (free) resources

Pro Tape CD Pro Tape CD


Plott Scan Plott Scan
ces Driv Rom ces Driv Rom

no of resources still needed by


ers ners ers ners
e s e s
no of resources held by each
s s

each process to proceed


P1 - - - - P1 0 0 0 0

P2 - - - - P2 0 0 0 0

P3 - - - - P3 0 0 0 0

P4 - - - - P4 0 0 0 0
process

P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 4 2 3 2
total no of each resource resources hold Available (free) resources

Pro Tape CD Pro Tape CD


Plott Scan Plott Scan
ces Driv Rom ces Driv Rom

no of resources still needed by


ers ners ers ners
e s e s
no of resources held by each
s s

each process to proceed


P1 - - - - P1 0 0 0 0

P2 - - - - P2 0 0 0 0

P3 - - - - P3 0 0 0 0

P4 - - - - P4 0 0 0 0
process

P5 2 1 1 0 P5 0 0 0 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 6 3 4 2
total no of each resource resources hold Available (free) resources

Pro Tape CD Pro Tape CD


Plott Scan Plott Scan
ces Driv Rom ces Driv Rom

no of resources still needed by


ers ners ers ners
e s e s
no of resources held by each
s s

each process to proceed


P1 - - - - P1 0 0 0 0

P2 - - - - P2 0 0 0 0

P3 - - - - P3 0 0 0 0

P4 - - - - P4 0 0 0 0
process

P5 - - - - P5 0 0 0 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 1 0 2 0
total no of each resource resources hold Available (free) resources

Pro Tape CD Pro Tape CD


Plott Scan Plott Scan
ces Driv Rom ces Driv Rom

no of resources still needed by


ers ners ers ners
e s e s
no of resources held by each
s s

each process to proceed


P1 3 0 1 1 P1 1 1 0 0

P2 0 1 0 0 P2 0 1 1 2

P3 1 1 1 0 P3 3 1 0 0

P4 1 1 0 1 P4 0 0 1 1
process

P5 0 0 0 0 P5 2 1 1 0
Deadlock prevention
▪ Deadlock can be prevented by attacking the one of the four
conditions that leads to deadlock.
1. Attacking the Mutual Exclusion Condition
2. Attacking the Hold and Wait Condition
3. Attacking the No Preemption Condition
4. Attacking the Circular Wait Condition
Attacking the mutual exclusion condition
▪ No deadlock if each resource can be assigned to more than one
process.
▪ We can not assign some resources to more than one process at a
time such as CD-Recorder, Printer etc…
▪ So this solution is not feasible.
Attacking the hold and wait condition
▪ Require processes to request all their resources before starting
execution.
▪ A process is allowed to run if all resources it needed is available.
Otherwise nothing will be allocated and it will just wait.
▪ Problem with this strategy is that a process may not know
required resources at start of run.
▪ Resource will not be used optimally.
Attacking the no preemption condition
▪ When a process P0 request some resource R which is held by
another process P1 then resource R is forcibly taken away from
the process P1 and allocated to P0.
▪ Consider a process holds the printer, halfway through its job;
taking the printer away from this process without having any ill
effect is not possible.
▪ This is not a possible option.
Attacking the circular wait condition
▪ Provide a global numbering of all the resources.
▪ Now the rule is that: processes can request resources whenever
they want to, but all requests must be made in numerical order.
▪ A process need not acquire them all at once.
▪ Circular wait is prevented if a process holding resource n cannot
wait for resource m, if m > n.
1. Printer
2. Scanner
3. Plotter
4. Tape Drive
5. CD Rom
▪ A process may request 1st a CD ROM, then tape drive. But it may
not request 1st a tape drive, then CD ROM.
▪ Resource graph can never have cycle.
Example
▪ Consider a system consisting of four resources of same type that
are shared by three processes, each of which needs at most two
resources. Show the system is deadlock free.
Proces Has Max Proces Has Max Proce Has Max
s s ss
A 1 2 A 2 2 A 0 0
B 1 2 B 1 2 B 1 2
C 1 2 C 1 2 C 1 2
Total : 4, Free : 1 Free : 0 Free : 2

Proce Has Max Proce Has Max Proce Has Max Proce Ha
ss ss ss ss s
A 0 0 A 0 0 A 0 0 A 0
B 2 2 B 0 0 B 0 0 B 0
C 1 2 C 1 2 C 2 2 C 0
Free : 1 Free : 3 Free : 2 Free : 4
Questions
1. What is RAG? Explain briefly.
2. What is Deadlock? List the conditions that lead to deadlock. How
Deadlock can be prevented?
3. Which are the necessary conditions for Deadlock? Explain
Deadlock recovery in brief.
Questions
4. Consider the snapshot of the system with Five Processes and Four
types of resources A,B,C,D.
Allocation Max
A B C D A B C D
P0 0 0 1 2 0 0 1 2
P1 1 0 0 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6

• Currently Available set of resources is (1,5,2,0).


• Answer the following Questions using bankers algorithm.
I. Find the content of Need Matrix.
II. Is the System in Safe State?
III. If request from Process P1 arrives for (0,4,2,0) can the request be
granted immediately

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy