{"id":173,"date":"2018-07-19T11:07:42","date_gmt":"2018-07-19T11:07:42","guid":{"rendered":"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=173"},"modified":"2018-08-07T10:22:02","modified_gmt":"2018-08-07T10:22:02","slug":"deadlocks-avoidance","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/chapter\/deadlocks-avoidance\/","title":{"rendered":"Deadlocks \u2013 Avoidance"},"content":{"raw":"<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>18.1 Introduction<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial\"><span style=\"font-size: 1em\">In a <\/span>multi-programming<span style=\"font-size: 1em\">\u00a0environment, many processes compete for a finite number of resources. If the resources requested by a process are not available, the process waits. If the resources requested by this process are held by other waiting processes, the situation may lead to a deadlock. The different ways to handle deadlocks are deadlock prevention, deadlock avoidance, deadlock detection and recovery. In the previous module, we learnt different ways to prevent deadlocks. In this module, we shall see how deadlocks are avoided.<\/span><\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">In the previous module, we learnt that a system with resources can be depicted using a resource-allocation graph. In a resource-allocation graph, processes are denoted using circles and resource types are denoted using rectangles. The different instances of each resource type are shown as dots within the rectangles. Processes and resource types are the nodes of the resource-allocation graph. The edges show the dependency between the processes and the resources. If there is an edge from a process to a resource, it means that the process is requesting for an instance of the resource (request edge). If there is an edge from a resource to a process, it means that an instance of the resource is assigned to the process (assignment edge).<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">18.2. Resource-Allocation Graph Algorithm for Deadlock Avoidance\u00a0<\/strong><\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n<img class=\"size-full wp-image-178 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Deadlock-Avoidance.jpg\" alt=\"\" width=\"312\" height=\"215\" \/>\r\n<p style=\"text-align: center\"><strong>Fig. 18.1 Resource-Allocation Graph for Deadlock Avoidance (Source: [1])<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">A variant of the resource-allocation graph is used to avoid deadlocks when the\u00a0<span style=\"font-size: 1em;text-align: initial\">system has only one instance of a resource type. In this graph, a new type of edge is added called the claim edge. A claim edge is represented by a dashed line as shown in Figure 18.1. A claim edge <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">i \u00ae <\/span><em style=\"font-size: 1em;text-align: initial\">R<\/em><span style=\"font-size: 1em;text-align: initial\">j indicates that process <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">j will request resource <\/span><em style=\"font-size: 1em;text-align: initial\">R<\/em><span style=\"font-size: 1em;text-align: initial\">j. All claim edges must appear in the graph, initially. Resources must be claimed <\/span><em style=\"font-size: 1em;text-align: initial\">a priori <\/em><span style=\"font-size: 1em;text-align: initial\">in the system. That is, at the beginning, the system should know the future requests of all the processes. These requests that would be made in future are depicted using claim edges.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">A claim edge <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i \u00ae \u00a0<\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">j is converted to a request edge when process <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i actually requests for resource <\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">j. When resource <\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">j is released by process <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i, the assignment edge <\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">i \u00ae <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i reconverts to a claim edge <\/span><em style=\"font-size: 1em\">P<\/em><em style=\"font-size: 1em\">i <\/em><span style=\"font-size: 1em\">\u00ae <\/span><em style=\"font-size: 1em\">R<\/em><em style=\"font-size: 1em\">j<\/em><span style=\"font-size: 1em\">.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">We will now learn how deadlocks can be avoided using this type of resource-allocation graphs. Suppose <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i requests <\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">j. The resource-allocation graph would have had a claim edge from <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i to <\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">j. The claim edge is now converted to a request edge <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i \u00ae <\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">j. The request edge <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i \u00ae <\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">j is converted to an assignment edge <\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">j \u00ae <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i. The resource-allocation graph now depicts how the state of the system would have been after the resource <\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">j is allocated to <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i. The resource-allocation graph is now checked for cycles using the cycle-detection algorithm. If there is no cycle, the system is in a safe state and the resource can be allocated to <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i. If there is a cycle, process <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i has\u00a0to wait. <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i is not allocated the resource <\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">j immediately.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">Thus, the resource-allocation graph alone is modified and the system is checked for safe state. The actual allocation of resource is not done immediately. Only if the allocation would result in a safe state, the actual allocation of resources is done.<\/span><\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n<img class=\"size-full wp-image-177 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Allocation-Graph-1.jpg\" alt=\"\" width=\"332\" height=\"216\" \/>\r\n<p style=\"text-align: center\">Fig. 18.2 Unsafe State In Resource-Allocation Graph (Source: [1])<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Consider the resource-allocation graph in Figure 18.2. <em>P<\/em>2 requests for resource <em>R<\/em>2 as shown using the claim edge from <em>P<\/em>2 to <em>R<\/em>2 in Figure 18.1. This is converted to an assignment edge in the resource-allocation graph as shown in Figure 18.2. The graph is now checked for cycles. Since there is a cycle, the system understands that allocating\u00a0<em style=\"font-size: 1em;text-align: initial\">R<\/em><span style=\"font-size: 1em;text-align: initial\">2 to <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">2 will lead to a deadlock. The only instance of <\/span><em style=\"font-size: 1em;text-align: initial\">R<\/em><span style=\"font-size: 1em;text-align: initial\">1 is assigned to <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1 and the only instance of <\/span><em style=\"font-size: 1em;text-align: initial\">R<\/em><span style=\"font-size: 1em;text-align: initial\">2 is assigned to <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">2. <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">2 has requested for <\/span><em style=\"font-size: 1em;text-align: initial\">R<\/em><span style=\"font-size: 1em;text-align: initial\">1. It is known from the claim edge that process <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1 may request for <\/span><em style=\"font-size: 1em;text-align: initial\">R<\/em><span style=\"font-size: 1em;text-align: initial\">2. If <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1 requests for <\/span><em style=\"font-size: 1em;text-align: initial\">R<\/em><span style=\"font-size: 1em;text-align: initial\">2 (before releasing <\/span><em style=\"font-size: 1em;text-align: initial\">R<\/em><span style=\"font-size: 1em;text-align: initial\">1, then both the processes <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1 and <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">2 will wait for a resource held by the other process. The system will go to a deadlocked state. Therefore, <\/span><em style=\"font-size: 1em;text-align: initial\">R<\/em><span style=\"font-size: 1em;text-align: initial\">2 is not allocated to <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">2. Thus, deadlocks are avoided.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">This method can be used to avoid deadlocks only if there is a single instance of each resource type. If there are more instances of each resource type, Banker\u2019s algorithm is used for deadlock avoidance.<\/span><\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>18.3\u00a0 Banker\u2019s Algorithm<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the Banker\u2019s algorithm, each process must a priori declare its maximum use. This should be done for any deadlock avoidance algorithm. The maximum resource need of each process must not exceed the total number of resources in the system. When a user requests for resources, the system checks whether the allocation of the request will result in a safe state. If, after the allocation, the system will be in a safe state, resources are allocated. Else, the process waits. When a process gets all its resources it must return them in a finite amount of time.<\/p>\r\n&nbsp;\r\n\r\n<strong>18.3.1\u00a0 Data Structures for the Banker\u2019s Algorithm\u00a0<\/strong>\r\n\r\n&nbsp;\r\n\r\nLet <em>n <\/em>denote the number of processes and <em>m <\/em>denote the number of resource types.\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0\u00a0<em>Available: <\/em>Vector of length <em>m<\/em>. If Available[<em>j<\/em>] = <em>k<\/em>, there are <em>k \u00a0<\/em>instances \u00a0of resource type <em>R<\/em><em>j <\/em>available.\r\n<p style=\"padding-left: 240px\"><em>A B C<\/em><\/p>\r\n<p style=\"padding-left: 240px\">3 3 2<\/p>\r\n<p style=\"text-align: justify\">In the example shown above, there are three resource types A, B and C. The number of available instances of resource types A, B and C are 3, 3 and 2 respectively.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022\u00a0<em>Max: n x m <\/em>matrix. The <em>n <\/em>rows correspond to <em>n <\/em>processes and the <em>m <\/em>columns correspond to the <em>m <\/em>resource types. If Max[<em>i, j<\/em>] = <em>k<\/em>, then process <em>P<\/em><em>i <\/em>may request at most <em>k <\/em>instances of resource type <em>R<\/em><em>j<\/em><\/p>\r\n\r\n<table class=\"aligncenter\" style=\"width: 100px\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 181.063px\"><\/td>\r\n<td style=\"width: 199.063px;padding-left: 30px\"><em>A B C<\/em><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 181.063px;text-align: right\"><em>P<\/em>0<\/td>\r\n<td style=\"width: 199.063px;padding-left: 30px\">7 5 3<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 181.063px;text-align: right\"><em>P<\/em>1<\/td>\r\n<td style=\"width: 199.063px;padding-left: 30px\">3 2 2<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 181.063px;text-align: right\"><em>P<\/em>2<\/td>\r\n<td style=\"width: 199.063px;padding-left: 30px\">9 0 2<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 181.063px;text-align: right\"><em>P<\/em>3<\/td>\r\n<td style=\"width: 199.063px;padding-left: 30px\">2 2 2<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the example shown above, there are three resource types A, B and C and four processes <em>P<\/em>0, <em>P<\/em>1, <em>P<\/em>2 and <em>P<\/em>3. The maximum need of process <em>P<\/em>0 can\u00a0 be 7 instances of resource type A, 5 instances of resource type B and 3 instances of\u00a0<span style=\"font-size: 1em;text-align: initial\">resource type C. Similarly, the maximum need of the other processes <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1, <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">2 and\u00a0<\/span><em style=\"text-align: initial;font-size: 1em\">P<\/em><span style=\"text-align: initial;font-size: 1em\">3 are also shown.<\/span><\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0<em>Allocation: n <\/em>x <em>m <\/em>matrix. The <em>n <\/em>rows correspond to <em>n <\/em>processes and the <em>m <\/em>columns correspond to the <em>m <\/em>resource types. If Allocation[<em>i,j<\/em>] = <em>k <\/em>then process <em>P<\/em><em>i <\/em>is currently allocated <em>k <\/em>instances of <em>R<\/em><em>j.<\/em><\/p>\r\n\r\n<table class=\"aligncenter\" style=\"width: 288px\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 176.063px\"><\/td>\r\n<td style=\"padding-left: 30px;width: 180.531px\"><strong><em>A B C<\/em><\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"text-align: right;width: 176.063px\"><em>P<\/em>0<\/td>\r\n<td style=\"padding-left: 30px;width: 180.531px\">0 1 0<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"text-align: right;width: 176.063px\"><em>P<\/em>1<\/td>\r\n<td style=\"padding-left: 30px;width: 180.531px\">2 0 0<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"text-align: right;width: 176.063px\"><em>P<\/em>2<\/td>\r\n<td style=\"padding-left: 30px;width: 180.531px\">3 0 2<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"text-align: right;width: 176.063px\"><em>P<\/em>3<\/td>\r\n<td style=\"padding-left: 30px;width: 180.531px\">2 1 1<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the example shown above, there are three resource types A, B and C and four processes <em>P<\/em>0, <em>P<\/em>1, <em>P<\/em>2 and <em>P<\/em>3. The number of instances of resource type currently allocated to each process is shown. Process <em>P<\/em>0 is currently allocated 0 instances of resource type A, 1 instance of resource type B and 0 instances of resource type C. Similarly, the current allocation of the other processes <em>P<\/em>1, <em>P<\/em>2 and <em>P<\/em>3 are also shown.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0<em>Need: n <\/em>x <em>m <\/em>matrix. The <em>n <\/em>rows correspond to <em>n <\/em>processes and the <em>m <\/em>columns correspond to the <em>m <\/em>resource types. This matrix shows the future need of each process. If <em>Need<\/em>[<em>i, j<\/em>] = <em>k<\/em>, then <em>P<\/em><em>i <\/em>may need <em>k <\/em>more instances of <em>R<\/em><em>j <\/em>to complete its task.<\/p>\r\n&nbsp;\r\n<p style=\"padding-left: 30px\"><em>Need <\/em>[<em>i, j] <\/em>= <em>Max<\/em>[<em>i, j<\/em>] \u2013 <em>Allocation <\/em>[<em>i, j<\/em>]<\/p>\r\n&nbsp;\r\n\r\nThat is, future need is a process\u2019s maximum need minus the resources currently allocated.\r\n\r\n&nbsp;\r\n\r\n<strong>18.3.2\u00a0 Notations Used:\u00a0<\/strong>\r\n\r\n&nbsp;\r\n\r\nIf <em>X <\/em>and <em>Y <\/em>are vectors of length <em>n<\/em>\r\n<ul>\r\n \t<li><em>X <\/em>\u2264 <em>Y <\/em>iff <em>X<\/em>[<em>i<\/em>] \u2264 <em>Y<\/em>[<em>i<\/em>] for all <em>i <\/em>= 1,2,\u2026,<em>n<\/em><\/li>\r\n<\/ul>\r\n<p style=\"padding-left: 90px\">That is, the ith element of vector X is less than or equal to the ith element of vector Y, for all i.<\/p>\r\n<p style=\"padding-left: 60px\">Consider the following vectors X and Y<\/p>\r\n\r\n<ul>\r\n \t<li>If <em>X <\/em>= (1,7,3,2), <em>Y <\/em>= (0,3,2,1)<\/li>\r\n<\/ul>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the above <em style=\"text-align: initial;font-size: 1em\">Y <\/em><span style=\"text-align: initial;font-size: 1em\">\u2264 <\/span><em style=\"text-align: initial;font-size: 1em\">X<\/em><span style=\"text-align: initial;font-size: 1em\">, because each element of vector Y is less than or equal to the corresponding element of vector X. That is, the first element of Y is less than the first element of X and so on.<\/span><\/p>\r\n&nbsp;\r\n\r\n<em>Y &lt;\u00a0\u00a0\u00a0 X, <\/em>if <em>Y <\/em>\u2264 <em>X <\/em>and <em>Y<\/em>\u2260 <em>X<\/em>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Each row in the matrices Allocation and Need is treated as a vector. For example, the <em>i<\/em>th\u00a0 row of matrix Allocation and Need are\u00a0 referred to as Allocation<em>i\u00a0 <\/em>and Need<em>i <\/em>respectively.<\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">The Banker\u2019s algorithm comprises a safety algorithm and a resource-allocation algorithm. For a given state of the system, the safety algorithm checks if the system is in a safe state. The safety algorithm is given below:<\/span><\/p>\r\n\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n\r\n<strong>18.3.3\u00a0 Safety Algorithm\u00a0<\/strong>\r\n\r\n&nbsp;\r\n\r\n1.\u00a0 Let <em>Work <\/em>and <em>Finish <\/em>be vectors of length <em>m <\/em>and <em>n <\/em>respectively.\u00a0\u00a0 Initialize:\r\n<p style=\"padding-left: 60px\"><em>Work <\/em>= <em>Available<\/em><\/p>\r\n<p style=\"padding-left: 60px\"><em>Finish <\/em>[<em>i<\/em>] = <em>false <\/em>for <em>i <\/em>= 1,2, \u2026, <em>n<\/em><\/p>\r\n2.\u00a0 Find an <em>i <\/em>such that both:\r\n<p style=\"padding-left: 60px\">(a)\u00a0 <em>Finish <\/em>[<em>i<\/em>] = <em>false<\/em><\/p>\r\n<p style=\"padding-left: 60px\">(b)\u00a0 <em>Need<\/em><em>i <\/em>\u00a3 <em>Work<\/em><\/p>\r\n<p style=\"padding-left: 60px\">If no such <em>i <\/em>exists, go to step 4.<\/p>\r\n3.\u00a0 W<em>ork <\/em>= <em>Work <\/em>+ <em>Allocation<\/em><em>i <\/em>\r\n<p style=\"padding-left: 60px\"><em>Finish<\/em>[<em>i<\/em>] = <em>true<\/em><\/p>\r\n<p style=\"padding-left: 60px\">go to step 2.<\/p>\r\n4.\u00a0 If <em>Finish <\/em>[<em>i<\/em>] == true for all <em>i<\/em>, then the system is in a safe state.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Whenever a request for resources comes from a process, the system runs the resource-request algorithm and modifies the state of the system in the simulation as if the resource is allocated. The actual state of the system is not modified; the data structures in the algorithm alone are modified. The safety algorithm is run to check if the system still remains in a safe state. The resource-request algorithm for process <em>P<\/em>i is given below:<\/p>\r\n&nbsp;\r\n\r\n<strong>18.3.4\u00a0 Resource-Request Algorithm for Process <em>P<\/em><em>i<\/em>\u00a0<\/strong>\r\n\r\n&nbsp;\r\n\r\n<em>Request<\/em><em>i <\/em>= request vector for process <em>P<\/em><em>i<\/em>. If <em>Request<\/em><em>i <\/em>[<em>j<\/em>] = <em>k <\/em>then process <em>P<\/em><em>i <\/em>wants <em>k\u00a0<\/em>instances of resource type <em>R<\/em><em>j<\/em>.\r\n\r\n&nbsp;\r\n\r\n1. If <em>Request<\/em><em>i <\/em>\u00a3 <em>Need<\/em><em>i <\/em>go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim.\r\n\r\n2. If <em>Request<\/em><em>i <\/em>\u00a3 <em>Available<\/em>, go to step 3. Otherwise <em>P<\/em><em>i\u00a0\u00a0\u00a0\u00a0 <\/em>must wait, since resources are not available.\r\n\r\n3. Pretend to allocate requested resources to <em>P<\/em><em>i <\/em>by modifying the state as follows:\r\n\r\n&nbsp;\r\n\r\n<em>Available <\/em>= <em>Request<\/em><em>i<\/em><em>;<\/em>\r\n\r\n<em>Allocation<\/em><em>i <\/em>= <em>Allocation<\/em><em>i <\/em>+ <em>Request<\/em><em>i<\/em>;\r\n\r\n<em>Need<\/em><em>i <\/em>= <em>Need<\/em><em>i <\/em>\u2013 <em>Request<\/em><em>i;;<\/em>\r\n\r\n&nbsp;\r\n\r\nThe safety algorithm is now run to check if the system is in a safe state with this modified state.\r\n\r\n&nbsp;\r\n\r\n<em>If safe <\/em>\u00de <em>the resources are allocated to P<\/em><em>i<\/em><em>.<\/em>\r\n\r\n<em>If unsafe <\/em>\u00de <em>P<\/em>i <em>m<\/em><em>ust wait, and the old resource-allocation state is restored<\/em>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">We will understand how the algorithm works by an example as given below.<\/span>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n\r\n<strong>18.3.5\u00a0 Example of Banker\u2019s Algorithm<\/strong>\r\n\r\n&nbsp;\r\n\r\nConsider 5 processes <em>P<\/em>0 \u00a0through <em>P<\/em>4; 3 resource types <em>A<\/em>, <em>B <\/em>and <em>C<\/em>. There are 10 instances of <em>A<\/em>, 5 instances of <em>B <\/em>and 7 instances of <em>C<\/em>.\r\n\r\n&nbsp;\r\n\r\nThe snapshot at time <em>T<\/em>0 is given below:\r\n<table class=\"aligncenter\" style=\"height: 105px;width: 308px\" border=\"1\">\r\n<tbody>\r\n<tr style=\"height: 15px\">\r\n<td style=\"height: 15px;width: 39.0625px\"><\/td>\r\n<td style=\"height: 15px;width: 126.063px\"><strong><em>Allocation<\/em><\/strong><\/td>\r\n<td style=\"height: 15px;width: 71.0625px\"><strong><em>M<\/em><\/strong><strong><em>ax<\/em><\/strong><\/td>\r\n<td style=\"height: 15px;width: 116.063px\"><strong><em>Available<\/em><\/strong><\/td>\r\n<\/tr>\r\n<tr style=\"height: 15px\">\r\n<td style=\"height: 15px;width: 39.0625px\"><\/td>\r\n<td style=\"height: 15px;width: 126.063px\"><strong><em>A B C<\/em><\/strong><\/td>\r\n<td style=\"height: 15px;width: 71.0625px\"><strong><em>A B C<\/em><\/strong><\/td>\r\n<td style=\"width: 116.063px\"><strong><em>A B C<\/em><\/strong><\/td>\r\n<\/tr>\r\n<tr style=\"height: 15px\">\r\n<td style=\"height: 15px;width: 39.0625px\"><strong><em>P<\/em><\/strong><strong>0<\/strong><\/td>\r\n<td style=\"height: 15px;width: 126.063px\"><strong>0 1 0<\/strong><\/td>\r\n<td style=\"height: 15px;width: 71.0625px\"><strong>7 5 3<\/strong><\/td>\r\n<td style=\"height: 15px;width: 116.063px\"><strong>3 3 2<\/strong><\/td>\r\n<\/tr>\r\n<tr style=\"height: 15px\">\r\n<td style=\"height: 15px;width: 39.0625px\"><strong><em>P<\/em><\/strong><strong>1<\/strong><\/td>\r\n<td style=\"height: 15px;width: 126.063px\"><strong>2 0 0<\/strong><\/td>\r\n<td style=\"height: 15px;width: 71.0625px\"><strong>3 2 2<\/strong><\/td>\r\n<td style=\"height: 15px;width: 116.063px\"><\/td>\r\n<\/tr>\r\n<tr style=\"height: 15px\">\r\n<td style=\"height: 15px;width: 39.0625px\"><strong><em>P<\/em><\/strong><strong>2<\/strong><\/td>\r\n<td style=\"height: 15px;width: 126.063px\"><strong>3 0 2<\/strong><\/td>\r\n<td style=\"height: 15px;width: 71.0625px\"><strong>9 0 2<\/strong><\/td>\r\n<td style=\"height: 15px;width: 116.063px\"><\/td>\r\n<\/tr>\r\n<tr style=\"height: 15px\">\r\n<td style=\"height: 15px;width: 39.0625px\"><strong><em>P<\/em><\/strong><strong>3<\/strong><\/td>\r\n<td style=\"height: 15px;width: 126.063px\"><strong>2 1 1<\/strong><\/td>\r\n<td style=\"height: 15px;width: 71.0625px\"><strong>2 2 2<\/strong><\/td>\r\n<td style=\"height: 15px;width: 116.063px\"><\/td>\r\n<\/tr>\r\n<tr style=\"height: 15px\">\r\n<td style=\"height: 15px;width: 39.0625px\"><strong><em>P<\/em><\/strong><strong>4<\/strong><\/td>\r\n<td style=\"height: 15px;width: 126.063px\"><strong>0 0 2<\/strong><\/td>\r\n<td style=\"height: 15px;width: 71.0625px\"><strong>4 3 3<\/strong><\/td>\r\n<td style=\"height: 15px;width: 116.063px\"><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\nThe content of the matrix Need is defined to be Max \u2013 Allocation. Matrix Need is given below:\r\n<p style=\"padding-left: 210px\"><em>Need <\/em><em>\u00a0A B C<\/em><\/p>\r\n<p style=\"padding-left: 210px\"><em>P<\/em>0\u00a0\u00a0\u00a0\u00a0\u00a0 7 4 3<\/p>\r\n<p style=\"padding-left: 210px\"><em>P<\/em>1\u00a0 \u00a0 \u00a0 \u00a01 2 2<\/p>\r\n<p style=\"padding-left: 210px\"><em>P<\/em>2\u00a0\u00a0\u00a0\u00a0\u00a0 6 0 0<\/p>\r\n<p style=\"padding-left: 210px\"><em>P<\/em>3\u00a0\u00a0\u00a0\u00a0\u00a0 0 1 1<\/p>\r\n<p style=\"padding-left: 210px\"><em>P<\/em>4\u00a0\u00a0\u00a0\u00a0\u00a0 4 3 1<\/p>\r\n&nbsp;\r\n\r\nWe will first check if the system is in a safe state in the current situation. For this, the safety algorithm is run.\r\n<p style=\"padding-left: 270px\"><em>Allocation<\/em><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Need\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Available<\/em><\/p>\r\n\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><em>\u00a0<\/em><\/td>\r\n<td><em>A B C<\/em><\/td>\r\n<td><em>A B C<\/em><\/td>\r\n<td><em>A B C<\/em><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><em>P<\/em>0<\/td>\r\n<td>0 1 0<\/td>\r\n<td>7 4 3<\/td>\r\n<td>3 3 2<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><em>P<\/em>1<\/td>\r\n<td>2 0 0<\/td>\r\n<td>1 2 2<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><em>P<\/em>2<\/td>\r\n<td>3 0 2<\/td>\r\n<td>6 0 0<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><em>P<\/em>3<\/td>\r\n<td>2 1 1<\/td>\r\n<td>0 1 1<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><em>P<\/em>4<\/td>\r\n<td>0 0 2<\/td>\r\n<td>4 3 1<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\nVector Work is initialized to Available Therefore, Work = Available = (3,3,2)\r\n\r\n&nbsp;\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">Finish[i] = false for i = 0,1,2,3,4 Let i = 0<\/span>\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">Is Need0 \u2264 Work?\u00a0\u00a0\u00a0 No, (7,4,3) is not less than or equal to (3,3,2)<\/span>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify;padding-left: 30px\">\r\n\r\nLet i = 1\r\n\r\nIs Need1 \u2264 Work?\u00a0\u00a0\u00a0 Yes, (1,2,2) is less than or equal to (3,3,2)\r\n\r\nTherefore, set Finish[1] = true. <em>P<\/em>1 added to the safe sequence &lt; <em>P<\/em>1&gt;\r\n\r\n<span style=\"text-align: initial;text-indent: 1em;font-size: 1em\">Is Need2 \u2264 Work?\u00a0\u00a0\u00a0 No, (6,0,0) is not less than or equal to (5,3,2)<\/span>\r\n\r\nWork = Work + Allocation1 = (3,3,2) + (2,0,0) = (5,3,2) Let i = 2\r\n\r\n&nbsp;\r\n\r\nLet i = 3\r\n\r\nIs Need3 \u2264 Work?\u00a0\u00a0\u00a0 Yes, (0,1,1) is less than or equal to (5,3,2)\r\n\r\nTherefore, set Finish[3] = true , <em>P<\/em>3 added to safe sequence &lt; <em>P<\/em>1, <em>P<\/em>3&gt;\r\n\r\nWork = Work + Allocation3 =(5,3,2) + (2,1,1) = (7,4,3)\r\n\r\n&nbsp;\r\n\r\nWork = (7,4,4)\r\n\r\nLet i = 4\r\n\r\nIs Need4 \u2264 Work? Yes, (4,3,1) is less than or equal to (7,4,3)\r\n\r\nSet Finish[4] = true , <em>P<\/em>4 is added to safe sequence &lt; <em>P<\/em>1 <em>,P<\/em>3<em>, P<\/em>4 &gt;\r\n\r\nWork = Work + Allocation4 = (7,4,3) + (0,0,2) = (7,4,5)\r\n\r\n&nbsp;\r\n\r\nWork = (7,4,5)\r\n\r\nNow, check again the processes that were not added to the safe sequence. <em>P<\/em>0\r\n\r\nand <em>P<\/em>2 are remaining.\r\n\r\n&nbsp;\r\n\r\nSo, let i = 0\r\n\r\nIs Need0 \u2264 Work?\u00a0\u00a0\u00a0 Yes\r\n\r\nSet Finish[0] = true, <em>P<\/em>0 is added to safe sequence &lt; <em>P<\/em>1 <em>,P<\/em>3<em>, P<\/em>4 <em>, P<\/em>0&gt;\r\n\r\nWork = Work + Allocation0 =(7,4,5) + (0,1, 0) = (7,5,5)\r\n\r\n&nbsp;\r\n\r\nWork = (7,5,5)\r\n\r\nLet i = 2\r\n\r\nIs Need2 \u2264 Work?\u00a0\u00a0\u00a0 Yes\r\n\r\nFinish[0] = true , <em>P<\/em>2 is added to safe sequence &lt;<em>P<\/em>1 <em>,P<\/em>3<em>, P<\/em>4 <em>, P<\/em>0 <em>, P<\/em>2 &gt;\r\n\r\nWork = Work + Allocation2 =(7,5,5) + (3,0,2) = (10,5,7)\r\n\r\n&nbsp;\r\n\r\nThe system is in a safe state since the sequence &lt; <em>P<\/em><sub>1<\/sub>, <em>P<\/em><sub>3<\/sub>, <em>P<\/em><sub>4<\/sub>, <em>P<\/em><sub>2<\/sub>, <em>P<\/em><sub>0<\/sub>&gt; satisfies the safety requirement\r\n\r\n&nbsp;\r\n\r\nNow, let <em>P<\/em><sub>1\u00a0<\/sub>Request (1,0,2)\r\n\r\n<\/div>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">First, check if Request1 \u00a3 Available (that is, (1,0,2) \u00a3 (3,3,2)) \u00de <\/span><em style=\"text-align: initial;font-size: 1em\">true.<\/em><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">Also, check if Request1 \u00a3 Need1 (that is (1,0,2) \u00a3 (1,2,2)) \u00de <\/span><em style=\"text-align: initial;font-size: 1em\">true.<\/em><\/p>\r\n\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now, the data structures are changed as if the request of <em>P<\/em>1 is granted. The future Need of <em>P<\/em>1 is changed from (1,2,2) to (0,2,0). The Allocation of <em>P<\/em>1 is changed from (2,0,0) to (3,0,2) and the resources available are changed to (2,3,0)<\/p>\r\n\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><\/td>\r\n<td><em>Allocation<\/em><em> A B C<\/em><\/td>\r\n<td><em>Need<\/em><em> A B C<\/em><\/td>\r\n<td><em>Available<\/em><em> A B C<\/em><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><em>P<\/em>0<\/td>\r\n<td>0 1 0<\/td>\r\n<td>7 4 3<\/td>\r\n<td>2 3 0<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><em>P<\/em>1<\/td>\r\n<td>3 0 2<\/td>\r\n<td>0 2 0<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><em>P<\/em>2<\/td>\r\n<td>3 0 1<\/td>\r\n<td>6 0 0<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><em>P<\/em>3<\/td>\r\n<td>2 1 1<\/td>\r\n<td>0 1 1<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><em>P<\/em>4<\/td>\r\n<td>0 0 2<\/td>\r\n<td>4 3 1<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The system is\u00a0 now checked for a safety sequence. Executing safety algorithm shows that the sequence &lt;<em>P<\/em>1, <em>P<\/em>3, <em>P<\/em>4, <em>P<\/em>0, <em>P<\/em>2&gt; satisfies the safety requirement. Therefore, the request of <em>P<\/em>1 can be granted.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>After <em>P<\/em>1\u2019s request is granted, can request for (3,3,0) by <em>P<\/em>4 be granted?<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">No, resources are not available. Only (2,3,0) is available. <em>P<\/em>4 needs 3 instances of A, but only 2 instances of A are available.<\/p>\r\n&nbsp;\r\n\r\n<strong>Can request for (0,2,0) by <em>P<\/em>0 be granted?<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong><em>P<\/em><\/strong><strong>0 <\/strong><strong>Request (0,2,0)<\/strong>\r\n\r\n&nbsp;\r\n\r\nCheck if Request0 \u00a3 Available\r\n<p style=\"padding-left: 30px\">(0,2,0) \u00a3 (2,3,0) \u00de <em>true.<\/em><\/p>\r\n&nbsp;\r\n\r\nAlso, check if Request0 \u00a3 Need0 (that is (0,2,0) \u00a3 (7,4,3)) \u00de <em>true.<\/em>\r\n<p style=\"text-align: justify\">Now, \u00a0change \u00a0the \u00a0data \u00a0structures \u00a0as \u00a0if \u00a0the \u00a0request \u00a0is \u00a0granted \u00a0and \u00a0run \u00a0the \u00a0safety algorithm to check for a safe sequence. The future Need of <em>P<\/em>0 is changed from (7,4,3) to (7,2,3). \u00a0The \u00a0Allocation \u00a0of \u00a0<em>P<\/em>1 \u00a0is \u00a0changed \u00a0from \u00a0(0,1,0) \u00a0to \u00a0(0,3,0) \u00a0and \u00a0the \u00a0resources available are changed from (2,3,0) to (2,1,0).<\/p>\r\n\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><\/td>\r\n<td><em>Allocation<\/em><em> A B C<\/em><\/td>\r\n<td><em>Need<\/em><em> A B C<\/em><\/td>\r\n<td><em>Available<\/em><em> A B C<\/em><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><em>P<\/em>0<\/td>\r\n<td>0 3 0<\/td>\r\n<td>7 2 3<\/td>\r\n<td>2 1 0<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><em>P<\/em>1<\/td>\r\n<td>3 0 2<\/td>\r\n<td>0 2 0<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><em>P<\/em>2<\/td>\r\n<td>3 0 1<\/td>\r\n<td>6 0 0<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><em>P<\/em>3<\/td>\r\n<td>2 1 1<\/td>\r\n<td>0 1 1<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><em>P<\/em>4<\/td>\r\n<td>0 0 2<\/td>\r\n<td>4 3 1<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\nThe future need of any of the processes cannot be satisfied with the resources available. Therefore, there is no safe sequence. Therefore, it is understood that the request of <em>P<\/em>0 should not be granted.\r\n\r\n<\/div>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>18.5\u00a0 Summary<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">This module discussed what is meant by a safe sequence. We learnt how deadlocks can be avoided when there is a single instance for all resource types. We also learnt how deadlocks can be avoided when there are multiple instances of resources using the Banker\u2019s algorithm.<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>References<\/strong><\/p>\r\n\r\n<ol>\r\n \t<li style=\"text-align: justify\">Abraham Silberschatz, \u00a0Peter \u00a0B. \u00a0Galvin, \u00a0Greg \u00a0Gagne, \u00a0\u201cOperating \u00a0System Concepts\u201d, Ninth Edition, John Wiley &amp; Sons Inc., 2012.<\/li>\r\n<\/ol>","rendered":"<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>18.1 Introduction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial\"><span style=\"font-size: 1em\">In a <\/span>multi-programming<span style=\"font-size: 1em\">\u00a0environment, many processes compete for a finite number of resources. If the resources requested by a process are not available, the process waits. If the resources requested by this process are held by other waiting processes, the situation may lead to a deadlock. The different ways to handle deadlocks are deadlock prevention, deadlock avoidance, deadlock detection and recovery. In the previous module, we learnt different ways to prevent deadlocks. In this module, we shall see how deadlocks are avoided.<\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">In the previous module, we learnt that a system with resources can be depicted using a resource-allocation graph. In a resource-allocation graph, processes are denoted using circles and resource types are denoted using rectangles. The different instances of each resource type are shown as dots within the rectangles. Processes and resource types are the nodes of the resource-allocation graph. The edges show the dependency between the processes and the resources. If there is an edge from a process to a resource, it means that the process is requesting for an instance of the resource (request edge). If there is an edge from a resource to a process, it means that an instance of the resource is assigned to the process (assignment edge).<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">18.2. Resource-Allocation Graph Algorithm for Deadlock Avoidance\u00a0<\/strong><\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-178 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Deadlock-Avoidance.jpg\" alt=\"\" width=\"312\" height=\"215\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Deadlock-Avoidance.jpg 312w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Deadlock-Avoidance-300x207.jpg 300w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Deadlock-Avoidance-65x45.jpg 65w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Deadlock-Avoidance-225x155.jpg 225w\" sizes=\"auto, (max-width: 312px) 100vw, 312px\" \/><\/p>\n<p style=\"text-align: center\"><strong>Fig. 18.1 Resource-Allocation Graph for Deadlock Avoidance (Source: [1])<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A variant of the resource-allocation graph is used to avoid deadlocks when the\u00a0<span style=\"font-size: 1em;text-align: initial\">system has only one instance of a resource type. In this graph, a new type of edge is added called the claim edge. A claim edge is represented by a dashed line as shown in Figure 18.1. A claim edge <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">i \u00ae <\/span><em style=\"font-size: 1em;text-align: initial\">R<\/em><span style=\"font-size: 1em;text-align: initial\">j indicates that process <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">j will request resource <\/span><em style=\"font-size: 1em;text-align: initial\">R<\/em><span style=\"font-size: 1em;text-align: initial\">j. All claim edges must appear in the graph, initially. Resources must be claimed <\/span><em style=\"font-size: 1em;text-align: initial\">a priori <\/em><span style=\"font-size: 1em;text-align: initial\">in the system. That is, at the beginning, the system should know the future requests of all the processes. These requests that would be made in future are depicted using claim edges.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">A claim edge <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i \u00ae \u00a0<\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">j is converted to a request edge when process <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i actually requests for resource <\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">j. When resource <\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">j is released by process <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i, the assignment edge <\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">i \u00ae <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i reconverts to a claim edge <\/span><em style=\"font-size: 1em\">P<\/em><em style=\"font-size: 1em\">i <\/em><span style=\"font-size: 1em\">\u00ae <\/span><em style=\"font-size: 1em\">R<\/em><em style=\"font-size: 1em\">j<\/em><span style=\"font-size: 1em\">.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">We will now learn how deadlocks can be avoided using this type of resource-allocation graphs. Suppose <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i requests <\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">j. The resource-allocation graph would have had a claim edge from <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i to <\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">j. The claim edge is now converted to a request edge <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i \u00ae <\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">j. The request edge <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i \u00ae <\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">j is converted to an assignment edge <\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">j \u00ae <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i. The resource-allocation graph now depicts how the state of the system would have been after the resource <\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">j is allocated to <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i. The resource-allocation graph is now checked for cycles using the cycle-detection algorithm. If there is no cycle, the system is in a safe state and the resource can be allocated to <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i. If there is a cycle, process <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i has\u00a0to wait. <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">i is not allocated the resource <\/span><em style=\"font-size: 1em\">R<\/em><span style=\"font-size: 1em\">j immediately.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">Thus, the resource-allocation graph alone is modified and the system is checked for safe state. The actual allocation of resource is not done immediately. Only if the allocation would result in a safe state, the actual allocation of resources is done.<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-177 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Allocation-Graph-1.jpg\" alt=\"\" width=\"332\" height=\"216\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Allocation-Graph-1.jpg 332w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Allocation-Graph-1-300x195.jpg 300w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Allocation-Graph-1-65x42.jpg 65w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Allocation-Graph-1-225x146.jpg 225w\" sizes=\"auto, (max-width: 332px) 100vw, 332px\" \/><\/p>\n<p style=\"text-align: center\">Fig. 18.2 Unsafe State In Resource-Allocation Graph (Source: [1])<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Consider the resource-allocation graph in Figure 18.2. <em>P<\/em>2 requests for resource <em>R<\/em>2 as shown using the claim edge from <em>P<\/em>2 to <em>R<\/em>2 in Figure 18.1. This is converted to an assignment edge in the resource-allocation graph as shown in Figure 18.2. The graph is now checked for cycles. Since there is a cycle, the system understands that allocating\u00a0<em style=\"font-size: 1em;text-align: initial\">R<\/em><span style=\"font-size: 1em;text-align: initial\">2 to <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">2 will lead to a deadlock. The only instance of <\/span><em style=\"font-size: 1em;text-align: initial\">R<\/em><span style=\"font-size: 1em;text-align: initial\">1 is assigned to <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1 and the only instance of <\/span><em style=\"font-size: 1em;text-align: initial\">R<\/em><span style=\"font-size: 1em;text-align: initial\">2 is assigned to <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">2. <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">2 has requested for <\/span><em style=\"font-size: 1em;text-align: initial\">R<\/em><span style=\"font-size: 1em;text-align: initial\">1. It is known from the claim edge that process <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1 may request for <\/span><em style=\"font-size: 1em;text-align: initial\">R<\/em><span style=\"font-size: 1em;text-align: initial\">2. If <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1 requests for <\/span><em style=\"font-size: 1em;text-align: initial\">R<\/em><span style=\"font-size: 1em;text-align: initial\">2 (before releasing <\/span><em style=\"font-size: 1em;text-align: initial\">R<\/em><span style=\"font-size: 1em;text-align: initial\">1, then both the processes <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1 and <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">2 will wait for a resource held by the other process. The system will go to a deadlocked state. Therefore, <\/span><em style=\"font-size: 1em;text-align: initial\">R<\/em><span style=\"font-size: 1em;text-align: initial\">2 is not allocated to <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">2. Thus, deadlocks are avoided.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">This method can be used to avoid deadlocks only if there is a single instance of each resource type. If there are more instances of each resource type, Banker\u2019s algorithm is used for deadlock avoidance.<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>18.3\u00a0 Banker\u2019s Algorithm<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the Banker\u2019s algorithm, each process must a priori declare its maximum use. This should be done for any deadlock avoidance algorithm. The maximum resource need of each process must not exceed the total number of resources in the system. When a user requests for resources, the system checks whether the allocation of the request will result in a safe state. If, after the allocation, the system will be in a safe state, resources are allocated. Else, the process waits. When a process gets all its resources it must return them in a finite amount of time.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>18.3.1\u00a0 Data Structures for the Banker\u2019s Algorithm\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Let <em>n <\/em>denote the number of processes and <em>m <\/em>denote the number of resource types.<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0\u00a0<em>Available: <\/em>Vector of length <em>m<\/em>. If Available[<em>j<\/em>] = <em>k<\/em>, there are <em>k \u00a0<\/em>instances \u00a0of resource type <em>R<\/em><em>j <\/em>available.<\/p>\n<p style=\"padding-left: 240px\"><em>A B C<\/em><\/p>\n<p style=\"padding-left: 240px\">3 3 2<\/p>\n<p style=\"text-align: justify\">In the example shown above, there are three resource types A, B and C. The number of available instances of resource types A, B and C are 3, 3 and 2 respectively.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0<em>Max: n x m <\/em>matrix. The <em>n <\/em>rows correspond to <em>n <\/em>processes and the <em>m <\/em>columns correspond to the <em>m <\/em>resource types. If Max[<em>i, j<\/em>] = <em>k<\/em>, then process <em>P<\/em><em>i <\/em>may request at most <em>k <\/em>instances of resource type <em>R<\/em><em>j<\/em><\/p>\n<table class=\"aligncenter\" style=\"width: 100px\">\n<tbody>\n<tr>\n<td style=\"width: 181.063px\"><\/td>\n<td style=\"width: 199.063px;padding-left: 30px\"><em>A B C<\/em><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 181.063px;text-align: right\"><em>P<\/em>0<\/td>\n<td style=\"width: 199.063px;padding-left: 30px\">7 5 3<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 181.063px;text-align: right\"><em>P<\/em>1<\/td>\n<td style=\"width: 199.063px;padding-left: 30px\">3 2 2<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 181.063px;text-align: right\"><em>P<\/em>2<\/td>\n<td style=\"width: 199.063px;padding-left: 30px\">9 0 2<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 181.063px;text-align: right\"><em>P<\/em>3<\/td>\n<td style=\"width: 199.063px;padding-left: 30px\">2 2 2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the example shown above, there are three resource types A, B and C and four processes <em>P<\/em>0, <em>P<\/em>1, <em>P<\/em>2 and <em>P<\/em>3. The maximum need of process <em>P<\/em>0 can\u00a0 be 7 instances of resource type A, 5 instances of resource type B and 3 instances of\u00a0<span style=\"font-size: 1em;text-align: initial\">resource type C. Similarly, the maximum need of the other processes <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1, <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">2 and\u00a0<\/span><em style=\"text-align: initial;font-size: 1em\">P<\/em><span style=\"text-align: initial;font-size: 1em\">3 are also shown.<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0<em>Allocation: n <\/em>x <em>m <\/em>matrix. The <em>n <\/em>rows correspond to <em>n <\/em>processes and the <em>m <\/em>columns correspond to the <em>m <\/em>resource types. If Allocation[<em>i,j<\/em>] = <em>k <\/em>then process <em>P<\/em><em>i <\/em>is currently allocated <em>k <\/em>instances of <em>R<\/em><em>j.<\/em><\/p>\n<table class=\"aligncenter\" style=\"width: 288px\">\n<tbody>\n<tr>\n<td style=\"width: 176.063px\"><\/td>\n<td style=\"padding-left: 30px;width: 180.531px\"><strong><em>A B C<\/em><\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: right;width: 176.063px\"><em>P<\/em>0<\/td>\n<td style=\"padding-left: 30px;width: 180.531px\">0 1 0<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: right;width: 176.063px\"><em>P<\/em>1<\/td>\n<td style=\"padding-left: 30px;width: 180.531px\">2 0 0<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: right;width: 176.063px\"><em>P<\/em>2<\/td>\n<td style=\"padding-left: 30px;width: 180.531px\">3 0 2<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: right;width: 176.063px\"><em>P<\/em>3<\/td>\n<td style=\"padding-left: 30px;width: 180.531px\">2 1 1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the example shown above, there are three resource types A, B and C and four processes <em>P<\/em>0, <em>P<\/em>1, <em>P<\/em>2 and <em>P<\/em>3. The number of instances of resource type currently allocated to each process is shown. Process <em>P<\/em>0 is currently allocated 0 instances of resource type A, 1 instance of resource type B and 0 instances of resource type C. Similarly, the current allocation of the other processes <em>P<\/em>1, <em>P<\/em>2 and <em>P<\/em>3 are also shown.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0<em>Need: n <\/em>x <em>m <\/em>matrix. The <em>n <\/em>rows correspond to <em>n <\/em>processes and the <em>m <\/em>columns correspond to the <em>m <\/em>resource types. This matrix shows the future need of each process. If <em>Need<\/em>[<em>i, j<\/em>] = <em>k<\/em>, then <em>P<\/em><em>i <\/em>may need <em>k <\/em>more instances of <em>R<\/em><em>j <\/em>to complete its task.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"padding-left: 30px\"><em>Need <\/em>[<em>i, j] <\/em>= <em>Max<\/em>[<em>i, j<\/em>] \u2013 <em>Allocation <\/em>[<em>i, j<\/em>]<\/p>\n<p>&nbsp;<\/p>\n<p>That is, future need is a process\u2019s maximum need minus the resources currently allocated.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>18.3.2\u00a0 Notations Used:\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>If <em>X <\/em>and <em>Y <\/em>are vectors of length <em>n<\/em><\/p>\n<ul>\n<li><em>X <\/em>\u2264 <em>Y <\/em>iff <em>X<\/em>[<em>i<\/em>] \u2264 <em>Y<\/em>[<em>i<\/em>] for all <em>i <\/em>= 1,2,\u2026,<em>n<\/em><\/li>\n<\/ul>\n<p style=\"padding-left: 90px\">That is, the ith element of vector X is less than or equal to the ith element of vector Y, for all i.<\/p>\n<p style=\"padding-left: 60px\">Consider the following vectors X and Y<\/p>\n<ul>\n<li>If <em>X <\/em>= (1,7,3,2), <em>Y <\/em>= (0,3,2,1)<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the above <em style=\"text-align: initial;font-size: 1em\">Y <\/em><span style=\"text-align: initial;font-size: 1em\">\u2264 <\/span><em style=\"text-align: initial;font-size: 1em\">X<\/em><span style=\"text-align: initial;font-size: 1em\">, because each element of vector Y is less than or equal to the corresponding element of vector X. That is, the first element of Y is less than the first element of X and so on.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><em>Y &lt;\u00a0\u00a0\u00a0 X, <\/em>if <em>Y <\/em>\u2264 <em>X <\/em>and <em>Y<\/em>\u2260 <em>X<\/em><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Each row in the matrices Allocation and Need is treated as a vector. For example, the <em>i<\/em>th\u00a0 row of matrix Allocation and Need are\u00a0 referred to as Allocation<em>i\u00a0 <\/em>and Need<em>i <\/em>respectively.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">The Banker\u2019s algorithm comprises a safety algorithm and a resource-allocation algorithm. For a given state of the system, the safety algorithm checks if the system is in a safe state. The safety algorithm is given below:<\/span><\/p>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p><strong>18.3.3\u00a0 Safety Algorithm\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0 Let <em>Work <\/em>and <em>Finish <\/em>be vectors of length <em>m <\/em>and <em>n <\/em>respectively.\u00a0\u00a0 Initialize:<\/p>\n<p style=\"padding-left: 60px\"><em>Work <\/em>= <em>Available<\/em><\/p>\n<p style=\"padding-left: 60px\"><em>Finish <\/em>[<em>i<\/em>] = <em>false <\/em>for <em>i <\/em>= 1,2, \u2026, <em>n<\/em><\/p>\n<p>2.\u00a0 Find an <em>i <\/em>such that both:<\/p>\n<p style=\"padding-left: 60px\">(a)\u00a0 <em>Finish <\/em>[<em>i<\/em>] = <em>false<\/em><\/p>\n<p style=\"padding-left: 60px\">(b)\u00a0 <em>Need<\/em><em>i <\/em>\u00a3 <em>Work<\/em><\/p>\n<p style=\"padding-left: 60px\">If no such <em>i <\/em>exists, go to step 4.<\/p>\n<p>3.\u00a0 W<em>ork <\/em>= <em>Work <\/em>+ <em>Allocation<\/em><em>i <\/em><\/p>\n<p style=\"padding-left: 60px\"><em>Finish<\/em>[<em>i<\/em>] = <em>true<\/em><\/p>\n<p style=\"padding-left: 60px\">go to step 2.<\/p>\n<p>4.\u00a0 If <em>Finish <\/em>[<em>i<\/em>] == true for all <em>i<\/em>, then the system is in a safe state.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Whenever a request for resources comes from a process, the system runs the resource-request algorithm and modifies the state of the system in the simulation as if the resource is allocated. The actual state of the system is not modified; the data structures in the algorithm alone are modified. The safety algorithm is run to check if the system still remains in a safe state. The resource-request algorithm for process <em>P<\/em>i is given below:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>18.3.4\u00a0 Resource-Request Algorithm for Process <em>P<\/em><em>i<\/em>\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><em>Request<\/em><em>i <\/em>= request vector for process <em>P<\/em><em>i<\/em>. If <em>Request<\/em><em>i <\/em>[<em>j<\/em>] = <em>k <\/em>then process <em>P<\/em><em>i <\/em>wants <em>k\u00a0<\/em>instances of resource type <em>R<\/em><em>j<\/em>.<\/p>\n<p>&nbsp;<\/p>\n<p>1. If <em>Request<\/em><em>i <\/em>\u00a3 <em>Need<\/em><em>i <\/em>go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim.<\/p>\n<p>2. If <em>Request<\/em><em>i <\/em>\u00a3 <em>Available<\/em>, go to step 3. Otherwise <em>P<\/em><em>i\u00a0\u00a0\u00a0\u00a0 <\/em>must wait, since resources are not available.<\/p>\n<p>3. Pretend to allocate requested resources to <em>P<\/em><em>i <\/em>by modifying the state as follows:<\/p>\n<p>&nbsp;<\/p>\n<p><em>Available <\/em>= <em>Request<\/em><em>i<\/em><em>;<\/em><\/p>\n<p><em>Allocation<\/em><em>i <\/em>= <em>Allocation<\/em><em>i <\/em>+ <em>Request<\/em><em>i<\/em>;<\/p>\n<p><em>Need<\/em><em>i <\/em>= <em>Need<\/em><em>i <\/em>\u2013 <em>Request<\/em><em>i;;<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>The safety algorithm is now run to check if the system is in a safe state with this modified state.<\/p>\n<p>&nbsp;<\/p>\n<p><em>If safe <\/em>\u00de <em>the resources are allocated to P<\/em><em>i<\/em><em>.<\/em><\/p>\n<p><em>If unsafe <\/em>\u00de <em>P<\/em>i <em>m<\/em><em>ust wait, and the old resource-allocation state is restored<\/em><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">We will understand how the algorithm works by an example as given below.<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p><strong>18.3.5\u00a0 Example of Banker\u2019s Algorithm<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Consider 5 processes <em>P<\/em>0 \u00a0through <em>P<\/em>4; 3 resource types <em>A<\/em>, <em>B <\/em>and <em>C<\/em>. There are 10 instances of <em>A<\/em>, 5 instances of <em>B <\/em>and 7 instances of <em>C<\/em>.<\/p>\n<p>&nbsp;<\/p>\n<p>The snapshot at time <em>T<\/em>0 is given below:<\/p>\n<table class=\"aligncenter\" style=\"height: 105px;width: 308px\">\n<tbody>\n<tr style=\"height: 15px\">\n<td style=\"height: 15px;width: 39.0625px\"><\/td>\n<td style=\"height: 15px;width: 126.063px\"><strong><em>Allocation<\/em><\/strong><\/td>\n<td style=\"height: 15px;width: 71.0625px\"><strong><em>M<\/em><\/strong><strong><em>ax<\/em><\/strong><\/td>\n<td style=\"height: 15px;width: 116.063px\"><strong><em>Available<\/em><\/strong><\/td>\n<\/tr>\n<tr style=\"height: 15px\">\n<td style=\"height: 15px;width: 39.0625px\"><\/td>\n<td style=\"height: 15px;width: 126.063px\"><strong><em>A B C<\/em><\/strong><\/td>\n<td style=\"height: 15px;width: 71.0625px\"><strong><em>A B C<\/em><\/strong><\/td>\n<td style=\"width: 116.063px\"><strong><em>A B C<\/em><\/strong><\/td>\n<\/tr>\n<tr style=\"height: 15px\">\n<td style=\"height: 15px;width: 39.0625px\"><strong><em>P<\/em><\/strong><strong>0<\/strong><\/td>\n<td style=\"height: 15px;width: 126.063px\"><strong>0 1 0<\/strong><\/td>\n<td style=\"height: 15px;width: 71.0625px\"><strong>7 5 3<\/strong><\/td>\n<td style=\"height: 15px;width: 116.063px\"><strong>3 3 2<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 15px\">\n<td style=\"height: 15px;width: 39.0625px\"><strong><em>P<\/em><\/strong><strong>1<\/strong><\/td>\n<td style=\"height: 15px;width: 126.063px\"><strong>2 0 0<\/strong><\/td>\n<td style=\"height: 15px;width: 71.0625px\"><strong>3 2 2<\/strong><\/td>\n<td style=\"height: 15px;width: 116.063px\"><\/td>\n<\/tr>\n<tr style=\"height: 15px\">\n<td style=\"height: 15px;width: 39.0625px\"><strong><em>P<\/em><\/strong><strong>2<\/strong><\/td>\n<td style=\"height: 15px;width: 126.063px\"><strong>3 0 2<\/strong><\/td>\n<td style=\"height: 15px;width: 71.0625px\"><strong>9 0 2<\/strong><\/td>\n<td style=\"height: 15px;width: 116.063px\"><\/td>\n<\/tr>\n<tr style=\"height: 15px\">\n<td style=\"height: 15px;width: 39.0625px\"><strong><em>P<\/em><\/strong><strong>3<\/strong><\/td>\n<td style=\"height: 15px;width: 126.063px\"><strong>2 1 1<\/strong><\/td>\n<td style=\"height: 15px;width: 71.0625px\"><strong>2 2 2<\/strong><\/td>\n<td style=\"height: 15px;width: 116.063px\"><\/td>\n<\/tr>\n<tr style=\"height: 15px\">\n<td style=\"height: 15px;width: 39.0625px\"><strong><em>P<\/em><\/strong><strong>4<\/strong><\/td>\n<td style=\"height: 15px;width: 126.063px\"><strong>0 0 2<\/strong><\/td>\n<td style=\"height: 15px;width: 71.0625px\"><strong>4 3 3<\/strong><\/td>\n<td style=\"height: 15px;width: 116.063px\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>The content of the matrix Need is defined to be Max \u2013 Allocation. Matrix Need is given below:<\/p>\n<p style=\"padding-left: 210px\"><em>Need <\/em><em>\u00a0A B C<\/em><\/p>\n<p style=\"padding-left: 210px\"><em>P<\/em>0\u00a0\u00a0\u00a0\u00a0\u00a0 7 4 3<\/p>\n<p style=\"padding-left: 210px\"><em>P<\/em>1\u00a0 \u00a0 \u00a0 \u00a01 2 2<\/p>\n<p style=\"padding-left: 210px\"><em>P<\/em>2\u00a0\u00a0\u00a0\u00a0\u00a0 6 0 0<\/p>\n<p style=\"padding-left: 210px\"><em>P<\/em>3\u00a0\u00a0\u00a0\u00a0\u00a0 0 1 1<\/p>\n<p style=\"padding-left: 210px\"><em>P<\/em>4\u00a0\u00a0\u00a0\u00a0\u00a0 4 3 1<\/p>\n<p>&nbsp;<\/p>\n<p>We will first check if the system is in a safe state in the current situation. For this, the safety algorithm is run.<\/p>\n<p style=\"padding-left: 270px\"><em>Allocation<\/em><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Need\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Available<\/em><\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><em>\u00a0<\/em><\/td>\n<td><em>A B C<\/em><\/td>\n<td><em>A B C<\/em><\/td>\n<td><em>A B C<\/em><\/td>\n<\/tr>\n<tr>\n<td><em>P<\/em>0<\/td>\n<td>0 1 0<\/td>\n<td>7 4 3<\/td>\n<td>3 3 2<\/td>\n<\/tr>\n<tr>\n<td><em>P<\/em>1<\/td>\n<td>2 0 0<\/td>\n<td>1 2 2<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><em>P<\/em>2<\/td>\n<td>3 0 2<\/td>\n<td>6 0 0<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><em>P<\/em>3<\/td>\n<td>2 1 1<\/td>\n<td>0 1 1<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><em>P<\/em>4<\/td>\n<td>0 0 2<\/td>\n<td>4 3 1<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>Vector Work is initialized to Available Therefore, Work = Available = (3,3,2)<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">Finish[i] = false for i = 0,1,2,3,4 Let i = 0<\/span><\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">Is Need0 \u2264 Work?\u00a0\u00a0\u00a0 No, (7,4,3) is not less than or equal to (3,3,2)<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify;padding-left: 30px\">\n<p>Let i = 1<\/p>\n<p>Is Need1 \u2264 Work?\u00a0\u00a0\u00a0 Yes, (1,2,2) is less than or equal to (3,3,2)<\/p>\n<p>Therefore, set Finish[1] = true. <em>P<\/em>1 added to the safe sequence &lt; <em>P<\/em>1&gt;<\/p>\n<p><span style=\"text-align: initial;text-indent: 1em;font-size: 1em\">Is Need2 \u2264 Work?\u00a0\u00a0\u00a0 No, (6,0,0) is not less than or equal to (5,3,2)<\/span><\/p>\n<p>Work = Work + Allocation1 = (3,3,2) + (2,0,0) = (5,3,2) Let i = 2<\/p>\n<p>&nbsp;<\/p>\n<p>Let i = 3<\/p>\n<p>Is Need3 \u2264 Work?\u00a0\u00a0\u00a0 Yes, (0,1,1) is less than or equal to (5,3,2)<\/p>\n<p>Therefore, set Finish[3] = true , <em>P<\/em>3 added to safe sequence &lt; <em>P<\/em>1, <em>P<\/em>3&gt;<\/p>\n<p>Work = Work + Allocation3 =(5,3,2) + (2,1,1) = (7,4,3)<\/p>\n<p>&nbsp;<\/p>\n<p>Work = (7,4,4)<\/p>\n<p>Let i = 4<\/p>\n<p>Is Need4 \u2264 Work? Yes, (4,3,1) is less than or equal to (7,4,3)<\/p>\n<p>Set Finish[4] = true , <em>P<\/em>4 is added to safe sequence &lt; <em>P<\/em>1 <em>,P<\/em>3<em>, P<\/em>4 &gt;<\/p>\n<p>Work = Work + Allocation4 = (7,4,3) + (0,0,2) = (7,4,5)<\/p>\n<p>&nbsp;<\/p>\n<p>Work = (7,4,5)<\/p>\n<p>Now, check again the processes that were not added to the safe sequence. <em>P<\/em>0<\/p>\n<p>and <em>P<\/em>2 are remaining.<\/p>\n<p>&nbsp;<\/p>\n<p>So, let i = 0<\/p>\n<p>Is Need0 \u2264 Work?\u00a0\u00a0\u00a0 Yes<\/p>\n<p>Set Finish[0] = true, <em>P<\/em>0 is added to safe sequence &lt; <em>P<\/em>1 <em>,P<\/em>3<em>, P<\/em>4 <em>, P<\/em>0&gt;<\/p>\n<p>Work = Work + Allocation0 =(7,4,5) + (0,1, 0) = (7,5,5)<\/p>\n<p>&nbsp;<\/p>\n<p>Work = (7,5,5)<\/p>\n<p>Let i = 2<\/p>\n<p>Is Need2 \u2264 Work?\u00a0\u00a0\u00a0 Yes<\/p>\n<p>Finish[0] = true , <em>P<\/em>2 is added to safe sequence &lt;<em>P<\/em>1 <em>,P<\/em>3<em>, P<\/em>4 <em>, P<\/em>0 <em>, P<\/em>2 &gt;<\/p>\n<p>Work = Work + Allocation2 =(7,5,5) + (3,0,2) = (10,5,7)<\/p>\n<p>&nbsp;<\/p>\n<p>The system is in a safe state since the sequence &lt; <em>P<\/em><sub>1<\/sub>, <em>P<\/em><sub>3<\/sub>, <em>P<\/em><sub>4<\/sub>, <em>P<\/em><sub>2<\/sub>, <em>P<\/em><sub>0<\/sub>&gt; satisfies the safety requirement<\/p>\n<p>&nbsp;<\/p>\n<p>Now, let <em>P<\/em><sub>1\u00a0<\/sub>Request (1,0,2)<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">First, check if Request1 \u00a3 Available (that is, (1,0,2) \u00a3 (3,3,2)) \u00de <\/span><em style=\"text-align: initial;font-size: 1em\">true.<\/em><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">Also, check if Request1 \u00a3 Need1 (that is (1,0,2) \u00a3 (1,2,2)) \u00de <\/span><em style=\"text-align: initial;font-size: 1em\">true.<\/em><\/p>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now, the data structures are changed as if the request of <em>P<\/em>1 is granted. The future Need of <em>P<\/em>1 is changed from (1,2,2) to (0,2,0). The Allocation of <em>P<\/em>1 is changed from (2,0,0) to (3,0,2) and the resources available are changed to (2,3,0)<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><\/td>\n<td><em>Allocation<\/em><em> A B C<\/em><\/td>\n<td><em>Need<\/em><em> A B C<\/em><\/td>\n<td><em>Available<\/em><em> A B C<\/em><\/td>\n<\/tr>\n<tr>\n<td><em>P<\/em>0<\/td>\n<td>0 1 0<\/td>\n<td>7 4 3<\/td>\n<td>2 3 0<\/td>\n<\/tr>\n<tr>\n<td><em>P<\/em>1<\/td>\n<td>3 0 2<\/td>\n<td>0 2 0<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><em>P<\/em>2<\/td>\n<td>3 0 1<\/td>\n<td>6 0 0<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><em>P<\/em>3<\/td>\n<td>2 1 1<\/td>\n<td>0 1 1<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><em>P<\/em>4<\/td>\n<td>0 0 2<\/td>\n<td>4 3 1<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The system is\u00a0 now checked for a safety sequence. Executing safety algorithm shows that the sequence &lt;<em>P<\/em>1, <em>P<\/em>3, <em>P<\/em>4, <em>P<\/em>0, <em>P<\/em>2&gt; satisfies the safety requirement. Therefore, the request of <em>P<\/em>1 can be granted.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>After <em>P<\/em>1\u2019s request is granted, can request for (3,3,0) by <em>P<\/em>4 be granted?<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">No, resources are not available. Only (2,3,0) is available. <em>P<\/em>4 needs 3 instances of A, but only 2 instances of A are available.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Can request for (0,2,0) by <em>P<\/em>0 be granted?<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong><em>P<\/em><\/strong><strong>0 <\/strong><strong>Request (0,2,0)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Check if Request0 \u00a3 Available<\/p>\n<p style=\"padding-left: 30px\">(0,2,0) \u00a3 (2,3,0) \u00de <em>true.<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>Also, check if Request0 \u00a3 Need0 (that is (0,2,0) \u00a3 (7,4,3)) \u00de <em>true.<\/em><\/p>\n<p style=\"text-align: justify\">Now, \u00a0change \u00a0the \u00a0data \u00a0structures \u00a0as \u00a0if \u00a0the \u00a0request \u00a0is \u00a0granted \u00a0and \u00a0run \u00a0the \u00a0safety algorithm to check for a safe sequence. The future Need of <em>P<\/em>0 is changed from (7,4,3) to (7,2,3). \u00a0The \u00a0Allocation \u00a0of \u00a0<em>P<\/em>1 \u00a0is \u00a0changed \u00a0from \u00a0(0,1,0) \u00a0to \u00a0(0,3,0) \u00a0and \u00a0the \u00a0resources available are changed from (2,3,0) to (2,1,0).<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><\/td>\n<td><em>Allocation<\/em><em> A B C<\/em><\/td>\n<td><em>Need<\/em><em> A B C<\/em><\/td>\n<td><em>Available<\/em><em> A B C<\/em><\/td>\n<\/tr>\n<tr>\n<td><em>P<\/em>0<\/td>\n<td>0 3 0<\/td>\n<td>7 2 3<\/td>\n<td>2 1 0<\/td>\n<\/tr>\n<tr>\n<td><em>P<\/em>1<\/td>\n<td>3 0 2<\/td>\n<td>0 2 0<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><em>P<\/em>2<\/td>\n<td>3 0 1<\/td>\n<td>6 0 0<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><em>P<\/em>3<\/td>\n<td>2 1 1<\/td>\n<td>0 1 1<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><em>P<\/em>4<\/td>\n<td>0 0 2<\/td>\n<td>4 3 1<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>The future need of any of the processes cannot be satisfied with the resources available. Therefore, there is no safe sequence. Therefore, it is understood that the request of <em>P<\/em>0 should not be granted.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>18.5\u00a0 Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This module discussed what is meant by a safe sequence. We learnt how deadlocks can be avoided when there is a single instance for all resource types. We also learnt how deadlocks can be avoided when there are multiple instances of resources using the Banker\u2019s algorithm.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>References<\/strong><\/p>\n<ol>\n<li style=\"text-align: justify\">Abraham Silberschatz, \u00a0Peter \u00a0B. \u00a0Galvin, \u00a0Greg \u00a0Gagne, \u00a0\u201cOperating \u00a0System Concepts\u201d, Ninth Edition, John Wiley &amp; Sons Inc., 2012.<\/li>\n<\/ol>\n","protected":false},"author":4,"menu_order":15,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-mary-anitha-rajam"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-173","chapter","type-chapter","status-publish","hentry","contributor-dr-mary-anitha-rajam"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters\/173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":5,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters\/173\/revisions"}],"predecessor-version":[{"id":420,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters\/173\/revisions\/420"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters\/173\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapter-type?post=173"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/contributor?post=173"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/license?post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}