#include<stdio.h>
#include<stdlib.h>
int main( )
{
int n, r, i, j, k, avail[ 10 ] , alloc[ 10 ] [ 10 ] , max[ 10 ] [ 10 ] , need[ 10 ] [ 10 ] , c= 0 , c1= 0 , finish[ 10 ] , flag= 1 ;
printf ( "enter the number of processes\n " ) ; printf ( "enter the number of resources\n " ) ; printf ( "enter the allocation matrix\n " ) ; for ( i= 0 ; i< n; i++ )
{
for ( j= 0 ; j< r; j++ )
{
scanf ( "%d" ,& alloc
[ i
] [ j
] ) ; }
}
printf ( "enter the max matrix\n " ) ; for ( i= 0 ; i< n; i++ )
{
for ( j= 0 ; j< r; j++ )
{
}
}
printf ( "enter the available matrix\n " ) ; for ( j= 0 ; j< r; j++ )
{
}
for ( i= 0 ; i< n; i++ )
{
for ( j= 0 ; j< r; j++ )
{
need[ i] [ j] = max[ i] [ j] - alloc[ i] [ j] ;
}
}
for ( i= 0 ; i< n; i++ )
{
finish[ i] = 0 ;
}
while ( flag)
{
flag= 0 ;
for ( i= 0 ; i< n; i++ )
{
c= 0 ;
for ( j= 0 ; j< r; j++ )
{
if ( ( finish[ i] == 0 ) && ( need[ i] [ j] <= avail[ j] ) )
{
c++;
if ( c== r)
{
for ( k= 0 ; k< r; k++ )
{
avail[ k] = avail[ k] + alloc[ i] [ k] ;
}
flag= 1 ;
finish[ i] = 1 ;
i= n;
}
}
}
}
}
for ( i= 0 ; i< n; i++ )
{
if ( finish[ i] == 1 )
{
c1++;
}
else
{
}
}
if ( c1== n)
{
printf ( "\n system is in safe state\n " ) ; }
else
{
printf ( "\n system is not in safe state\n " ) ; }
}
I2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8c3RkbGliLmg+CmludCBtYWluKCkKewoJaW50IG4scixpLGosayxhdmFpbFsxMF0sYWxsb2NbMTBdWzEwXSxtYXhbMTBdWzEwXSxuZWVkWzEwXVsxMF0sYz0wLGMxPTAsZmluaXNoWzEwXSxmbGFnPTE7CglwcmludGYoImVudGVyIHRoZSBudW1iZXIg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