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. Utilizing the following data structures, the Banker's Algorithm is implemented: Assume that there are n processes in the system and m different resource kinds. Available:
Max:
Max[i, j] = k. Allocation:
Need:
The resources that are now allotted to process Pi are identified by Allocation, while the extra resources that process Pi could yet need in order to do its work are identified by Need. 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 Request represent the process Pi request array. Process Pi requests k instances of resource type Rj, which is indicated by Request [j] = k. The following things happen when process Pi makes a request for resources: Banker's algorithm source codeOutput: Following is the SAFE Sequence P1 -> P3 -> P4 -> P0 -> P2 ....................................................... Process executed in 2.32 seconds Press any key to continue. Explanation:
Next TopicFIFO Page Replacement Algorithm in C |