The Banker's algorithm is an algorithm used in operating systems to avoid the deadlock situation by making sure that all the processes will get the resources they need to complete execution. This implementation of the Banker's algorithm is written in Python using PySimpleGUI and NumPy libraries.
The following libraries are required to run the code:
- PySimpleGUI
- NumPy
These libraries can be easily installed using pip. Open your terminal and run the following command:
- pip install PySimpleGUI
- pip install numpy
- Run the bankers_algorithm.py file in a Python environment (e.g. IDLE, Jupyter Notebook, etc.)
- The application will start and you will be prompted to enter the number of resources and processes.
- Enter the required parameters and click on the Next button.
- Enter the required matrices (total number of each resource, available number of each resource, maximum number of each resource for each process, current number of each resource for each process) and click on the Almost there button.
- Enter the process number, resource number, and the request. Then click on the Check button to check whether the request can be granted or not.
- If request can be granted user can start simulation else user must enter valid number of resources required.
- When simulation starts user can observe the different progress bars updating and popup messages inducating current state.
- When done a popup message appears
- Now try a deadlock case to see the other popup messages :)
- Example:
- total resource <6,5,7,6>
- available <3,1,1,2>
- currently allocated <<1,2,2,1>,<1,0,3,3>,<1,2,1,0>>
- maximum need <<3,3,2,2>,<1,2,3,4>,<1,3,5,0>>
- process 2 requests 1 of resource 2
Its quite simple actually after taking all the user inputs we take a "reserved" value of necessary matrices note that a numpy array we must use np.copy as regular assignment will be referenced.
Then a request is asked for example: process 1 requests 1 unit of process 1. After checking for units validity the numpy arrays are updated.
Here comes the cool part
- we have a while loop to check whether the entire matrix is set to a certain value (-1 our flag to state that a process is done)
- then we have a for loop to check on each process
- if the need resources of this process is less than or equal available then it is safe
- the available matrix is updated by adding the current resources of this process to the available
- and the row of this process in need matrix is set to -1 (to then avoid in upcoming iterations)
- else we do nothing we just update an iterator
- if the iterator is equal to processes then all processes are unsafe we just exit
- now you wonder an infinite loop might occure because what if we never have the entire matrix set to -1 and not all processes are unsafe
- the counter plays its role here after each for loop we update the counter once we reached equal number of processes we exit
- the counter also ensures if they don't come in a nice sequence of safe safe safe or unsafe unsafe unsafe if they are random everything will be checked
- try this example:
- total resource <6,5,7,6>
- available <3,1,1,2>
- currently allocated <<1,0,3,3>,<1,2,2,1>,<1,2,1,0>>
- maximum need <<1,2,3,4>,<3,3,2,2>,<1,3,5,0>>
- process 1 requests 1 of resource 1
- not the most efficient approach but it gets the job done ;)
- I hope you find this repo helpful for your next project.
- Feel free to contact me anytime 😊 will be glad to answer any questions or implement your suggestions for a more efficient optimized code 😄