Implement Banker’s Algorithm doc (DOC)
Description
This document contains Lab practicals of Operating System Lab for CSE students, under Kurukshetra University scheme.
Document Sample


PROGRAM NO. -5
Aim: -WAP to Implement Banker’s Algorithm.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
int i,j,p,r,P[5][5],R[5],A[5][5],N[5][5],count;
cout<<"Enter the no. of processes:";
cin>>p;
cout<<"Enter the no. of resources:";
cin>>r;
cout<<"Enter the vlaues of resources:";
for(i=1;i<=r;i++)
cin>>R[i];
cout<<"\nEnter the maximum values of resources:\n";
for(i=1;i<=p;i++)
for(j=1;j<=r;j++)
{
cout<<"P"<<i<<" -->R"<<j<<" :";
cin>>P[i][j];
}
cout<<"\nEnter the resources allocated:\n";
for(i=1;i<=p;i++)
for(j=1;j<=r;j++)
{
cout<<"P"<<i<<" -->R"<<j<<" :";
cin>>A[i][j];
}
cout<<"Calculating the remaining requirement\n";
for(i=1;i<=p;i++)
{
for(j=1;j<=r;j++)
{
if(A[i][j]<R[j])
{
N[i][j]=P[i][j]-A[i][j];
R[j]=R[j]-A[i][j];
cout<<N[i][j];
}
cout<<"\t";
}
cout<<"\n";
}
cout<<"\nChecking for remianing allocation\n";
for(i=1;i<=p;i++)
{
count=0;
for(j=1;j<=r;j++)
{
if(N[i][j]<=R[j])
{
count++;
}
}
if(count==3)
{
for(j=1;j<=r;j++)
{
R[j]=R[j]-N[i][j];
R[j]=R[j]+P[i][j];
N[i][j]=10000;
}
cout<<"\nProcess:P"<<i<<" completed";
for(j=1;j<=r;j++)
cout<<R[j]<<"\t";
cout<<"\n";
i=0;
}
}
getch();
}
OUTPUT
Enter the no. of processes:3
Enter the no. of resources:3
Enter the values of resources:10
5
8
Enter the maximum values of resources:
P1 -->R1 :8
P1 -->R2 :3
P1 -->R3 :5
P2 -->R1 :7
P2 -->R2 :2
P2 -->R3 :6
P3 -->R1 :3
P3 -->R2 :2
P3 -->R3 :3
Enter the resources allocated:
P1 -->R1 :4
P1 -->R2 :2
P1 -->R3 :3
P2 -->R1 :0
P2 -->R2 :1
P2 -->R3 :0
P3 -->R1 :2
P3 -->R2 :1
P3 -->R3 :2
Process:P1 completed
Process:P2 completed
Process:P3 completed
Get documents about "