Wednesday, October 23, 2013

Banker's Safety Algorithm in Operating System


When  a  new  process enters the system, it  must  declare  the  maximum number  of instances of each resource type  that  it may need. This  number  may not  exceed the total  number  of resources  in  the system.  When  a  user  requests a set of resources, the  system  must  determine  whether  the allocation of these resources will leave the system  in  a safe state.  If  it will,  the  resources are allocated; otherwise, the process  must  wait  until  some  other  process releases enough  resources.

Source Code in C :-

 #include <stdio.h>  
 #include <stdlib.h>  
 #include <unistd.h>  
 struct myProcess{  
     int id, alloc, max, need, is_terminated;  
 };  
 typedef struct myProcess process;  
 int main(int argc, char *argv[]){  
     int ret=0;  
     if(argc==2){  
         int total=atoi(argv[1]);  
         if(0==total){  
             fprintf(stderr,"\nerror : type valid number as command line argument...\n");  
             ret=-1;  
         }  
         else{  
             int i=0, j=0, res=0, all_proc_terminated=0, dead=1;  
             process p[total];  
             printf("\nEnter Available Resources : ");  
             scanf("%d",&res);  
             for(i=0;i<total;i++){      
                 again:;  
                 printf("\nProcess%2d: Enter Allocated and Maximum Resources : ",i+1);  
                 scanf("%d%d",&p[i].alloc,&p[i].max);  
                 p[i].need=p[i].max-p[i].alloc;  
                 if(p[i].need < 0){  
                     fprintf(stderr,"\nerror : enter valid values...\n");  
                     goto again;  
                 }  
                 p[i].is_terminated=0;  
                 p[i].id=i+1;                  
             }  
             printf("\n\nTotal Resources : %d\n",res);  
             printf("\nProcess | Allocated | Maximum | Needed");  
             printf("\n--------+-----------+---------+--------\n");  
             for(i=0;i<total;i++){  
                 printf("%7d | %9d | %7d | %6d\n",p[i].id,p[i].alloc,p[i].max,p[i].need);  
             }  
             printf("--------+-----------+---------+--------\n");  
             i=0;  
             printf(" Sequence : ");  
             while(0==all_proc_terminated){  
                 all_proc_terminated=1;  
                 if(0==p[i].is_terminated){  
                     if(p[i].need < res){  
                         res-=p[i].need;  
                         res+=p[i].max;  
                         p[i].is_terminated=1;  
                         printf("P%d, ",p[i].id);  
                     }  
                     else dead++;  
                 }  
                 if(total==dead){  
                     printf("\n---------------------------------------\n");  
                     printf(" DEAD LOCK OCCURED...");  
                     break;  
                 }  
                 for(j=0;j<total;j++){  
                     if(0==p[j].is_terminated){  
                         all_proc_terminated=0;  
                     }  
                 }  
                 i++;  
                 i%=total;  
             }  
             printf("\n---------------------------------------\n");  
         }  
     }  
     else{  
         fprintf(stderr,"\nusage : pass number of process as command line argument...\n");  
         ret=-1;  
     }  
     return ret;  
 }  

 

OUTPUT :-

$ gcc -o banker banker.c
$ ./banker 5

Enter Available Resources : 222

Process 1: Enter Allocated and Maximum Resources : 101 731
Process 2: Enter Allocated and Maximum Resources : 122 422
Process 3: Enter Allocated and Maximum Resources : 410 430
Process 4: Enter Allocated and Maximum Resources : 141 152
Process 5: Enter Allocated and Maximum Resources : 001 422

Total Resources : 222

Process | Allocated | Maximum | Needed
--------+-----------+---------+--------
      1 |       101 |     731 |    630
      2 |       122 |     422 |    300
      3 |       410 |     430 |     20
      4 |       141 |     152 |     11
      5 |         1 |     422 |    421
--------+-----------+---------+--------
 Sequence : P3, P4, P5, P1, P2,
---------------------------------------

No comments:

Post a Comment