[go: up one dir, main page]

0% found this document useful (0 votes)
4 views2 pages

Deadlock

The document contains a C program that implements the Banker's algorithm to determine a safe sequence of process execution in a system with multiple processes and resources. It initializes allocation and maximum matrices for five processes and checks if a safe sequence exists based on available resources. If a safe sequence is found, it prints the sequence; otherwise, it indicates that the system is not safe.

Uploaded by

atavalinamratha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Deadlock

The document contains a C program that implements the Banker's algorithm to determine a safe sequence of process execution in a system with multiple processes and resources. It initializes allocation and maximum matrices for five processes and checks if a safe sequence exists based on available resources. If a safe sequence is found, it prints the sequence; otherwise, it indicates that the system is not safe.

Uploaded by

atavalinamratha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

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

int max[5][3] = {{7, 5, 3}, // P0 // MAX Matrix


{3, 2, 2}, // P1
{9, 0, 2}, // P2
{2, 2, 2}, // P3
{4, 3, 3}}; // P4

int avail[3] = {3, 3, 2}; // Available Resources

int f[n], seq[n], ind = 0;


for (k = 0; k < n; k++) //Initializes all flag in f to 0,
{
f[k] = 0;
}
int need[n][m];
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}
int y = 0;
for (k = 0; k < 5; k++) // loop to find a safe sequence
{
for (i = 0; i < n; i++) // Checking each process
{
if (f[i] == 0)
{
int flag = 0; // Indicates that all resources needed by the process can be satisfied
for (j = 0; j < m; j++)
{
if (need[i][j] > avail[j])
{
flag = 1; // Indicates that at least one resource type needed by the process exceeds the
currently available resources

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);
}

You might also like