[go: up one dir, main page]

0% found this document useful (0 votes)
26 views4 pages

Bankers Program

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

Bankers Program

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

#include <stdio.

h>

#define P 5 // Number of processes


#define R 3 // Number of resource types

// Function to check if a system is in a safe state


int isSafe(int processes[], int avail[], int max[][R],
int alloc[][R]) {
int need[P][R], finish[P], safeSeq[P], work[R];

// Calculate the Need matrix (Need = Max - Alloc)


for (int i = 0; i < P; i++) {
for (int j = 0; j < R; j++) {
need[i][j] = max[i][j] - alloc[i][j];
}
}

// Initialize the work and finish arrays


for (int i = 0; i < R; i++) {
work[i] = avail[i];
}
for (int i = 0; i < P; i++) {
finish[i] = 0;
}

int count = 0; // Number of processes finished

while (count < P) {


int found = 0;
for (int p = 0; p < P; p++) {
if (finish[p] == 0) { // Process not
finished yet
int canProceed = 1;
for (int j = 0; j < R; j++) {
if (need[p][j] > work[j]) {
canProceed = 0;
break;
}
}

if (canProceed) {
for (int k = 0; k < R; k++) {
work[k] += alloc[p][k]; //
Simulate resource release
}
safeSeq[count++] = p;
finish[p] = 1;
found = 1;
}
}
}

if (!found) {
printf("The system is not in a safe state.\
n");
return 0;
}
}

// If we found a safe sequence


printf("The system is in a safe state.\nSafe
sequence is: ");
for (int i = 0; i < P; i++) {
printf("P%d ", safeSeq[i]);
}
printf("\n");
return 1;
}

int main() {
int processes[] = {0, 1, 2, 3, 4}; // Process
identifiers

// Available instances of resources


int avail[] = {3, 3, 2};

// Maximum demand of each process for each resource


int max[P][R] = {
{7, 5, 3},
{3, 2, 2},
{9, 0, 2},
{2, 2, 2},
{4, 3, 3}
};

// Resources currently allocated to each process


int alloc[P][R] = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 2},
{2, 1, 1},
{0, 0, 2}
};

// Check if the system is in a safe state


isSafe(processes, avail, max, alloc);

return 0;
}

Explanation:

1. Processes: We assume there are 5 processes (P = 5) and 3 types of


resources (R = 3).
2. Max Matrix: The max matrix represents the maximum resources each
process may request.
3. Alloc Matrix: The alloc matrix shows the resources currently allocated to
each process.
4. Available Resources: The avail array contains the available instances of
each resource type.
5. Need Matrix: The program calculates the need matrix using the formula:

Need[i][j]=Max[i][j]−Alloc[i][j]\text{Need}[i][j] = \text{Max}[i][j] - \
text{Alloc}[i][j]Need[i][j]=Max[i][j]−Alloc[i][j]

6. Safety Algorithm: The program simulates resource allocation by checking


if all processes can finish in a safe sequence. If a safe sequence is found, the
system is declared to be in a safe state.
7. Output: The program outputs the safe sequence of process execution if the
system is in a safe state, or indicates that the system is unsafe.
Sample Output:

csharp
Copy code
The system is in a safe state.
Safe sequence is: P1 P3 P4 P0 P2

You might also like