OS Chapter 6 (Deadlock) (13!4!2020) 1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

Concurrency: Deadlock and

Starvation
Deadlock and Starvation problems in
concurrent process environment will be
discussed
Reference:  
Operating Systems: Internals and Design Principles by Author William
Stallings
Concurrency: Deadlock and
Starvation
• Lecture Contents
– Deadlock and Starvation – Deadlock Avoidance
introduction – Two Approaches to Deadlock
– Deadlock Avoidance
– Reusable and Consumable – Resource Allocation Denial
Resources – The Banker's Algorithm
– Resource Allocation Graphs – Deadlock Avoidance and its
– Conditions for Deadlock conditions
– Possibility of Deadlock – Deadlock Detection
– Existence of Deadlock – Strategies Once Deadlock
– Deadlock Prevention Detected
– Dining Philosophers Problem
Objective and Learning Outcome
• Objective
– The main goal of this lesson is to help students understand the
reasons why deadlock happens, its prevention methods, and
how to recover from a deadlock situation.
• Learning Outcome
– At the end of this lesson the students will be al to understand:
• The reason behind the occurrence of deadlock in computer systems
• The methods and related algorithms used to prevent and avoid the
deadlock condition
• The methods and related algorithms used once deadlock is
detected
Introduction
• Deadlock
– A set of processes is deadlocked when every process
in the set is waiting for a resource that is currently
allocated to another process in the set ( and which
can only be released when that other waiting process
makes progress).
• Starvation
– Starvation is a condition where a process does not
get the resources it needs for a long time because the
resources are being allocated to other processes. It
generally occurs in a Priority based scheduling
System.
Deadlock
• Permanent blocking of a set of processes that
either compete for system resources or
communicate with each other
• No efficient solution
• Involve conflicting needs for resources by two or
more processes
Deadlock
• The figure 6.1, which is of a traffic deadlock,
makes the nature of deadlock very clear.

• None of the cars can pass unless at least one of


them backs up or is removed
Reusable Resources
• Reusable resources are used by only one
process at a time and not depleted by that use

• Processes obtain resources that they later


release for reuse by other processes
Reusable Resources
• Examples
• Processors, I/O channels, main and secondary
memory, devices, and data structures such as
files, databases, and semaphores

• Deadlock occurs if each process holds one


resource and requests the other
Reusable Resources
• Figure 6.4

• Consider two processes that compete for


exclusive access to a disk file D and a tape drive
T.

• Deadlock occurs if each process holds one


resource and requests the other.

• p0 p1 q0 q1 p2 q2 will lead to deadlock


Reusable Resources
• Space is available for allocation of 200Kbytes,
and the following sequence of events occur

P1 P2
... ...
Request 80 Kbytes; Request 70 Kbytes;
... ...
Request 60 Kbytes; Request 80 Kbytes;

• Deadlock occurs if both processes progress to


their second request
Consumable Resources
• Consumable resources are created (produced)
and destroyed (consumed)
• Examples
• Interrupts, signals, messages, and information in
I/O buffers
• Deadlock may occur if a Receive message is
blocking
• It may take a rare combination of events to
cause deadlock
Example of Deadlock
• Deadlock occurs if receive is blocking

P1 P2
... ...
Receive(P2); Receive(P1);
... ...
Send(P2, M1); Send(P1, M2);
Resource Allocation Graphs
• Directed graph that depicts a state of the system
of resources and processes
Resource Allocation Graphs
• In some cases deadlocks can be understood more
clearly through the use of Resource-Allocation Graphs,
having the following properties:A set of resource
categories, { R1, R2, R3, . . ., RN }, which appear as
square nodes on the graph. Dots inside the resource
nodes indicate specific instances of the resource. ( E.g.
two dots might represent two laser printers. )

• A set of processes, { P1, P2, P3, . . ., PN }


Resource Allocation Graphs
• Request Edges - A set of directed arcs from Pi to Rj,
indicating that process Pi has requested Rj, and is
currently waiting for that resource to become available.

• Assignment Edges - A set of directed arcs from Rj to Pi


indicating that resource Rj has been allocated to process
Pi, and that Pi is currently holding resource Rj.

• Note that a request edge can be converted into


an assignment edge by reversing the direction of the
arc when the request is granted. 
Conditions for Deadlock
• There are four conditions that are necessary to
achieve deadlock

1) Mutual exclusion
– Only one process may use a resource at a time
2) Hold-and-wait
– A process may hold allocated resources while
awaiting assignment of others
Conditions for Deadlock
3) No preemption
– No resource can be forcibly removed form a process
holding it
4)Circular wait
– A closed chain of processes exists, such that each
process holds at least one resource needed by the
next process in the chain
Resource Allocation Graphs
Resource Allocation Graphs

Resource Allocation Graph with Deadlock


Possibility of Deadlock
Deadlock is possible if these 3 conditions are
there in the system

• Mutual Exclusion
• No preemption
• Hold and wait
Existence of Deadlock
• Deadlock can exist if and only if 4 conditions
hold simultaneously:

• Mutual Exclusion
• No preemption
• Hold and wait
• Circular wait
To be continued….

You might also like