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

Os Lab - 9

This C code is an OS lab assignment that checks for a safe sequence of processes using the Banker's Algorithm. It takes the allocation matrix, maximum matrix and available resources as input. It calculates the need matrix, finds a safe sequence of processes that don't violate the resource allocation and checks if the system is in a safe state. If a safe sequence exists, it prints the sequence, otherwise it prints that the system is not safe. The code contains the necessary variables, nested for loops and if-else conditions to implement the Banker's Algorithm logic.

Uploaded by

GUNDA LAXMISAI
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)
58 views4 pages

Os Lab - 9

This C code is an OS lab assignment that checks for a safe sequence of processes using the Banker's Algorithm. It takes the allocation matrix, maximum matrix and available resources as input. It calculates the need matrix, finds a safe sequence of processes that don't violate the resource allocation and checks if the system is in a safe state. If a safe sequence exists, it prints the sequence, otherwise it prints that the system is not safe. The code contains the necessary variables, nested for loops and if-else conditions to implement the Banker's Algorithm logic.

Uploaded by

GUNDA LAXMISAI
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

OS LAB ASSIGNMENT – 9

Gunda Laxmisai
21MIS7119

Code:
#include <stdio.h> int

main()

int n, m, i, j, k;

n = 5;

m = 3;

int alloc[5][4] = { { 2, 0, 1, 2 },

{ 1, 0, 0, 0 },

{ 1, 3, 5, 4 },

{ 0, 6, 3, 2 },

{ 0, 0, 1, 4 } };

int max[5][4] = { { 2, 0, 5, 2 },

{ 1, 7, 5, 0 },

{ 2, 3, 5, 6 },
{ 0, 6, 5, 2 },

{ 0, 6, 5, 6 } };

int avail[4] = { 4, 5, 2, 2 };

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

for (k = 0; k < n; k++)

{ 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++) { for (i =

0; i < n; i++) { if

(f[i] == 0) {

int flag =

0; for (j =

0; j < m; j++) {

if (need[i][j] >

avail[j]){

flag = 1;

break;

if (flag == 0)

{ ans[ind++] = i;

for (y = 0; y < m; y++)


avail[y] += alloc[i][y];

f[i] = 1;

int flag = 1;

for(int i=0;i<n;i++)

{ if(f[i]==

0)

flag=0;

printf("The following system is not safe");

break;

if(flag==1)

printf("SAFE Sequence EXISTS\n");

printf("Following is the SAFE Sequence\n");

for (i = 0; i < n - 1; i++)

printf(" P%d ->", ans[i]);

printf(" P%d", ans[n - 1]);

return (0);
}

You might also like