0% found this document useful (0 votes)
17 views13 pages

Lecture 18

The document discusses deadlock management techniques, specifically deadlock avoidance algorithms such as the resource-allocation graph and the banker’s algorithm. It details the data structures used in the banker’s algorithm, including matrices for available resources, maximum claims, allocations, and needs. Additionally, it provides examples and scenarios for applying the banker’s algorithm and resource allocation strategies.

Uploaded by

divyanshujain176
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)
17 views13 pages

Lecture 18

The document discusses deadlock management techniques, specifically deadlock avoidance algorithms such as the resource-allocation graph and the banker’s algorithm. It details the data structures used in the banker’s algorithm, including matrices for available resources, maximum claims, allocations, and needs. Additionally, it provides examples and scenarios for applying the banker’s algorithm and resource allocation strategies.

Uploaded by

divyanshujain176
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/ 13

Lecture 18

Deadlock Management (Cont.)


Deadlock Avoidance Algorithms

• Single instance of a resource type


• Use a resource-allocation graph

• Multiple instances of a resource type


• Use the banker’s algorithm
Resource-Allocation Graph Algorithm

• Suppose that process Pi requests a resource Rj

• The request can be granted only if converting


the request edge to an assignment edge does not
result in the formation of a cycle in the resource
allocation graph
Banker’s Algorithm

• Multiple instances

• Each process must have a priori claim maximum use.

• When a process requests a resource it may have to wait

• When a process gets all its resources it must return them in a finite
amount of time
Data Structures for the Banker’s Algorithm
Let n = number of processes, and m = number of resources types.

• Available: Vector of length m. If available [j] = k, there are k instances of


resource type Rj available

• Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k


instances of resource type Rj
• Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently
allocated k instances of Rj
• Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of
Rj to complete its task
Need [i,j] = Max[i,j] – Allocation [i,j]
Safety Algorithm
1.Let Work and Finish be vectors of length m and n, respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1

2.Find an i such that both:


(a) Finish [i] = false
(b) Needi  Work
If no such i exists, go to step 4

3. Work = Work + Allocationi


Finish[i] = true
go to step 2

4. If Finish [i] == true for all i, then the system is in a safe state
Resource-Request Algorithm for Process Pi

Requesti = request vector for process Pi. If Requesti [j] = k


then process Pi wants k instances of resource type Rj
1. If Requesti  Needi go to step 2. Otherwise, raise error condition,
since process has exceeded its maximum claim
2. If Requesti  Available, go to step 3. Otherwise Pi must wait, since
resources are not available
3. Pretend to allocate requested resources to Pi by modifying the state
as follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
 If safe  the resources are allocated to Pi
 If unsafe  Pi must wait, and the old resource-allocation state is restored
Example of Banker’s Algorithm
• 5 processes P0 through P4;
3 resource types:
A (10 instances), B (5instances), and C (7 instances)
• Snapshot at time T0:
Allocation Max Available
ABC ABC ABC
P0 0 1 0 753 332
P1 2 0 0 322
P2 3 0 2 902
P3 2 1 1 222
P4 0 0 2 433
Find the safe sequence:

Processes Allocation A B C Max A B C Available A B C

P0 112 433 210


P1 212 322
P2 401 902
P3 020 753
P4 112 112
Consider the following scenario of resource allocation to find the safe sequence, if available (Steps Required)

Allocation Max Available


A B C D A B C D A B C D
P0 0 0 1 2 0 0 1 2 1 5 2 0
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
A single processor system has three resource types X, Y and Z, which are shared
by three processes. There are 5 units of each resource type. Consider the following
scenario, where the column Alloc denotes the number of units of each resource
type allocated to each process, and the column Need denotes the additional
number of units of each resource type requested by a process in order to complete
execution. Which of these processes will finish LAST?

Alloc Need
X Y Z X Y Z
P0 1 2 1 1 0 3
P1 2 0 1 0 1 2
P2 2 2 1 1 2 0
An operating system uses the banker’s algorithm for deadlock
avoidance when managing the allocation of three resource types X, Y, and Z to three
processes P0, P1, and P2. The table given below presents the current system state.
Here, the Allocation matrix shows the current number of resources of each type
allocated to each process and the Max matrix shows the maximum number of
resources of each type required by each process during its execution.
There are 3 units of type X, 2 units of type Y and 2 units of type Z still available. Is
the system is currently in safe state.
Allocation Max
X Y Z X Y Z
P0 0 0 1 8 4 3
P1 3 2 0 6 2 0
P2 2 1 1 3 3 3
Execute the Round Robin Scheduling algorithm with q=4
Pid BT AT

P1 5 0
P2 6 2
P3 3 4
P4 1 1
P5 5 6
P6 4 8

You might also like