Banker's Algorithm


C Program For Banker's Algorithm Source Code: #include<stdio.h> #include<stdlib.h> void copy(int a[20][20],int b[20][20],int c,int d) { int l1,l2; for(l1=1;l1<=d;l1++) { for(l2=1;l2<=c;l2++) { b[l1][l2]=a[l1][l2]; } } } void show(int a[20][20],int c,int d) { int l1,l2; for(l1=1;l1<=c;l1++) { for(l2=1;l2<=d;l2++) { printf(" %d",a[l1][l2]); } printf("\n"); } printf("\n----------------------\n"); } void copy2(int a[20],int b[20],int c) { int l1; for(l1=1;l1<=c;l1++) { b[l1]=a[l1]; } } void show2(int a[20],int c) { int l1; for(l1=1;l1<=c;l1++) { printf(" %d",a[l1]); } printf("\n-------------------------\n"); } int safetyalgo(int m,int n,int max[20][20],int all[20][20],int need[20][20], int ava[20],int seq[20],int send) { int work[20],finish[20],c=0,f=0,f0=0,sem,i,j,g,ml,cd,s=0,ct=0; for(i=1;i<=m;i++) { work[i]=ava[i]; } for(i=1;i<=n;i++) { finish[i]=0; } printf("\nMaximum need\n"); show(max,n,m); printf("\nAllocations\n"); show(all,n,m); printf("\nAvailable\n"); show2(work,m); printf("\nNeed\n"); show(need,n,m); if(send==0) printf("\nSteps to check if current system is safe or not\n"); else printf("\nSteps for allocating resources\n"); while(f!=1 && f0!=1) { cd=0; for(i=1;i<=n;i++) { if(need[i][1]<=work[1] && need[i][2]<=work[2] && need[i][3]<=work[3] && finish[i]==0) { for(j=1;j<=m;j++) { work[j]=work[j]+all[i][j]; } finish[i]=1; s++; seq[s]=i; printf("\n\nStep=%d",s); printf("\nwork\n"); show2(work,m); printf("\nfinish\n"); show2(finish,n); cd=0; i=0; } else cd++; if(cd==n) { f0=1; break; } c=0; for(ml=1;ml<=n;ml++) { if(finish[ml]==1) { c++; } } if(c==n) { f=1; break; } } } if(f==1) { sem=1; } if(f0==1) { sem=0; } return sem; } int ralgo(int m,int n,int max[20][20],int all[20][20],int need[20][20], int ava[20],int req[20],int seq[20],int id) { int i,j,get1; if(req[1]<=need[id][1] && req[2]<=need[id][2] && req[3]<=need[id][3]) { if(req[1]<=ava[1] && req[2]<=ava[2] && req[3]<=ava[3]) { for(j=1;j<=m;j++) { need[id][j]=need[id][j]-req[j]; all[id][j]=all[id][j]+req[j]; ava[j]=ava[j]-req[j]; } } else { printf("\nError2\n"); exit(0); } } else { printf("Error1\n"); exit(0); } get1=safetyalgo(m,n,max,all,need,ava,seq,1); return get1; } void main() { printf("\n------------BANKER'S ALGORITHM-----------\n"); int m,n,max[20][20],all[20][20],need[20][20],ava[20],work[20],i,j,get,req[20], id,get1,seq[20]; int m1,n1,max1[20][20],all1[20][20],need1[20][20],ava1[20],work1[20], finish1[20],req1[20],id1; printf("\nEnter number of processes:"); scanf("%d",&n); m=3; printf("\nNumber of resource types=3 (defined by the system)"); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { printf("\nEnter resource-%d's maximum need of process P%d:",j,i); scanf("%d",&max[i][j]); } } for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { printf("\nEnter allocation of resource-%d for process P%d:",j,i); scanf("%d",&all[i][j]); } } for(i=1;i<=m;i++) { printf("\nEnter availability of resource-%d in system:",i); scanf("%d",&ava[i]); } printf("\nEnter the process id of the requesting process:"); scanf("%d",&id); printf("\nEnter number of requests for each processes:"); for(i=1;i<=m;i++) { scanf(" %d",&req[i]); } for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { need[i][j]=max[i][j]-all[i][j]; } } id1=id; n1=n; m1=m; copy(max,max1,m,n); copy(need,need1,m,n); copy(all,all1,m,n); copy2(ava,ava1,m); copy2(work,work1,m); copy2(req,req1,m); printf("\nChecking if the present system is safe or not by safety algorithm\n"); get=safetyalgo(m,n,max,all,need,ava,seq,0); if(get==0) printf("\nThe system is in unsafe state,resources could not be allocated to the processes\n"); else { printf("\nThe present system is in safe state.resources could be allocated\n"); printf("\nSafe Sequence\n"); for(i=1;i<=n;i++) { printf(" P%d",seq[i]); } printf("\nAllocating resources by resource allocation algorithm\n"); get1=ralgo(m1,n1,max1,all1,need1,ava1,req1,seq,id1); if(get1==1) { printf("\nAllocation successful\n"); printf("\nSafe Sequence\n"); for(i=1;i<=n;i++) { printf(" P%d",seq[i]); } } else printf("\nSystem leads to unsafe state.Allocation should be rolled back\n"); } } OUTPUT [Rakesh@localhost ~]$ cc -o k bankers.c [Rakesh@localhost ~]$ ./k ------------BANKER'S ALGORITHM----------- Enter number of processes:5 Number of resource types=3 (defined by the system) Enter resource-1's maximum need of process P1:7 Enter resource-2's maximum need of process P1:5 Enter resource-3's maximum need of process P1:3 Enter resource-1's maximum need of process P2:3 Enter resource-2's maximum need of process P2:2 Enter resource-3's maximum need of process P2:2 Enter resource-1's maximum need of process P3:9 Enter resource-2's maximum need of process P3:0 Enter resource-3's maximum need of process P3:2 Enter resource-1's maximum need of process P4:2 Enter resource-2's maximum need of process P4:2 Enter resource-3's maximum need of process P4:2 Enter resource-1's maximum need of process P5:4 Enter resource-2's maximum need of process P5:3 Enter resource-3's maximum need of process P5:3 Enter allocation of resource-1 for process P1:0 Enter allocation of resource-2 for process P1:1 Enter allocation of resource-3 for process P1:0 Enter allocation of resource-1 for process P2:2 Enter allocation of resource-2 for process P2:0 Enter allocation of resource-3 for process P2:0 Enter allocation of resource-1 for process P3:3 Enter allocation of resource-2 for process P3:0 Enter allocation of resource-3 for process P3:2 Enter allocation of resource-1 for process P4:2 Enter allocation of resource-2 for process P4:1 Enter allocation of resource-3 for process P4:1 Enter allocation of resource-1 for process P5:0 Enter allocation of resource-2 for process P5:0 Enter allocation of resource-3 for process P5:2 Enter availability of resource-1 in system:3 Enter availability of resource-2 in system:3 Enter availability of resource-3 in system:2 Enter the process id of the requesting process:2 Enter number of requests for each processes:1 0 2 Checking if the present system is safe or not by safety algorithm Maximum need 7 5 3 3 2 2 9 0 2 2 2 2 4 3 3 ---------------------- Allocations 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 ---------------------- Available 3 3 2 ------------------------- Need 7 4 3 1 2 2 6 0 0 0 1 1 4 3 1 ---------------------- Steps to check if current system is safe or not Step=1 work 5 3 2 ------------------------- finish 0 1 0 0 0 ------------------------- Step=2 work 7 4 3 ------------------------- finish 0 1 0 1 0 ------------------------- Step=3 work 7 5 3 ------------------------- finish 1 1 0 1 0 ------------------------- Step=4 work 10 5 5 ------------------------- finish 1 1 1 1 0 ------------------------- Step=5 work 10 5 7 ------------------------- finish 1 1 1 1 1 ------------------------- The present system is in safe state.resources could be allocated Safe Sequence P2 P4 P1 P3 P5 Allocating resources by resource allocation algorithm Maximum need 7 5 3 3 2 2 9 0 2 2 2 2 4 3 3 ---------------------- Allocations 0 1 0 3 0 2 3 0 2 2 1 1 0 0 2 ---------------------- Available 2 3 0 ------------------------- Need 7 4 3 0 2 0 6 0 0 0 1 1 4 3 1 ---------------------- Steps for allocating resources Step=1 work 5 3 2 ------------------------- finish 0 1 0 0 0 ------------------------- Step=2 work 7 4 3 ------------------------- finish 0 1 0 1 0 ------------------------- Step=3 work 7 5 3 ------------------------- finish 1 1 0 1 0 ------------------------- Step=4 work 10 5 5 ------------------------- finish 1 1 1 1 0 ------------------------- Step=5 work 10 5 7 ------------------------- finish 1 1 1 1 1 ------------------------- Allocation successful Safe Sequence P2 P4 P1 P3 P5 [Rakesh@localhost ~]$ ./k ------------BANKER'S ALGORITHM----------- Enter number of processes:5 Number of resource types=3 (defined by the system) Enter resource-1's maximum need of process P1:7 Enter resource-2's maximum need of process P1:5 Enter resource-3's maximum need of process P1:3 Enter resource-1's maximum need of process P2:3 Enter resource-2's maximum need of process P2:2 Enter resource-3's maximum need of process P2:2 Enter resource-1's maximum need of process P3:9 Enter resource-2's maximum need of process P3:0 Enter resource-3's maximum need of process P3:2 Enter resource-1's maximum need of process P4:2 Enter resource-2's maximum need of process P4:2 Enter resource-3's maximum need of process P4:2 Enter resource-1's maximum need of process P5:4 Enter resource-2's maximum need of process P5:3 Enter resource-3's maximum need of process P5:3 Enter allocation of resource-1 for process P1:0 Enter allocation of resource-2 for process P1:1 Enter allocation of resource-3 for process P1:0 Enter allocation of resource-1 for process P2:3 Enter allocation of resource-2 for process P2:0 Enter allocation of resource-3 for process P2:2 Enter allocation of resource-1 for process P3:3 Enter allocation of resource-2 for process P3:0 Enter allocation of resource-3 for process P3:2 Enter allocation of resource-1 for process P4:2 Enter allocation of resource-2 for process P4:1 Enter allocation of resource-3 for process P4:1 Enter allocation of resource-1 for process P5:0 Enter allocation of resource-2 for process P5:0 Enter allocation of resource-3 for process P5:2 Enter availability of resource-1 in system:2 Enter availability of resource-2 in system:3 Enter availability of resource-3 in system:0 Enter the process id of the requesting process:1 Enter number of requests for each processes:0 2 0 Checking if the present system is safe or not by safety algorithm Maximum need 7 5 3 3 2 2 9 0 2 2 2 2 4 3 3 ---------------------- Allocations 0 1 0 3 0 2 3 0 2 2 1 1 0 0 2 ---------------------- Available 2 3 0 ------------------------- Need 7 4 3 0 2 0 6 0 0 0 1 1 4 3 1 ---------------------- Steps to check if current system is safe or not Step=1 work 5 3 2 ------------------------- finish 0 1 0 0 0 ------------------------- Step=2 work 7 4 3 ------------------------- finish 0 1 0 1 0 ------------------------- Step=3 work 7 5 3 ------------------------- finish 1 1 0 1 0 ------------------------- Step=4 work 10 5 5 ------------------------- finish 1 1 1 1 0 ------------------------- Step=5 work 10 5 7 ------------------------- finish 1 1 1 1 1 ------------------------- The present system is in safe state.resources could be allocated Safe Sequence P2 P4 P1 P3 P5 Allocating resources by resource allocation algorithm Maximum need 7 5 3 3 2 2 9 0 2 2 2 2 4 3 3 ---------------------- Allocations 0 3 0 3 0 2 3 0 2 2 1 1 0 0 2 ---------------------- Available 2 1 0 ------------------------- Need 7 2 3 0 2 0 6 0 0 0 1 1 4 3 1 ---------------------- Steps for allocating resources System leads to unsafe state.Allocation should be rolled back

Share:

0 comments