Lab No 14
Lab No 14
Islamabad Campus
OPERATING SYSTEMS
Submitted by
Name: ________________________________
Submitted to:
91
LAB NO 14: SIMULATE BANKER’S ALGORITHM FOR
DEADLOCK AVOIDANCE
Duration 3 Hours
OS /Tool/Language C compiler
Banker’s Algorithm
The Banker’s Algorithm is a resource allocation that tests the safety by simulating the
allocation of predetermined maximum possible amounts of all resources, and then makes "s-
state" check to test for possible deadlock conditions for all other pending activities, before
deciding whether allocation should be allowed to continue.
When a new process enters the system, it declares the maximum number of instances that are
needed. This number cannot exceed the total number of resources in the system. If the process
can be accommodated based upon the needs of the system, then resources are allocated,
otherwise the process must wait. The algorithm is actually made up of two separate algorithms:
the safety algorithm and the resource allocation algorithm.
1. Work and Finish be the vector of length m and n respectively, Work=Available and
Finish[i] =False.
92
2. Find an i such that both
Finish[i] =False
Need<=Work If no such I exists go to step 4.
3. work= work + Allocation, Finish[i] =True;
4. if Finish[1]=True for all I, then the system is in safe state.
#include<stdio.h>
void main() {
char job[10][10];
int time[10],avail,tem[10],temp[10];
int safe[10];
int ind=1,i,j,q,n,t;
scanf("%d",&n);
for(i=0;i<n;i++) {
scanf("%s%d",&job[i],&time[i]);
93
}
scanf("%d",&avail);
for(i=0;i<n;i++) {
temp[i]=time[i];
tem[i]=i;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++) {
if(temp[i]>temp[j]) {
t=temp[i];
temp[i]=temp[j];
temp[j]=t;
t=tem[i];
tem[i]=tem[j];
tem[j]=t;
for(i=0;i<n;i++) {
q=tem[i];
if(time[q]<=avail) {
safe[ind]=tem[i];
avail=avail-tem[q];
printf("%s",job[safe[ind]]);
ind++;
94
}
else {
for(i=1;i<ind; i++)
printf("%s %d\n",job[safe[i]],time[safe[i]]);
getch();
Input/Output:
Enter no of jobs: 4
Let Request i be request vector for the process Pi, If request i=[j]=k, then process Pi wants k
instances of resource type Rj.
95
Available=Available-Request I;
Allocation I=Allocation +Request I;
Need i=Need i- Request I;
If the resulting resource allocation state is safe, the transaction is completed and process Pi is
allocated its resources. However if the state is unsafe, the Pi must wait for Request i and the
old resource-allocation state is restored.
#include<stdio.h>
#include<string.h>
void main() {
int alloc[10][10],max[10][10];
int avail[10],work[10],total[10];
int i,j,k,n,need[10][10];
int m;
int count=0,c=0;
char finish[10];
96
printf("Enter the no. of processes and resources:");
scanf("%d%d",&n,&m);
for(i=0;i<=n;i++)
finish[i]='n';
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&max[i][j]);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&alloc[i][j]);
printf("Resource vector:");
for(i=0;i<m;i++)
scanf("%d",&total[i]);
for(i=0;i<m;i++)
avail[i]=0;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
avail[j]+=alloc[i][j];
for(i=0;i<m;i++)
work[i]=avail[i];
for(j=0;j<m;j++)
work[j]=total[j]-work[j];
for(i=0;i<n;i++)
97
for(j=0;j<m;j++)
need[i][j]=max[i][j]-alloc[i][j];
A:
for(i=0;i<n;i++) {
c=0;
for(j=0;j<m;j++)
if((need[i][j]<=work[j])&&(finish[i]=='n')) c++;
if(c==m) {
for(k=0;k<m;k++) {
work[k]+=alloc[i][k];
printf("%4d",work[k]);
printf("\n"); finish[i]='y';
count++;
if(count!=n)
goto A;
else
getch();
98
}
Intput
322
613
314
422
100
612
211
002
Resource vector:9 3 6
Output
Process 2 executed?:y
Process 3 executed?:y
Process 4 executed?:y
99
Available resources are: 9 3 6
Process 1 executed?:y
Lab Task(s)
100