




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Operating Systems is necessary course in Computer Science. Its about threading, process scheduling, deadlocks, memory management etc. This lecture includes: Deadlock, Avoidance, Resource, Allocation, Algorithm, Edge, Process, Dash, Safety, Sequence
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Operating Systems Lecture No. 28
Chapter 8 of the textbook Lecture 28 on Virtual TV
Deadlock avoidance Banker’s algorithms Safety algorithm Safe Sequence
Resource Allocation Graph Algorithm
In addition to the request and assignment edges explained in the previous lectures, we introduce a new type of edge called a claim edge to resource allocation graphs. A claim edge Pi Rj indicates that process Pi may request resource Rj at some time in the future. A dashed line is used to represent a claim edge. When Pi requests resource Rj the claim edge is converted to a request edge. In the following resource allocation graph, the edge P 2 R 2 is a claim edge.
Suppose that Pi requests resource Rj. The request can be granted only if converting the request edge P (^) i Rj into an assignment edge Rj Pi does not result in the formation of a cycle. If no cycle exists, then the allocation of the resource will leave the system in a safe state. If a cycle is found, then the allocation will put the system in an unsafe state. The following resource allocation graph shows that the system is in an unsafe state:
Banker’s Algorithm In this algorithm, when a new process enters the system, it must declare the maximum number of instances of each resource type that it may need, i.e., each process must a priori claim maximum use of various system resources. This number may not exceed the total number of instances of resources in the system, and there can be multiple instances of resources. When a process requests a set of resources, the system must determine whether the allocation of these resources will leave the system in a safe state. If it will, the resources are allocated; otherwise the process must wait until some other process releases enough resources. We say that a system is in a safe state if all of the processes in the system can be executed to termination in some order; the order of process termination is called safe sequence. When a process gets all its resources, it must use them and return them in a finite amount of time. Let n be the number of processes in the system and m be the number of resource types. We need the following data structures in the Banker’s algorithm:
Available: A vector of length m indicates the number of available instances of resources of each type. Available[j] = = k means that there are k available instances of resource Rj. Max: An n x m matrix defines the maximum demand of resources of each process. Max[i,j] = = k means that process P (^) i may request at most k instances of resource Rj. Allocation: An n x m matrix defines the number of instances of resources of each type currently allocated to each process. Allocation[i,j] = = k means that Pi is currently allocated k instances of resource type Rj. Need: An n x m matrix indicates the remaining resource need of each process. Need[i,j] = = k means that Pi may need k more instances of resource type Rj to complete its task. Note that Need[i,j] = = Max[i,j] - Allocation[i,j].
The content of the matrix Need is defined to be Max- Allocation and is:
Need A B C P 0 7 4 3 P 1 1 2 2 P 2 6 0 0 P 3 0 1 1 P 4 4 3 1
In the following sequence of snapshots, we show execution of the Safety algorithm for the given system state to determine if the system is in a safe state. We progressively construct a safe sequence.
Allocation Need Available A B C A B C A B C P 0 0 1 0 7 4 3 3 3 2 P 1 2 0 0 1 2 2 5 3 2 P 2 3 0 2 6 0 0 P 3 2 1 1 0 1 1 P 4 0 0 2 4 3 1 Safe Sequence: < P 1 >
Allocation Need Available A B C A B C A B C P 0 0 1 0 7 5 3 3 3 2 P 1 2 0 0 1 2 2 5 3 2 P 2 3 0 2 6 0 0 7 4 3 P 3 2 1 1 0 1 1 P 4 0 0 2 4 3 1 Safe Sequence: < P1, P 3 >
Allocation Need Available A B C A B C A B C P 0 0 1 0 7 5 3 3 3 2 P 1 2 0 0 1 2 2 5 3 2 P 2 3 0 2 6 0 0 7 4 3 P 3 2 1 1 0 1 1 7 4 5 P 4 0 0 2 4 3 1 Safe Sequence: < P1, P3, P 4 >
Allocation Need Available A B C 7 5 3 A B C P 0 0 1 0 1 2 2 3 3 2
Safe Sequence: < P1, P3, P4, P 0 >
The Safety algorithm concludes that the system is in a safe state, with < P 0 , P 1 , P 2 , P 3 , P 4 > being a safe sequence. Suppose now that process P 1 requests one additional instance of resource type A and two instances of resource type C so Request 1 = (1, 0, 2). To decide whether this request can be fulfilled immediately, we invoke Banker’s algorithm, which first check that Request 1 <= Available, which is true because (1,0,2)<=(3,3,2). It then pretends that this request has been fulfilled, and arrives at the following state:
Allocation Need Available A B C A B C A B C P 0 0 1 0 7 4 3 2 3 0 P 1 3 0 2 0 2 0 P 2 3 0 2 6 0 0 P 3 2 1 1 0 1 1 P 4 0 0 2 4 3 1
Banker’s algorithm then executes the Safety algorithm to determine if the resultant system will be in a safe state. Here is the complete working of Banker’s algorithm. If P 1 requests (1,0,2), lets evaluate if this request may be granted immediately. Banker’s algorithm takes the following steps.
It then pretends that request is granted and updates the various data structures accordingly. It then invokes the Safety algorithm to determine if the resultant state is safe. Here is sequence of steps taken by the Safety algorithm. The algorithm progressively constructs a safe sequence.
Need Allocation Available A B C A B C A B C P 0 7 4 3 0 1 0 3 3 2 P 1 0 2 0 3 0 2 P 2 6 0 0 3 0 2 P 3 0 1 1 2 1 1 P 4 4 3 1 0 0 2
Hence executing Safety algorithm shows that sequence <P1, P3, P4, P0, P2> satisfies the safety requirement and so P 1 ’s request may be granted immediately. Note that safe sequence is not necessarily a unique sequence. There are several safe sequences for the above example. See lecture slides for more details. Here is another example. P 0 requests (0,2,0). Should this request be granted? In order to answer this question, we again follow Banker’s algorithm as shown in the following sequence of steps.
Need Allocation Available A B C A B C A B C P 0 7 4 3 0 1 0 3 3 2 P 1 1 2 2 2 0 0 P 2 6 0 0 3 0 2 P 3 0 1 1 2 1 1 P 4 4 3 1 0 0 2 The following is the updated system state. We run the Safety algorithm on this state now and show the steps of executing the algorithm.
Need Allocation Work A B C A B C A B C P 0 7 4 3 0 1 0 3 1 2 P 1 1 2 2 2 0 0 P 2 6 0 0 3 0 2 P 3 0 1 1 2 1 1 P 4 4 3 1 0 0 2
Need Allocation Work A B C A B C A B C P 0 7 2 3 0 3 0 3 1 2 P 1 1 2 2 2 0 0 5 2 3 P 2 6 0 0 3 0 2 P 3 0 1 1 2 1 1 P 4 4 3 1 0 0 2 Safe Sequence: <P 3 >
Need Allocation Work A B C A B C A B C P 0 7 2 3 0 3 0 3 1 2 P 1 1 2 2 2 0 0 5 2 3 P 2 6 0 0 3 0 2 7 2 3 P 3 0 1 1 2 1 1
Safe Sequence: <P3, P 1 >
Need Allocation Work A B C A B C A B C P 0 7 2 3 0 3 0 3 1 2 P 1 1 2 2 2 0 0 5 2 3 P 2 6 0 0 3 0 2 7 2 3 P 3 0 1 1 2 1 1 10 2 5 P 4 4 3 1 0 0 2 Safe Sequence: <P3, P1, P 2 > Need Allocation Work A B C A B C A B C P 0 7 2 3 0 3 0 3 1 2 P 1 1 2 2 2 0 0 5 2 3 P 2 6 0 0 3 0 2 7 2 3 P 3 0 1 1 2 1 1 10 2 5 P 4 4 3 1 0 0 2 10 5 5 Safe Sequence: <P3, P1, P2, P0, P 4 >
Hence executing the safety algorithm shows that sequence <P3, P1, P2, P0, P 4 > satisfies safety requirement. And so P 0 ’s request may be granted immediately. Suppose P 0 requests (0,2,0). Can this request be granted after granting P 1 ’s request of (1,0,2)?