0% found this document useful (0 votes)
33 views

Lecture23 24

The document discusses deadlocks and the Banker's Algorithm for deadlock avoidance. It provides an example of how the Banker's Algorithm works on a system with 5 processes and 3 resource types. The algorithm determines if allocating resources to a requesting process would result in a safe state where all processes could terminate, or an unsafe state where deadlock could occur. It demonstrates running the algorithm on the example to show whether additional resource requests can be safely granted or must be denied.

Uploaded by

numan jafar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Lecture23 24

The document discusses deadlocks and the Banker's Algorithm for deadlock avoidance. It provides an example of how the Banker's Algorithm works on a system with 5 processes and 3 resource types. The algorithm determines if allocating resources to a requesting process would result in a safe state where all processes could terminate, or an unsafe state where deadlock could occur. It demonstrates running the algorithm on the example to show whether additional resource requests can be safely granted or must be denied.

Uploaded by

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

 A set of blocked processes each holding a

resource and waiting to acquire a resource


held by another process in the set.
 Example
◦ Consider a system with three CD RVV drives.
◦ Suppose each of three processes holds one of these
CD RW drives.
◦ If each process now requests another drive, the
three processes will be in a deadlock state.
◦ Each is waiting for the event "CD RVV is released,"
which can be caused only by one of the other
waiting processes.
 Traffic only in one direction.
 The bridge can be viewed as a resource and the cars
as processes.
 If a deadlock occurs, it can be resolved if one car
backs up (preempt resources and rollback).
 Several cars may have to be backed up if a deadlock
occurs.
 Starvation is possible.
 Banker's algorithm is a resource allocation and deadlock
avoidance algorithm developed for the safe allocation of
predetermined maximum possible amounts of all resources.

 The Banker's algorithm is run by the operating system whenever a


process requests resources.

 The algorithm avoids deadlock by denying or postponing the


request if it determines that accepting the request could put the
system in an unsafe state (one where deadlock could occur).
 For the Banker's algorithm to work, it needs to know three things:

1. How much of each resource each process could possibly request [Max]
2. How much of each resource each process is currently holding
[ALLOCATED]
3. How much of each resource the system currently has available
[AVAILABLE]

 Resources may be allocated to a process only if it satisfies the


following conditions:

 request ≤ max, else set error condition as process has crossed


maximum claim made by it.
 request ≤ available, else process waits until resources are
available.
 A state is considered safe if it is possible for all processes to finish
executing.

 Since the system cannot know when a process will terminate, or


how many resources it will have requested by then, the system
assumes that all processes will eventually attempt to acquire their
stated maximum resources and terminate soon afterward.

 The algorithm determines if a state is safe by trying to find a


hypothetical set of requests by the processes that would allow
each to acquire its maximum resources and then terminate
(returning its resources to the system). Any state where no such
set exists is an unsafe state.
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].
1. Let Work and Finish be vectors of length m
and n, respectively. Initialize:
Work = Available
Finish [i] = false for i - 1,3, …, n.
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.
 5 processes P0 through P4; 3 resource types A
(10 instances), B (5 instances, and C (7 instances).

 Snapshot at time T0:


Allocation Max Available
ABC ABC ABC
P0 0 1 0 7 5 3 332
P1 2 0 0 3 2 2
P2 3 0 2 902
P3 2 1 1 222
P4 0 0 2 4 3 3
 The content of the matrix. Need is defined to be
Max – Allocation.
Need
ABC
P0 7 4 3
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.HOW?
 For P1
 Available resources – P1(Need)
 <3,3,2> - <1,2,2> = <2,1,0>

 So P1’s request is satisfied and after using the resources it returns


the resources to system.
 Available = <2,1,0> + P1(Max) = <2,1,0> + <3,2,2> = <5,3,2>

 For P3
 Available resources – P3(Need)
 <5,3,2> - <0,1,1> = <5,2,1>

 So P3’s request is satisfied and after using the resources it returns


the resources to system.
 Available = <5,2,1> + P3(Max) = <5,2,1> + <2,2,2> = <7,4,3>
 For P4
 Available resources – P4(Need)
 <7,4,3> - <4,3,1> = <3,1,2>

 So P4’s request is satisfied and after using the resources it


returns the resources to system.
 Available = <3,1,2> + P4(Max) = <3,1,2> + <4,3,3> =
<7,4,5>

 For P2
 Available resources – P2(Need)
 <7,4,5> - <6,0,0> = <1,4,5>

 So P2’s request is satisfied and after using the resources it


returns the resources to system.
 Available = <1,4,5> + P2(Max) = <1,4,5> + <9,0,2> =
<10,4,7>
 For P0
 Available resources – P0(Need)
 <10,4,7> - <7,4,3> = <3,0,4>

 So P0’s request is satisfied and after using the resources it


returns the resources to system.
 Available = <3,0,4> + P0(Max) = <3,0,4> + <7,5,3> =
<10,5,7>
 Suppose now that process P1 requests one additional instance of
resource type A and two instances of resource type C, so Request
= (1,0,2).
 First we check that Request < Available i-e <1,0,2> < <3,3,2>,
which is true.

 We then pretend that this request has been fulfilled, and we arrive
at the following new state:
Allocation Need Available
ABC ABC ABC
P0 0 1 0 743 230
P1 302 020
P2 301 600
P3 211 011
P4 002 431
 We must determine whether this new system state is safe.

 To do so, we execute our safety algorithm and find that the


sequence <P1, P3, P4, P0, P2> satisfies the safety requirement.
Hence, we can immediately grant the request of process P1.

 However when the system is in this state, a request for (3,3,0) by


P4 cannot be granted, since the resources are not available.

 Furthermore, a request for (0,2,0) by P0 cannot be granted, even


though the resources are available, since the resulting state is
unsafe.

You might also like