Question
Need the following : Need the following : I need below code to be dry run on paper as well as I need comments on
Need the following : Need the following :
I need below code to bedry runon paper as well as I needcommentson the code.
#include
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("Enter the no of processes : ");
scanf("%d", &p);
for(i = 0; i
completed[i] = 0;
printf(" Enter the no of resources : ");
scanf("%d", &r);
printf(" Enter the Max Matrix for each process : ");
for(i = 0; i
{
printf(" For process %d : ", i + 1);
for(j = 0; j
scanf("%d", &Max[i][j]);
}
printf(" Enter the allocation for each process : ");
for(i = 0; i
{
printf(" For process %d : ",i + 1);
for(j = 0; j
scanf("%d", &alloc[i][j]);
}
printf(" Enter the Available Resources : ");
for(i = 0; i
scanf("%d", &avail[i]);
for(i = 0; i
for(j = 0; j
need[i][j] = Max[i][j] - alloc[i][j];
do
{
printf(" Max matrix:\tAllocation matrix: ");
for(i = 0; i
{
for( j = 0; j
printf("%d ", Max[i][j]);
printf("\t\t");
for( j = 0; j
printf("%d ", alloc[i][j]);
printf(" ");
}
process = -1;
for(i = 0; i
{
if(completed[i] == 0)//if not completed
{
process = i ;
for(j = 0; j
{
if(avail[j]
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}
if(process != -1)
{
printf(" Process %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
count++;
for(j = 0; 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(" The system is in a safe state!! ");
printf("Safe Sequence :
for( i = 0; i
printf("%d ", safeSequence[i]);
printf("> ");
}
else
printf(" The system is in an unsafe state!!");
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started