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

LAB - 5 (Banker's) - Code

The document is a C program implementing the Banker's Algorithm for determining a safe sequence of processes to prevent deadlocks in an operating system. It takes input for the maximum resource requirements, currently allocated resources, and available resources for multiple processes and resources. It then calculates the remaining need for each process and checks if the system is in a safe state by determining if the needs of each process can be met by the available resources without causing deadlock. If safe, it outputs the safe sequence of processes.

Uploaded by

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

LAB - 5 (Banker's) - Code

The document is a C program implementing the Banker's Algorithm for determining a safe sequence of processes to prevent deadlocks in an operating system. It takes input for the maximum resource requirements, currently allocated resources, and available resources for multiple processes and resources. It then calculates the remaining need for each process and checks if the system is in a safe state by determining if the needs of each process can be met by the available resources without causing deadlock. If safe, it outputs the safe sequence of processes.

Uploaded by

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

File: /home/sdl/Documents/OSLAB/OSLAB55.

c Page 1 of 2

//Sanket Yelam

#include<stdio.h>

int main(){
int n, m, i, j, y, k, ind = 0, flag = 0;

printf("Enter the Number of Processes: ");


scanf("%d", &n);
printf("Enter the Number of Resources: ");
scanf("%d", &m);

int max[n][m], allocation[n][m], available[m];

// Taking input for MAXIMUM Matrix, ALLOCATION Matrix, and Available Vector

printf("Enter the Maximum Matrix of a Process:\n");


for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
scanf("%d", &max[i][j]);

printf("Enter the Allocated Matrix of a Process:\n");


for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
scanf("%d", &allocation[i][j]);

printf("Enter the Available Vector:\n");


for (i = 0; i < m; i++)
scanf("%d", &available[i]);

// Declaring finish array which will represent the TRUE/FALSE value for the Execution
of Process
// and an ans array for storing the SAFE sequence of the processes

int f[n], ans[n];

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


f[i] = 0; // Initialize f[] to 0

// Calculating the NEED Matrix from the MAXIMUM and ALLOCATION Matrix which is NEED =
MAX - ALLOCATION

int need[n][m];

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


for (j = 0; j < m; j++)
need[i][j] = max[i][j] - allocation[i][j];

// Printing ALLOCATION Matrix, NEED Matrix, and Available vector

printf("\nPrinting ALLOCATION Matrix:\n");


for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
printf("%d\t", allocation[i][j]);
}
printf("\n");
}

printf("\nPrinting NEED Matrix:\n");


for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
printf("%d\t", need[i][j]);
}
printf("\n");
}

printf("\nPrinting AVAILABLE VECTOR:\n");


File: /home/sdl/Documents/OSLAB/OSLAB55.c Page 2 of 2

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


printf("%d\t", available[i]);

printf("\n\n");

// Implementing the Banker's Algorithm

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


for (i = 0; i < n; i++) {
if (f[i] == 0) {
flag = 0;
for (j = 0; j < m; j++) {
if (need[i][j] > available[j]) {
flag = 1;
break;
}
}
if (flag == 0) {
ans[ind++] = i;
for (y = 0; y < m; y++)
available[y] += allocation[i][y];
f[i] = 1;
}
}
}
}

flag = 1;
for (i = 0; i < n; i++) {
if (f[i] == 0) {
flag = 0;
printf("\nFollowing System is NOT in Safe Sequence\n");
break;
}
}

if (flag == 1) {
printf("\nFollowing System is in SAFE STATE\n\n");
printf("The Following is the SAFE SEQUENCE that will be executed according to the
Banker's Algorithm:\n\n");
for (i = 0; i < n - 1; i++) {
printf("P%d -> ", ans[i]);
}
printf("P%d\n", ans[n - 1]);
}

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy