0% found this document useful (0 votes)
19 views3 pages

Oss 9

Uploaded by

ankush8522109
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)
19 views3 pages

Oss 9

Uploaded by

ankush8522109
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/ 3

Ankush kumar

(8522109)

PROGRAM NO.-9
Objective: Program for Bankers Algorithm.

Description:The Banker's Algorithm is a resource allocation and deadlock


avoidance algorithm used in operating systems, particularly in environments where
multiple processes compete for a finite number of resources. It was developed by
Edsger Dijkstra in the early 1970s.

Source code:
#include<stdio.h>

int main(){

//P0,P1,P2,P3,P4 are the names of Process

int n,r,i,j,k;

n=5;//Indicates the number of processes

r=3;//indicates the number of resources

int alloc[5][3]={{0,0,1},//P0//This is Allocation Matrix

{3,0,0},//P1

{1,0,1},//P2

{2,3,2},//P3

{0,0,3}};//P4

int max[5][3]={{7,6,3},//P0//MAX MATRIX

{3,2,2},//P1

{8,0,2},//P2

{2,1,2},//P3

{5,2,3}};//P4

int avail[3]={2,3,2};//these are available resources

int f[n],ans[n],ind=0;
Ankush kumar
(8522109)

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

f[k];

int need[n][r];

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

for(j=0;j<r;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<r;j++){

if(need[i][j]>avail[j]){

flag=1;

break;

if(flag==0){

ans[ind++]=i;

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

avail[y]+=alloc[i][y];
Ankush kumar
(8522109)

f[i]=1;

printf("The SAFE Sequence is as follows\n");

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

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

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

return 0;

OUTPUT:

You might also like