Deadlock Prevention using Banker's Algorithm in CThe banker's algorithm is a resource allocation and deadlock avoidance algorithm that simulates resource allocation for predetermined maximum possible amounts of all resources before performing an "s-state" check to look for potential activities and determining whether allocation should be permitted to continue. Why the Banker's Algorithm is called that?The reason the banker's algorithm is so titled is because it is employed in the banking sector to determine whether or not to authorize a loan to an individual. Assume a bank has n account holders, each of whom has an individual balance of S. When someone requests for a loan, the bank first deducts the loan amount from the total amount of money it possesses, and only approves the loan if the balance is more than S. It is done because the bank can simply do it if every account holder shows up to get their money. In other words, the bank would never arrange its funds in a way that would prevent it from meeting the demands of all of its clients. The bank would strive to maintain safety at all times. The Banker's Algorithm is implemented using the following data structures: Let n be the number of processes in the system and m be the number of resource kinds. Available:
Max:
Allocation:
Need:
The resources that are now allotted to process Pi are identified by Allocationi, while the extra resources that process Pi could yet need in order to do its work are identified by Needi. The safety algorithm and the resource request algorithm make up the banker's algorithm. Algorithm for SafetyThe following is a description of the method for determining if a system is in a safe state: Algorithm for Resource RequestsLet Requesti represent the process Pi request array. Process Pi requests k instances of resource type Rj, which is indicated by Requesti [j] = k. The following things happen when process Pi makes a request for resources: Below is the implementation of Banker's AlgorithmOutput Following is the SAFE Sequence P1 -> P3 -> P4 -> P0 -> P2 ........................................................ Process execute din 1.33 seconds Press any key to continue. Explanation:
Next TopicSimple Stopwatch Program in C |