0% found this document useful (0 votes)
154 views

Assignment 4 (C Program For Banker's Algorithm)

The document is a C program code for implementing the Banker's algorithm. It contains code to input the number of resources and maximum instances of each resource, allocate processes to resources and calculate the need matrix, and implement the Banker's algorithm to find a safe sequence of processes that avoids deadlock. The code takes input for allocation matrix, calculates available resources and need matrix, and uses a label and loop to find processes that can complete and output the safe sequence.

Uploaded by

Rakshita Godiyal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
154 views

Assignment 4 (C Program For Banker's Algorithm)

The document is a C program code for implementing the Banker's algorithm. It contains code to input the number of resources and maximum instances of each resource, allocate processes to resources and calculate the need matrix, and implement the Banker's algorithm to find a safe sequence of processes that avoids deadlock. The code takes input for allocation matrix, calculates available resources and need matrix, and uses a label and loop to find processes that can complete and output the safe sequence.

Uploaded by

Rakshita Godiyal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

ASSIGNMENT 4

NAME: RAKSHITA GODIYAL


CLASS: TE
SUBJECT: SPOS
ROLL NO: 10

Write a C program for Banker’s algorithm

#include<stdio.h>
#include<conio.h>
void main() {
int
k=0,output[10],d=0,t=0,ins[5],i,avail[5],allocated[10][5],need[10][5],MAX[10][5],pno,P[10],j,rz,
count=0;
printf("\n Enter the number of resources : ");
scanf("%d", &rz);
printf("\n enter the max instances of each resources\n");
for (i=0;i<rz;i++) {
avail[i]=0;
printf("%c= ",(i+97));
scanf("%d",&ins[i]);
}
printf("\n Enter the number of processes : ");
scanf("%d", &pno);
printf("\n Enter the allocation matrix \n ");
for (i=0;i<rz;i++)
printf(" %c",(i+97));
printf("\n");
for (i=0;i <pno;i++) {
P[i]=i;
printf("P[%d] ",P[i]);
for (j=0;j<rz;j++) {
scanf("%d",&allocated[i][j]);
avail[j]+=allocated[i][j];
}
}
printf("\nEnter the MAX matrix \n ");
for (i=0;i<rz;i++) {
printf(" %c",(i+97));
avail[i]=ins[i]-avail[i];
}
printf("\n");
for (i=0;i <pno;i++) {
printf("P[%d] ",i);
for (j=0;j<rz;j++)
scanf("%d", &MAX[i][j]);
}
printf("\n");
A: d=-1;
for (i=0;i <pno;i++) {
count=0;
t=P[i];
for (j=0;j<rz;j++) {
need[t][j] = MAX[t][j]-allocated[t][j];
if(need[t][j]<=avail[j])
count++;
}
if(count==rz) {
output[k++]=P[i];
for (j=0;j<rz;j++)
avail[j]+=allocated[t][j];
} else
P[++d]=P[i];
}
if(d!=-1) {
pno=d+1;
goto A;
}
printf("\t <");
for (i=0;i<k;i++)
printf(" P[%d] ",output[i]);
printf(">");
getch();
}
OUTPUT:

You might also like