#include <stdio.h>
#include <stdlib.h>
int max[ 100 ] [ 100 ] ;
int alloc[ 100 ] [ 100 ] ;
int need[ 100 ] [ 100 ] ;
int avail[ 100 ] ;
int n, r;
void input( ) ;
void show( ) ;
void cal( ) ;
int main( ) {
printf ( "********** Deadlock Detection Algorithm ************\n " ) ; input( ) ;
show( ) ;
cal( ) ;
system ( "pause" ) ; // Replaces getch() return 0 ;
}
void input( ) {
int i, j;
printf ( "Enter the number of processes: " ) ;
printf ( "Enter the number of resource instances: " ) ;
printf ( "Enter the Max Matrix:\n " ) ; for ( i = 0 ; i < n; i++ ) {
for ( j = 0 ; j < r; j++ ) {
}
}
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 Available Resources:\n " ) ; for ( j = 0 ; j < r; j++ ) {
}
}
void show( ) {
int i, j;
printf ( "\n Process\t Allocation\t Max\t Available\n " ) ;
for ( i = 0 ; i < n; i++ ) {
for ( j = 0 ; j < r; j++ ) {
}
for ( j = 0 ; j < r; j++ ) {
}
if ( i == 0 ) {
for ( j = 0 ; j < r; j++ )
}
}
}
void cal( ) {
int finish[ 100 ] , flag = 1 , k, c1 = 0 ;
int dead[ 100 ] , safe[ 100 ] ;
int i, j;
for ( i = 0 ; i < n; i++ )
finish[ i] = 0 ;
// Calculate Need Matrix
for ( i = 0 ; i < n; i++ ) {
for ( j = 0 ; j < r; j++ ) {
need[ i] [ j] = max[ i] [ j] - alloc[ i] [ j] ;
}
}
while ( flag) {
flag = 0 ;
for ( i = 0 ; i < n; i++ ) {
int 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] += alloc[ i] [ k] ;
}
finish[ i] = 1 ;
flag = 1 ;
}
}
}
j = 0 ;
flag = 0 ;
for ( i = 0 ; i < n; i++ ) {
if ( finish[ i] == 0 ) {
dead[ j] = i;
j++;
flag = 1 ;
}
}
if ( flag == 1 ) {
printf ( "\n \n System is in Deadlock. The deadlocked processes are:\n " ) ; for ( i = 0 ; i < j; i++ ) {
}
} else {
printf ( "\n No Deadlock Occurred.\n " ) ; }
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCmludCBtYXhbMTAwXVsxMDBdOwppbnQgYWxsb2NbMTAwXVsxMDBdOwppbnQgbmVlZFsxMDBdWzEwMF07CmludCBhdmFpbFsxMDBdOwppbnQgbiwgcjsKCnZvaWQgaW5wdXQoKTsKdm9pZCBzaG93KCk7CnZvaWQgY2FsKCk7CgppbnQgbWFpbigpIHsKICAgIHByaW50ZigiKioqKioqKioqKiBEZWFkbG9jayBEZXRlY3Rpb24gQWxnb3JpdGhtICoqKioqKioqKioqKlxuIik7CiAgICBpbnB1dCgpOwogICAgc2hvdygpOwogICAgY2FsKCk7CiAgICBzeXN0ZW0oInBhdXNlIik7IC8vIFJlcGxhY2VzIGdldGNoKCkKICAgIHJldHVybiAwOwp9Cgp2b2lkIGlucHV0KCkgewogICAgaW50IGksIGo7CiAgICBwcmludGYoIkVudGVyIHRoZSBudW1iZXIgb2YgcHJvY2Vzc2VzOiAiKTsKICAgIHNjYW5mKCIlZCIsICZuKTsKCiAgICBwcmludGYoIkVudGVyIHRoZSBudW1iZXIgb2YgcmVzb3VyY2UgaW5zdGFuY2VzOiAiKTsKICAgIHNjYW5mKCIlZCIsICZyKTsKCiAgICBwcmludGYoIkVudGVyIHRoZSBNYXggTWF0cml4OlxuIik7CiAgICBmb3IgKGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgZm9yIChqID0gMDsgaiA8IHI7IGorKykgewogICAgICAgICAgICBzY2FuZigiJWQiLCAmbWF4W2ldW2pdKTsKICAgICAgICB9CiAgICB9CgogICAgcHJpbnRmKCJFbnRlciB0aGUgQWxsb2NhdGlvbiBNYXRyaXg6XG4iKTsKICAgIGZvciAoaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICBmb3IgKGogPSAwOyBqIDwgcjsgaisrKSB7CiAgICAgICAgICAgIHNjYW5mKCIlZCIsICZhbGxvY1tpXVtqXSk7CiAgICAgICAgfQogICAgfQoKICAgIHByaW50ZigiRW50ZXIgdGhlIEF2YWlsYWJsZSBSZXNvdXJjZXM6XG4iKTsKICAgIGZvciAoaiA9IDA7IGogPCByOyBqKyspIHsKICAgICAgICBzY2FuZigiJWQiLCAmYXZhaWxbal0pOwogICAgfQp9Cgp2b2lkIHNob3coKSB7CiAgICBpbnQgaSwgajsKICAgIHByaW50ZigiXG5Qcm9jZXNzXHQgQWxsb2NhdGlvblx0IE1heFx0IEF2YWlsYWJsZVxuIik7CgogICAgZm9yIChpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIHByaW50ZigiUCVkXHQgIiwgaSArIDEpOwogICAgICAgIGZvciAoaiA9IDA7IGogPCByOyBqKyspIHsKICAgICAgICAgICAgcHJpbnRmKCIlZCAiLCBhbGxvY1tpXVtqXSk7CiAgICAgICAgfQoKICAgICAgICBwcmludGYoIlx0Iik7CiAgICAgICAgZm9yIChqID0gMDsgaiA8IHI7IGorKykgewogICAgICAgICAgICBwcmludGYoIiVkICIsIG1heFtpXVtqXSk7CiAgICAgICAgfQoKICAgICAgICBwcmludGYoIlx0Iik7CiAgICAgICAgaWYgKGkgPT0gMCkgewogICAgICAgICAgICBmb3IgKGogPSAwOyBqIDwgcjsgaisrKQogICAgICAgICAgICAgICAgcHJpbnRmKCIlZCAiLCBhdmFpbFtqXSk7CiAgICAgICAgfQogICAgICAgIHByaW50ZigiXG4iKTsKICAgIH0KfQoKdm9pZCBjYWwoKSB7CiAgICBpbnQgZmluaXNoWzEwMF0sIGZsYWcgPSAxLCBrLCBjMSA9IDA7CiAgICBpbnQgZGVhZFsxMDBdLCBzYWZlWzEwMF07CiAgICBpbnQgaSwgajsKCiAgICBmb3IgKGkgPSAwOyBpIDwgbjsgaSsrKQogICAgICAgIGZpbmlzaFtpXSA9IDA7CgogICAgLy8gQ2FsY3VsYXRlIE5lZWQgTWF0cml4CiAgICBmb3IgKGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgZm9yIChqID0gMDsgaiA8IHI7IGorKykgewogICAgICAgICAgICBuZWVkW2ldW2pdID0gbWF4W2ldW2pdIC0gYWxsb2NbaV1bal07CiAgICAgICAgfQogICAgfQoKICAgIHdoaWxlIChmbGFnKSB7CiAgICAgICAgZmxhZyA9IDA7CiAgICAgICAgZm9yIChpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgICAgICBpbnQgYyA9IDA7CiAgICAgICAgICAgIGZvciAoaiA9IDA7IGogPCByOyBqKyspIHsKICAgICAgICAgICAgICAgIGlmICgoZmluaXNoW2ldID09IDApICYmIChuZWVkW2ldW2pdIDw9IGF2YWlsW2pdKSkgewogICAgICAgICAgICAgICAgICAgIGMrKzsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQoKICAgICAgICAgICAgaWYgKGMgPT0gcikgewogICAgICAgICAgICAgICAgZm9yIChrID0gMDsgayA8IHI7IGsrKykgewogICAgICAgICAgICAgICAgICAgIGF2YWlsW2tdICs9IGFsbG9jW2ldW2tdOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZmluaXNoW2ldID0gMTsKICAgICAgICAgICAgICAgIGZsYWcgPSAxOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQoKICAgIGogPSAwOwogICAgZmxhZyA9IDA7CgogICAgZm9yIChpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIGlmIChmaW5pc2hbaV0gPT0gMCkgewogICAgICAgICAgICBkZWFkW2pdID0gaTsKICAgICAgICAgICAgaisrOwogICAgICAgICAgICBmbGFnID0gMTsKICAgICAgICB9CiAgICB9CgogICAgaWYgKGZsYWcgPT0gMSkgewogICAgICAgIHByaW50ZigiXG5cblN5c3RlbSBpcyBpbiBEZWFkbG9jay4gVGhlIGRlYWRsb2NrZWQgcHJvY2Vzc2VzIGFyZTpcbiIpOwogICAgICAgIGZvciAoaSA9IDA7IGkgPCBqOyBpKyspIHsKICAgICAgICAgICAgcHJpbnRmKCJQJWRcdCIsIGRlYWRbaV0pOwogICAgICAgIH0KICAgICAgICBwcmludGYoIlxuIik7CiAgICB9IGVsc2UgewogICAgICAgIHByaW50ZigiXG5ObyBEZWFkbG9jayBPY2N1cnJlZC5cbiIpOwogICAgfQp9Cg==