VOOZH about

URL: https://www.geeksforgeeks.org/operating-systems/program-bankers-algorithm-set-1-safety-algorithm/

โ‡ฑ Program for Banker's Algorithm | Set 1 (Safety Algorithm) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program for Banker's Algorithm | Set 1 (Safety Algorithm)

Last Updated : 5 Feb, 2025

The bankerโ€™s algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating the allocation for the predetermined maximum possible amounts of all resources, then makes an โ€œs-stateโ€ check to test for possible activities, before deciding whether allocation should be allowed to continue. The following Data structures are used to implement the Bankerโ€™s Algorithm: Let โ€˜nโ€™ be the number of processes in the system and โ€˜mโ€™ be the number of resource types.

Available: 

  • It is a 1-d array of size โ€˜mโ€™ indicating the number of available resources of each type.
  • Available[ j ] = k means there are โ€˜kโ€™ instances of resource type Rj

Max:

  • It is a 2-d array of size โ€˜n*mโ€™ that defines the maximum demand of each process in a system.
  • Max[ i, j ] = k means process Pi may request at most โ€˜kโ€™ instances of resource type Rj.

Allocation:

  • It is a 2-d array of size โ€˜n*mโ€™ that defines the number of resources of each type currently allocated to each process.
  • Allocation[ i, j ] = k means process Pi is currently allocated โ€˜kโ€™ instances of resource type Rj

Need:

  • It is a 2-d array of size โ€˜n*mโ€™ that indicates the remaining resource need of each process.
  • Need [ i, j ] = k means process Pi currently allocated โ€˜kโ€™ instances of resource type Rj
  • Need [ i, j ] = Max [ i, j ] โ€“ Allocation [ i, j ]

Allocationi specifies the resources currently allocated to process Pi and Needi specifies the additional resources that process Pi may still request to complete its task. Banker's algorithm consist of Safety algorithm and Resource request algorithm Safety Algorithm

The algorithm for finding out whether or not a system is in a safe state can be described as follows:

  1. Let Work and Finish be vectors of length 'm' and 'n' respectively. Initialize: Work= Available Finish [i]=false; for i=1,2,......,n
  2. Find an i such that both a) Finish [i]=false b) Need_i<=work 
    if no such i exists goto step (4)
  3. Work=Work + Allocation_i Finish[i]= true goto step(2)
  4. If Finish[i]=true for all i, then the system is in safe state.

Safe sequence is the sequence in which the processes can be safely executed. A state is safe if the system can allocate resources to each process (up to its maximum) in some order and still avoid a deadlock. More formally, " a system is in a safe state only if there exists a safe sequence" .If a system has a safe sequence, it implies that there is no deadlock present. The sequence guarantees that all processes will complete their execution without getting into a circular wait situation. It is possible to have multiple safe sequences in a system. This occurs when there are multiple ways to order the execution of processes such that no deadlock will occur. Each safe sequence represents a different order in which processes can be executed while avoiding deadlocks. The existance of multiple safe sequences allows the system to have flexibility in scheduling processes and allocating resources. Different scheduling algorithms or resource allocation can depend on factors such as system efficiency, resource utilization, fairness or other criteria that the operating system aims to optimize.

In this post, implementation of Safety algorithm of Banker's Algorithm is done. 

Output:

System is in safe state.
Safe sequence is: 1 3 4 0 2

Illustration : Considering a system with five processes P0 through P4 and three resources types A, B, C. Resource type A has 10 instances, B has 5 instances and type C has 7 instances. Suppose at time t0 following snapshot of the system has been taken: ๐Ÿ‘ Image
We must determine whether the new system state is safe. To do so, we need to execute Safety algorithm on the above given allocation chart.


Following is the resource allocation graph:

๐Ÿ‘ Bankers
Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2 > satisfies safety requirement.

Time complexity = O(n*n*m)
where n = number of processes and m = number of resources.

Conclusion

The program for the Banker's Algorithm demonstrates how the system ensures safe resource allocation to prevent deadlocks. Through the provided code and example, it becomes clear how the algorithm checks resource requests, evaluates safe states, and grants resources only when the system remains stable. This algorithm is an essential tool in operating systems for managing resources efficiently and ensuring smooth multitasking without entering unsafe states. Understanding its implementation helps in designing systems that avoid deadlocks while making the best use of available resources.


Comment