Professional Documents
Culture Documents
Programs
Programs
Reenas operating system uses an algorithm for deadlock avoidance to manage the
allocation of resources say three namely A, B, and C to three processes P0, P1, and P2. Consider the
following scenario as reference .user must enter the current state of system as given in this example
: Suppose P0 has 0,0,1 instances , P1 is having 3,2,0 instances and P2 occupies 2,1,1 instances of
A,B,C resource respectively. Also the maximum number of instances required for P0 is 8,4,3 and for
p1 is 6,2,0 and finally for P2 there are 3,3,3 instances of resources A,B,C respectively. There are 3
instances of resource A, 2 instances of resource B and 2 instances of resource C available. Write a
program to check whether Reenas operating system is in a safe state or not in the following
independent requests for additional resources in the current state: 1. Request1: P0 requests 0
instances of A and 0 instances of B and 2 instances of C. 2. Request2: P1 requests for 2 instances of
A, 0 instances of B and 0 instances of C. All the request must be given by user as input.
Solution:
#include<stdio.h>
int main()
int inst[3][3]={0,0,1,3,2,0,2,1,1},sum=0;
int max[3][3]={8,4,3,6,2,0,3,3,3};
int d,f=0,count=0,avail[3]={3,2,2},flag[3]={3,2,2},a=0,b,c;
int seq[3],s=0;
printf("\t**************************************************************\n");
printf("\t**************************************************************\n");
printf("\t\t|\tA\t|\tB\t|\tC\t|\n");
for(a=0;a<3;a++)
printf("\tP%d\t|",a);
for(b=0;b<3;b++)
printf("\t%d\t|",inst[a][b]);
printf("\n");
printf("\n");
printf("\t**************************************************************\n");
printf("\t\t\tMax Matrix\n");
printf("\t**************************************************************\n");
printf("\t\t|\tA\t|\tB\t|\tC\t|\n");
for(a=0;a<3;a++)
printf("\tP%d\t|",a);
for(b=0;b<3;b++)
printf("\t%d\t|",max[a][b]);
printf("\n");
a=0;
while(1)
for(b=0;b<3;b++)
if((inst[a][b]+avail[b])>=max[a][b])
sum++;
if(sum==3)
avail[0]+=inst[a][0];
avail[1]+=inst[a][1];
avail[2]+=inst[a][2];
flag[a]=1;
seq[s]=a;
s++;
}
count++;
a=(a+1)%3;
sum=0;
for(c=0;c<3;c++)
if(flag[c]==1)
f++;
if(f==3)
d=0;
break;
if(count>=4)
d=1;
break;
else f=0;
if(d==0)
printf("\n\n\t======================================================");
printf("\t======================================================\n");
for(a=0;a<3;a++)
else if(d==1)
{
printf("\n\n\t======================================================");
printf("\t======================================================\n");
printf("\n\n");
printf("\n\n\tNow taking independent requests for additional resources from user for
process[0] and process[1]\n");
printf("\t------------------------------------------------------------------------------------------------\n");
printf("\tA:\t");
scanf("%d",&s);
max[0][0]+=s;
printf("\tB:\t");
scanf("%d",&s);
max[0][1]+=s;
printf("\tC:\t");
scanf("%d",&s);
max[0][2]+=s;
printf("\tA:\t");
scanf("%d",&s);
max[1][0]+=s;
printf("\tB:\t");
scanf("%d",&s);
max[1][1]+=s;
printf("\tC:\t");
scanf("%d",&s);
max[1][2]+=s;
printf("\t**************************************************************\n");
printf("\n");
printf("\t===============================================================\n");
printf("\t\t\tMax Matrix\n");
printf("\t===============================================================\n");
printf("\t\t|\tA\t|\tB\t|\tC\t|\n");
for(a=0;a<3;a++)
printf("\tP%d\t|",a);
for(b=0;b<3;b++)
printf("\t%d\t|",max[a][b]);
printf("\n");
s=0;
d=0;
a=0;
sum=0;
count=0;
flag[0]=0;
flag[1]=0;
flag[2]=0;
f=0;
avail[0]=3;
avail[1]=2;
avail[2]=2;
while(1)
for(b=0;b<3;b++)
if((inst[a][b]+avail[b])>=max[a][b])
{
sum++;
if(sum==3)
avail[0]+=inst[a][0];
avail[1]+=inst[a][1];
avail[2]+=inst[a][2];
flag[a]=1;
seq[s]=a;
s++;
count++;
a=(a+1)%3;
sum=0;
for(c=0;c<3;c++)
if(flag[c]==1)
f++;
if(f==3)
d=0;
break;
if(count>=4)
d=1;
break;
}
else f=0;
if(d==0)
for(a=0;a<3;a++)
else if(d==1)
Ques. 22. Consider following and Generate a solution to find whether the system is in safe state or
not?
Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
count = 0;
printf("\t**************************************************************\n");
printf("\n\t*************************************************************\n");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;
scanf("%d", &r);
printf("\n-------------------------------------------\n");
scanf("%d", &Max[i][j]);
printf("\n-------------------------------------------\n");
scanf("%d", &alloc[i][j]);
scanf("%d", &avail[i]);
do
printf("\n---------------\t-----------------");
printf("---------------\t-----------------\n");
printf("\t\t");
printf("\n");
process = -1;
if(completed[i] == 0)
process = i ;
process = -1;
break;
if(process != -1)
break;
if(process != -1)
printf("\n================================");
printf("\n================================\n");
safeSequence[count] = process + 1;
count++;
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
if(count == p)
printf("\t**************************************************************\n");
printf("\n");
printf(">\n");
printf("\n\t==============================================================\n");
else
printf("\t**************************************************************\n");
printf("\n\t*************************************************************\n");