Unit -3 Deadlock
Unit -3 Deadlock
c) No Preempti~n: Resources cannot be preempted from a process. They must be released by the
process holding them.
d) · · processes such that PO is waiting fo
Circular Wait: There must be a set (PO , Pl , •··, p n ) of waiting
a resource held by Pl, Pl is waiting for a resource held by P2, and so on, with Pn waiting f r
resource held by PO. or a
c) Deadlock Detection and Recovery: In this method, the system does not prevent or avoid
deadlocks, but instead, it detects when a deadlock has occurred and then recovers from it. This
can involve identifying and terminating one or more processes involved in the deadlock, or
progressively rolling back processes to a safe state.
Deadlock Prevention
Deadlock prevention is a set of strategies to ensure that at least one of the Coffman conditions
(Mutual Exclusion, Hold and Wait, No Preemption, Circular Wait) is not met, and hence, deadlocks
cannot occur. Here's how each of these conditions can be addressed:
a) Mutual Exclusion: This condition is often necessary for correct behavior, so it's hard to prevent.
However, careful design can sometimes limit the need for mutual exclusion.
b) Hold and Wait: This condition can be prevented by requiring that a process request all its
resources at once before it begins execution, or by requiring that a process release all its
resources before it requests another. Both strategies can be too restrictive and can lead to other
problems like low resource utilization.
c) No Preemption: This condition can be prevented by allowing resources to be forcibly taken from
a process if it requests another resource that is already held by another process. However, this
can be difficult to implement, especially if the resource has some internal state or data that
Deadlock Avoidance
Deadlock av0 1· d · .
ance 1s a dynamic strate th t · I •
state whenever a re . gy a invo ves making sure that a system remains in a safe
rocesses that f~~urce is requested. A safe state is one in which there exists a sequence of
P can in1sh even if all oth .
algorithm . II k ~r processes request their maximum resources. The Banker's
15
a we - nown deadlock avoidance algorithm
It is called "Banker" b · • l'k · · ·
. ecause its I e a bank deciding whether to grant a loan to a customer based on
avai lable resources and th • ·
e customers maximum needs, such that the bank remains solvent.
Banker's Algorithm
The Banker's Algorithm is a resource allocation and deadlock avoidance algorithm developed by
Edsger Dijkstra. It tests for safety by simulating the allocation of predefined maximum possible
amounts of all resources and then makes a state test to check for possible deadlock conditions for all
other pending activities, before deciding whether allocation should be allowed to continue.
. .
Example: · cD d
Example banker's algorithm with following snapshot of a system with resources A, B, 1
an
process PO to P4
Max
ABCD
PO 601 2
p11 7 5 2
p2 2 3 5 6
p3 16 5 3
p416 5 6
Allocation
ABCD
PO 4001
Pl 110 0
P212 5 4
P3 O 6 3 3
P40 212
Available
ABCD
3211
Max matrix:
Answer:
Calculate the Need matrix:
Need[i}U] = Max[i}[j] - Allocation[i]U]
ABCD
PO 2011
Pl O 6 5 2
P2110 2
P3 10 2 0
P4 l 444
After executing the algorithm, the safe sequence is: Pl-> P3 ->PO-> P2 -> P4.
Deadlock Detection
Deadlock detection involves periodically checking the system's state to see if a deadlock has
occurred. This is typically done using a wait-for graph, which is a directed graph where the nodes
represent processes, and the edges represent resource allocation and requests. If the graph contains
a cycle, a deadlock exists. The algorithm to detect a cycle in the graph can be the Depth First Search
{OFS) algorithm or similar algorithms. Deadlock detection is more permissive than deadlock
avoidance or prevention, as it allows more concurrency. However, it also requires additional
computation to detect deadlocks and possibly roll back or restart processes.
Device management
Devices and their Characteristics
Devices in computer systems are classified into different categories based on their characteristics.
They can be input devices, output devices, storage devices, communication devices, etc. Each device
has its own set of characteristics:
a) Device Speed: The speed at which a device can operate and transfer data. For example, a hard
drive will be slower than RAM.
b) Doto Rote: The rate at which data can be transferred between the device and the computer
system. For example, USB 3.0 has a higher data rate than USB 2.0.
c) Volatility: Whether the device retains data when power is turned off. RAM is volatile, while hard
drives are non-volatile.
d) Random vs. Sequential Access: Some devices allow random access of data {like hard drives),
while others are sequential (like tape drives).
e) Block vs. Character Devices: Block devices transfer data in blocks and are suitable for random
access. Character devices transfer data one character at a time and are suitable for sequential
access.
Device Drivers
Device drivers are software programs that communicate bNween thc> operating system and the
hardware devices. They provide a set of functions that the operating ~ystem can use to control the
device, such as reading, writing, initializing, t1nd manJging device ~pecific operations. Device drivers
serve as an abstraction layer bt'tween the hardware and the higher level software, making it easier
to write applications without worrying about the specifics of how each device works.
Device Handling
Device handling refers to how the operating system manages devices, allocates resources to them,
and schedules their use. The operating system uses a device management subsystem to keep track
of all the devices connected to the system, their types, status, and the drivers that control them. It
handles requests from applications to access devices, translates them into device-specific
commands, and manages any contention for resources.
the end,St~e~I it rtevSeCrAseNs ~::~~:n~isk arm moves in only one direction and jumps to the other
d) c-SCAN: 1m1 ar o ,
end when it reaches th~ ~nd of the disk. d C-SCAN that stop moving in a direction if there are no
e) LOOK and C-LOOK: Va nations of SCAN an
more requests in that direction.
Swap Space Management ·1 h Id data that doesn't fit into RAM. The
. k th tis used to temporan y o
Swap space is a space on dis a RAM and the swap space as needed. Swap sp~ce .
operating system swaps data between t to disk and which data to bnng back into
. . h. h data to swap ou ' . s
management involves deciding w ic ent and page replacement algorithms. ome
d virtual memory managem . .
RAM This is closely relate to . ace management include.
. 'th s used in swap sp . . I d
common page replacement algon m h 't been used for the longest time is rep ace .
U) Th page that asn
a) Least Recently Used (LR_ : o~dest page is replaced.
b) First-In, First-Out (FIFO). The
c) Optimal Page Replacement: The page that will not be used for the longest time in the future is
replaced . This is an idealized algorithm and is not implementable in practice.
di Clock: A variation of LRU that approximates the LRU algorithm using a circular list and a hand
that moves through it.