0% found this document useful (0 votes)
50 views20 pages

Lab Assignment 3: Name-Abhinav Yadav REG N0. - 18BCI0103 Subject - Operating Systems Faculty - Srimathi C

The document contains code for implementing the Banker's algorithm in C to check for safety of processes. It takes input for number of processes, resources, allocation and maximum matrix. It calculates the need of each process and checks if the available resources are sufficient for the requesting process using a flag. If sufficient, it updates available resources and marks that process as safe. Finally, it prints the safe sequence of processes.

Uploaded by

Abhinav Yadav
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)
50 views20 pages

Lab Assignment 3: Name-Abhinav Yadav REG N0. - 18BCI0103 Subject - Operating Systems Faculty - Srimathi C

The document contains code for implementing the Banker's algorithm in C to check for safety of processes. It takes input for number of processes, resources, allocation and maximum matrix. It calculates the need of each process and checks if the available resources are sufficient for the requesting process using a flag. If sufficient, it updates available resources and marks that process as safe. Finally, it prints the safe sequence of processes.

Uploaded by

Abhinav Yadav
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/ 20

LAB ASSIGNMENT 3

NAME- ABHINAV YADAV


REG N0.- 18BCI0103
SUBJECT- OPERATING SYSTEMS
FACULTY- SRIMATHI C
BANKER’S ALGORITHM:

CODE:

#include <stdio.h>

int main()

printf("Enter the number of processes ");

scanf("%d",&n);

printf("\nEnter the number of resources ");

scanf("%d",&m);

int alloc[n][m];

printf("\nEnter the allocation matrix ");

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

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

scanf("%d",&alloc[i][j]);

int max[5][3];

printf("\nEnter the maximum matrix ");

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

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

scanf("%d",&max[i][j]);

int avail[m];
printf("\n Enter the available resources ");

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

scanf("%d",&avail[i]);

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];

printf("Process\t Allocation\t Max\t Available\t");

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

printf("\nP%d\t ",i+1);

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

printf("%d ",alloc[i][j]);

printf("\t");

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

printf("%d ",max[i][j]);
}

printf("\t");

if(i==0)

{ printf(" ");

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

printf("%d ",avail[j]);

}printf("\n");

printf("\n\n\n");

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) {

printf("The available resources are: \t");

for(int z=0;z<m;z++){

printf("%d ",avail[z]);

printf("\n Therefore the selected process is: P%d\t\n",i);

ans[ind++] = i;
for (y = 0; y < m; y++)

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

f[i] = 1;

printf("\n\nFollowing 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);

OUTPUTS:
CLASSICAL PROBLEMS:

 Producer Consumer’s problem:


 Reader’s writer’s process:
 Dinning Philosopher’s problem

You might also like