Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C++

Corporate projects & human resource management using Banker's algorithm

Rate me:
Please Sign up or sign in to vote.
4.61/5 (9 votes)
27 Jul 2014CPOL2 min read 23.3K   739   14   7
This will show the simple and useful way to implement Banker's algorithm in managing & scheduling projects,their resources & to give effective solution.

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:

C++
for(i=0;i<np;i++)
{
    if(chkpcmp[i]==0) {  // Do for those projects for who has not completed yet
    
    
   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 required resources are available
   {
             
      if(j==nr-1)
       {
       flag=1;
      fc++;
       for(int k=0;k<nr;k++)       //After completion,transfer resources into available resources
       {avl[k]=avl[k]+allo[i][k];}
       
       for(int k=0;k<nr;k++)     //After completion, make allocated resources as zero 
       {allo[i][k]=0;}
             
        for(int k=0;k<nr;k++)    //After completion, make claimed resources as zero
       {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.

C++
for(cp=0;cp<np;cp++)
    {
        
          if(chkpcmp[cp]==0// Do for those projects who has not completed yet
          {
              
           for(int j=0;j<nr;j++)     // Summation of all resources
           {
               
               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;}  //Find minimum sum so that to hire minimum number of employees
               
          
           if(a<=minc)
           {
               minc=a;
                              
               for(int t=0;t<nr;t++)      //Store it as per resources
               {
                   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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA13-Aug-14 3:17
professionalȘtefan-Mihai MOGA13-Aug-14 3:17 
GeneralRe: My vote of 5 Pin
Rakesh Waghulde13-Aug-14 23:44
Rakesh Waghulde13-Aug-14 23:44 
GeneralMy vote of 5 Pin
Matth Moestl28-Jul-14 10:21
professionalMatth Moestl28-Jul-14 10:21 
GeneralRe: My vote of 5 Pin
Rakesh Waghulde28-Jul-14 19:34
Rakesh Waghulde28-Jul-14 19:34 
GeneralImages missing Pin
Snorri Kristjansson27-Jul-14 23:51
professionalSnorri Kristjansson27-Jul-14 23:51 
GeneralRe: Images missing Pin
Rakesh Waghulde28-Jul-14 0:01
Rakesh Waghulde28-Jul-14 0:01 
GeneralRe: Images missing Pin
Rakesh Waghulde28-Jul-14 8:15
Rakesh Waghulde28-Jul-14 8:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.