OS Lecture 4
OS Lecture 4
2021-2022
Introduction
• In a multiprogramming environment, several
processes may compete for a finite number of
resources.
• A process requests resources; if the resources are
not available at that time, the process enters a
waiting state.
• Sometimes, a waiting process is never again able
to change state, because the resources it has
requested are held by other waiting processes.
• This situation is called a deadlock
Deadlock
• So Deadlock situation is: A set of blocked
processes each holding a resource and waiting
to acquire a resource held by another process
in the set.
• Example
– System has 2 disk drives.
– P1 and P2 each hold one disk drive and each needs
another one.
System Model
• A system consists of a finite number of resources
to be distributed among a number of competing
processes.
• The resources are partitioned into several types,
each consisting of some number of identical
instances. Memory space, CPU cycles, files, and
I/0 devices (such as printers and DVD drives) are
examples of resource types.
• If a system has two CPUs, then the resource type
CPU has two instances. Similarly, the resource
type printer may have five instances.
System Model
• Resource types R1, R2, . . ., Rm
• Each resource type Ri has Wi instances.
• Each process utilizes a resource as follows:
1. Request: The process requests the resource. If the
request cannot be granted immediately (for example, if
the resource is being used by another process), then
the requesting process must wait until it can acquire
the resource.
2. use
3. release
Deadlock Characterization
• In a deadlock, processes never finish
executing, and system resources are tied up,
preventing other jobs from starting.
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
• The system is in a safe state since the sequence < P1, P3, P4, P2, P0>
satisfies safety criteria
Example: P1 Request (1,0,2)
• Check that Request Available (that is, (1,0,2) (3,3,2) true.
Allocation Need Available
ABC ABC ABC
P0 0 1 0 743 230
P1 3 0 2 020
P2 3 0 2 600
P3 2 1 1 011
P4 0 0 2 431
• Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2>
satisfies safety requirement.
• Now can request for (3,3,0) by P4 be granted?
• Can request for (0,2,0) by P0 be granted?
Deadlock Detection
• If a system does not employ either a deadlock-
prevention or a deadlock avoidance algorithm,
then a deadlock situation may occur. In this
environment, the system must provide:
• An algorithm that examines the state of the
system to determine whether a deadlock has
occurred
• An algorithm to recover from the deadlock
Single Instance of Each Resource Type
The Algorithm:
1. Let Work and Finish be vectors of length m and n,
respectively Initialize:
(a) Work = Available
(b) For i = 1,2, …, n, if Allocation i 0, then
Finish[i] = false; otherwise, Finish[i] = true.
Detection Algorithm
2. Find an index i such that both:
(a) Finish[i] == false
(b) Requesti Work
If no such i exists, go to step 4
• Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i.
Example of Detection Algorithm
• P2 requests an additional instance of type C.
Request
ABC
P0 0 0 0
P1 2 0 2
P2 0 0 1
P3 1 0 0
P4 0 0 2
• State of system?
– Deadlock exists, consisting of processes P1, P2, P3, and P4
Detection-Algorithm Usage
• When should we invoke the detection
algorithm? The answer depends on two
factors:
1. Process Termination
2. Preempting Recourses
Process Termination
• we use one of two methods. In both methods, the system
reclaims all resources allocated to the terminated
processes.