AIM:
To write a LINUX/UNIX C Program for the Implementation of Banker's Algorithm in CS1254 - Operating Systems Laboratory.
SOURCE CODE:
#include<stdio.h>
struct process
{
int allocation[3];
int max[3];
int need[3];
int finish;
}p[10];
int main()
{
int n,i,j,avail[3],work[3],flag,count=0,sequence[10],k=0;
printf(“\n Enter the number of process:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nEnter the %dth process allocated resources”,i);
scanf(“%d%d%d”,&p[i].allocation[0],&p[i].allocation[1],&p[i].allocation[2]);
printf(“\n Enter the %d process maxium resources:”,i);
scanf(“%d%d%d”,&p[i].max[0],&p[i].max[1],&p[i].max[2]);
p[i].finish=0;
p[i].need[0]=p[i].max[0]-p[i].allocation[0];
p[i].need[0]=p[i].max[1]-p[i].allocation[1];
p[i].need[0]=p[i].max[2]-p[i].allocation[2];
}
printf(“\n Enter the available vector:”);
scanf(“%d%d%d”,&avail[1],&avail[2]);
for(i=0;i<3;i++);
work[i]=avail[i];
while(count!=n)
{
count=0
for(i=0;i<n;i++)
{
flag=1;
if(p[i].finish==0)
if(p[i].need[0]<=work[0])
if(p[i].need[1]<=work[1])
if(p[i].need[2]<=work[2])
{
for(j=0;j<3;j++)
work[j]+=p[i].allocation[j]
p[i].finish=1;
sequence[k++]=I;
flag=0;
}
if(flag==1)
count++;
}
}
for(i=0;i<n;i++)
if(p]i].finish==1)
count++;
printf(“\n The safe sequence is:\t”);
if(count++n)
for(i=0;i<k;i++)
printf(“ %d\t”,sequence[i]);
else
printf(“SYSTEM IS NOT IN A SAFE STATE \n\n”);
return 0;
}
OUTPUT:
[examuser35@localhost Jebastin]$ cc ban.c
[examuser35@localhost Jebastin]$ ./a.out
Enter the number of process:3
Enter the 0th process allocated resources:1 2 3
Enter the 0th process maximum resources:4 5 6
Enter the 1th process allocated resources:3 4 5
Enter the 1th process maximum resources:6 7 8
Enter the 2th process allocated resources:1 2 3
Enter the 2th process maximum resources:3 4 5
Enter the available vector:10 12 11
The state sequence :SYSTEM IS NOT IN A SAFE STATE
To write a LINUX/UNIX C Program for the Implementation of Banker's Algorithm in CS1254 - Operating Systems Laboratory.
SOURCE CODE:
#include<stdio.h>
struct process
{
int allocation[3];
int max[3];
int need[3];
int finish;
}p[10];
int main()
{
int n,i,j,avail[3],work[3],flag,count=0,sequence[10],k=0;
printf(“\n Enter the number of process:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nEnter the %dth process allocated resources”,i);
scanf(“%d%d%d”,&p[i].allocation[0],&p[i].allocation[1],&p[i].allocation[2]);
printf(“\n Enter the %d process maxium resources:”,i);
scanf(“%d%d%d”,&p[i].max[0],&p[i].max[1],&p[i].max[2]);
p[i].finish=0;
p[i].need[0]=p[i].max[0]-p[i].allocation[0];
p[i].need[0]=p[i].max[1]-p[i].allocation[1];
p[i].need[0]=p[i].max[2]-p[i].allocation[2];
}
printf(“\n Enter the available vector:”);
scanf(“%d%d%d”,&avail[1],&avail[2]);
for(i=0;i<3;i++);
work[i]=avail[i];
while(count!=n)
{
count=0
for(i=0;i<n;i++)
{
flag=1;
if(p[i].finish==0)
if(p[i].need[0]<=work[0])
if(p[i].need[1]<=work[1])
if(p[i].need[2]<=work[2])
{
for(j=0;j<3;j++)
work[j]+=p[i].allocation[j]
p[i].finish=1;
sequence[k++]=I;
flag=0;
}
if(flag==1)
count++;
}
}
for(i=0;i<n;i++)
if(p]i].finish==1)
count++;
printf(“\n The safe sequence is:\t”);
if(count++n)
for(i=0;i<k;i++)
printf(“ %d\t”,sequence[i]);
else
printf(“SYSTEM IS NOT IN A SAFE STATE \n\n”);
return 0;
}
OUTPUT:
[examuser35@localhost Jebastin]$ cc ban.c
[examuser35@localhost Jebastin]$ ./a.out
Enter the number of process:3
Enter the 0th process allocated resources:1 2 3
Enter the 0th process maximum resources:4 5 6
Enter the 1th process allocated resources:3 4 5
Enter the 1th process maximum resources:6 7 8
Enter the 2th process allocated resources:1 2 3
Enter the 2th process maximum resources:3 4 5
Enter the available vector:10 12 11
The state sequence :SYSTEM IS NOT IN A SAFE STATE
EmoticonEmoticon