Hello all,
I'm trying to write a program that detects deadlock. Some of the code I already tested in an earlier stage but at this point when I run the code it somehow gets stuck. I believe that it has something to do with a negative integer value. Anyhow, the program should still work if a negative integer comes up. Here's my code so far. I think it's fairly clear on what the idea is, but if you have any questions please let me know.
Code:
#include <stdio.h>
#define FALSE 0
#define TRUE 1
int ALLOC[5][5], REQ[5][5], TOT_RES[5], process, resource, NUMB_RES=5, NUMB_PRO=5;
int TMP[5]={0}, AVAIL[5], CNTR=0, FIN[5]={FALSE}, FLAG;
int main(int argc, char **argv, char **envp)
{
srand(time(NULL));
//Create a randomly generated allocation matrix
//with a value between 0 and 5 for each resource and process
printf("Allocation matrix:\n");
for(process=0;process<5;process++)
{
for(resource=0;resource<5;resource++)
{
ALLOC[process][resource]=rand()%4;
printf("\t%d",ALLOC[process][resource]);
}
printf("\n");
}
//Create a randomly generated request matrix
//with a value between 0 and 5 for each resource and process
printf("\nRequest matrix:\n");
for(process=0;process<5;process++)
{
for(resource=0;resource<5;resource++)
{
REQ[process][resource]=rand()%4;
printf("\t%d",REQ[process][resource]);
}
printf("\n");
}
//Create a randomly generated total resource vector
//with a value between 10 and 15 for each resource and process
printf("\nResources:\n");
for(process=0;process<5;process++)
{
TOT_RES[process]=rand()%3+NUMB_RES+NUMB_PRO;
printf("\t%d",TOT_RES[process]);
}
//display the total number of resources in use
//printf("\n\nTotal claimed per resource:\n");
for(process=0;process<5;process++)
{
for(resource=0;resource<5;resource++)
{
TMP[process]+=ALLOC[resource][process];
}
//printf("\t%d", TMP[process]);
}
//Display the total number of available resources
printf("\n\nAvailable:\n");
for(process=0;process<5;process++)
{
AVAIL[process]=TOT_RES[process]-TMP[process];
printf("\t%d",AVAIL[process]);
}
while(TRUE)
{
for(process=0;process<5;process++)
{
CNTR=0;
if(FIN[process] == FALSE)
{
for(resource=0;resource<5;resource++)
{
//zijn resources beschikbaar?
if(REQ[process][resource]<=AVAIL[resource])
{
//zo ja, verhoog de counter
CNTR++;
}
}
FLAG = FALSE;
//vergelijk counter met aantal resources
//als counter tot 5 is opgelopen zijn alle resources beschikbaar
//en kunnen worden teruggegeven aan de available pool
if(CNTR == NUMB_RES)
{
for(resource=0;resource<5;resource++)
{
//geef resources terug
AVAIL[resource]+=ALLOC[process][resource];
}
FIN[process]=TRUE;
printf("\nProcess number %d is finished\n",process+1);
}
else
FLAG = TRUE;
}
}
CNTR=0;
for(process=0;process<5;process++)
{
if(FIN[process] == TRUE)
{
CNTR++;
}
}
if(CNTR==5)
{
break;
}
}
for(process=0;process<5;process++)
{
if(FIN[process] == FALSE)
{
printf("\n Deadlock detected. Responsible process number is: %d\n",process+1);
return;
}
}
}
The program usually gets stuck after producing output displaying the "Available: " text. After that a list of tabbed numbers should appear, but it doesn't. Any help would be much appreciated.
Greetings,
Deheerwassenaar