You are on page 1of 11

Ques. 11.

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\tInstances before independent request for resources\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("\n\t\t\tSafe sequece Exists\n");

printf("\t======================================================\n");

for(a=0;a<3;a++)

printf("\tP%d %c ",seq[a],' ');

else if(d==1)

{
printf("\n\n\t======================================================");

printf("\n\tNO safe sequence Exists\n");

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("\n\tEnter the resoures of process 0\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("\tEnter the resoures of process 1\n");

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("\n\t\tMax after request for resources\n");

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)

printf("\n\tSafe sequence Exists\n");

for(a=0;a<3;a++)

printf("\tP%d %c ",seq[a],' ');

else if(d==1)

printf("\n\tNo safe sequence Exists now\n");

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()

int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10];

int p, r, i, j, process, count;

count = 0;

printf("\t**************************************************************\n");

printf("\t\tPROGRAM TO FIND THE SAFE STATE OF THE SYSTEM");

printf("\n\t*************************************************************\n");

printf("Enter the no of processes : ");

scanf("%d", &p);
for(i = 0; i< p; i++)

completed[i] = 0;

printf("\n\nEnter the no of resources : ");

scanf("%d", &r);

printf("\n\nEnter the Max Matrix for each process : ");

printf("\n-------------------------------------------\n");

for(i = 0; i < p; i++)

printf("\nFor process %d : ", i);

for(j = 0; j < r; j++)

scanf("%d", &Max[i][j]);

printf("\n\nEnter the allocation for each process : ");

printf("\n-------------------------------------------\n");

for(i = 0; i < p; i++)

printf("\nFor process %d : ",i);

for(j = 0; j < r; j++)

scanf("%d", &alloc[i][j]);

printf("\n\nEnter the Available Resources : ");

for(i = 0; i < r; i++)

scanf("%d", &avail[i]);

for(i = 0; i < p; i++)


for(j = 0; j < r; j++)

need[i][j] = Max[i][j] - alloc[i][j];

do

printf("\n---------------\t-----------------");

printf("\n Max matrix:\tAllocation matrix:\n");

printf("---------------\t-----------------\n");

for(i = 0; i < p; i++)

for( j = 0; j < r; j++)

printf("%d ", Max[i][j]);

printf("\t\t");

for( j = 0; j < r; j++)

printf("%d ", alloc[i][j]);

printf("\n");

process = -1;

for(i = 0; i < p; i++)

if(completed[i] == 0)

process = i ;

for(j = 0; j < r; j++)

if(avail[j] < need[i][j])

process = -1;
break;

if(process != -1)

break;

if(process != -1)

printf("\n================================");

printf("\nProcess %d runs to completion!", process );

printf("\n================================\n");

safeSequence[count] = process + 1;

count++;

for(j = 0; j < r; j++)

avail[j] += alloc[process][j];

alloc[process][j] = 0;

Max[process][j] = 0;

completed[process] = 1;

while(count != p && process != -1);

if(count == p)

printf("\t**************************************************************\n");

printf("\t\t\tTHE SYSTEM IS IN SAFE STATE");


printf("\n\t*************************************************************\n");

printf("\n");

printf("Safe Sequence : < ");

for( i = 0; i < p; i++)

printf("process[%d] ", safeSequence[i]-1);

printf(">\n");

printf("\n\t==============================================================\n");

else

printf("\t**************************************************************\n");

printf("\t\t\tTHE SYSTEM IS NOT IN SAFE STATE");

printf("\n\t*************************************************************\n");

You might also like