Deadlock
Deadlock
h>
int main()
{
// P0, P1, P2, P3, P4 are the Process names here
int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
int alloc[5][3] = {{0, 1, 0}, // P0 // Allocation Matrix
{2, 0, 0}, // P1
{3, 0, 2}, // P2
{2, 1, 1}, // P3
{0, 0, 2}}; // P4
break;
}
}
if (flag == 0) // allocating Resources if Safe
{
seq[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}
int flag = 1; // All processes have been marked as finished
for (int i = 0; i < n; i++) // Checking for Safe state
{
if (f[i] == 0)
{
flag = 0; // At least one process could not finish
printf("The following system is not safe");
break;
}
}
if (flag == 1) // Print the Safe Sequence
{
printf("Following is the SAFE Sequence\n");
for (i = 0; i < n - 1; i++)
printf(" P%d ->", seq[i]);
printf(" P%d", seq[n - 1]);
}
return (0);
}