Introduction
Normally, Banker's algorithm is used to manage operating system process & their resources. Same idea is used here. Process are treated as "Projects" and resources are treated as "Project resources". for e.g. In "P1" project, 3 c++, 2 Java, 4 dot net resources are required & so on. This code takes "Claimed resources","Allocated resources" & "Available resources" as Input and gives proper project completion sequence as output. If unsafe state is occurred then, program will give you suggestion to hire number of employees of particular resources based on logic so that minimum number of resources have to be hired.
Background
The Banker's algorithm is a resource allocation and deadlock avoidance algorithm developed by Edsger Dijkstra that tests for safety by simulating the allocation of predetermined maximum possible amounts of all resources, and then makes an "s-state" check to test for possible deadlock conditions for all other pending activities, before deciding whether allocation should be allowed to continue.
Output
In the following output, code takes the number of processes, number of resources, for each process required/claimed resources, allocated resources for each project, available resources as input & gives the proper sequence of completion of all projects as output (Following is an example of safe state).
In the following output, code evaluates the allocated & claimed resources & evaluates unsafe state. It tells user to hire particular number of employees to complete projects in the efficient & proper sequence.
Using the code
Following code gives the logic to schedule project:
for(i=0;i<np;i++)
{
if(chkpcmp[i]==0) {
for(int j=0;j<nr;)
{
for(int y=0;y<nr;y++)
{
if(clm[i][y]==0)
count++;
else {count=0;break;}
}
if(count==nr) {count=0;break;}
if((clm[i][j]-allo[i][j])<=avl[j]) {
if(j==nr-1)
{
flag=1;
fc++;
for(int k=0;k<nr;k++) {avl[k]=avl[k]+allo[i][k];}
for(int k=0;k<nr;k++) {allo[i][k]=0;}
for(int k=0;k<nr;k++) {clm[i][k]=0;}
cout<<"\nProject "<<"P"<<i+1<<" is completed\n";
In the unsafe state, claimed/required resources are greater than the available. In such cases, we have to hire more resources to complete the projects. Following code snippet suggests to hire minimum number of employees in order to complete all the projects.
for(cp=0;cp<np;cp++)
{
if(chkpcmp[cp]==0) {
for(int j=0;j<nr;j++) {
if(clm[cp][j]>allo[cp][j]+avl[j])
a=a+clm[cp][j]-(allo[cp][j]+avl[j]);
}
if(flag2==1)
{minc=a+1;flag2=0;}
if(a<=minc)
{
minc=a;
for(int t=0;t<nr;t++) {
if(clm[cp][t]>allo[cp][t]+avl[t])
tempr[t]=clm[cp][t]-(allo[cp][t]+avl[t]);
}
}
}
}
Points of Interest
We can use this in real life managing Human Resources. In large scale, we can create HTML page as front-end and can use drop down menu to select project & resources and can use SQL/Oracle database to store the project information as back-end. This will be very useful & simple to use.
Future addition
In unsafe state, current program gives the suggestion to hire new resources. We can also add few more option to this.
For e.g. transfer resources from other projects in balanced way. i.e. if we transfer resources from only one project then, it may have some problem, we can not complete projects with the help of few resources. So, job is to tranfer resources from all the projects.
References
http://en.wikipedia.org/wiki/Banker's_algorithm
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.