Write a program to implement deadlock avoidance algorithm i.e. banker’s algorithm using C++

  The following c++ program will help you to implement the deadlock avoidance algorithm using C++

C++ Program Code

   
 
/*;==========================================
; Title: Write a program to implement deadlock avoidance algorithm i.e. banker's algorithm. 
; Owned: codenaive littleboy8506@  <[email protected]> <[email protected]>
; Contribution:  Mansi bansal
; Programming Language: C++
; Date:   26 Jan 2021
;==========================================*/
 

#include <iostream>
using namespace std;
int main()
{
int i,j;
int ac[5][4]={{0,0,1,2},{1,0,0,0},{1,3,5,4},{0,6,3,2},{0,0,1,4}};
int maxm[5][4]={{0,0,1,2},{1,7,5,0},{2,3,5,6},{0,6,5,2},{0,6,5,6}};
int avail[4]={1,5,2,0};
int av[4]={7,5,2,0};
int need[5][4],ct=0,wt=0;
int po[5],q=0;
cout<<"Allocation Matrix\n";
for(i=0;i<5;i++)
{
	for(j=0;j<4;j++)
	{
		cout<<ac[i][j]<<" ";
	}
	cout<<"\n";
}
cout<<"Maximum Matrix\n";
for(i=0;i<5;i++)
{
	for(j=0;j<4;j++)
	{
		cout<<maxm[i][j]<<" ";
		
	}
	cout<<"\n";
}
cout<<"Availability\n";
for(i=0;i<4;i++)
{
	
		cout<<avail[i]<<" ";
		
	
}
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
need[i][j]=maxm[i][j]-ac[i][j];
}
cout<<"\nNeed matrix\n";
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
cout<<need[i][j]<<" ";
cout<<"\n";
}
cout<<"\n";
int ss[5]={-1,-1,-1,-1,-1},ws[5]={-1,-1,-1,-1,-1};
for(i=0;i<5;i++)
{
if((need[i][0]<=avail[0]) && (need[i][1]<=avail[1]) && (need[i][2]<=avail[2]) && (need[i][3]<=avail[3]))
{
for(j=0;j<4;j++)
{
    avail[j]=avail[j]+ac[i][j];
}
ss[i]=i;
po[q]=i;
q++;
ct++;
}
else
    {
        ws[i]=i;
        wt++;
    }
}
int g,m;
if(wt>0)
for(m=0;m<wt;m++)
{
for(i=0;i<5;i++)
    if(ws[i]!=-1)
        {g=ws[i];
        break;}
if((need[g][0]<=avail[0]) && (need[g][1]<=avail[1]) && (need[g][2]<=avail[2]) && (need[g][3]<=avail[3]))
{
for(j=0;j<4;j++)
{
    avail[j]=avail[j]+ac[g][j];
}
ss[i]=i;
ct++;
wt--;
ws[g]=-1;
po[q]=i;
q++;
}
}
cout<<"Sequence Order:\n<";
for(i=0;i<5;i++)
{
if(po[i]>=0)
cout<<po[i]<<" ";
}
cout<<">";
if(ct!=5)
    cout<<"\nSystem is not in safe state\n";
else
    cout<<"\nSystem is in safe state\n";
cout<<"\nRequests\n";
int req1[4]={0,1,0,0};
int req0[4]={0,0,0,0};
int req2[4]={1,0,0,0};
int req3[4]={0,0,1,0};
int req4[4]={0,1,0,0};
cout<<"P0  0 0 0 0 \n";
cout<<"P1  0 1 0 0 \n";
cout<<"P2  1 0 0 0 \n";
cout<<"P3  0 0 1 0 \n";
cout<<"P4  0 1 0 0 \n";

int rf=0;
for(i=0;i<4;i++)
{
av[i]=av[i]-req0[i];
need[rf][i]=need[rf][i]-req0[i];
ac[rf][i]=ac[rf][i]+req0[i];
}
rf=rf+1;
for(i=0;i<4;i++)
{
av[i]=av[i]-req1[i];
need[rf][i]=need[rf][i]-req1[i];
ac[rf][i]=ac[rf][i]+req1[i];
}
rf=rf+1;
for(i=0;i<4;i++)
{
av[i]=av[i]-req2[i];
need[rf][i]=need[rf][i]-req2[i];
ac[rf][i]=ac[rf][i]+req2[i];
}
rf=rf+1;
for(i=0;i<4;i++)
{
av[i]=av[i]-req3[i];
need[rf][i]=need[rf][i]-req3[i];
ac[rf][i]=ac[rf][i]+req3[i];
}
rf=rf+1;
for(i=0;i<4;i++)
{
av[i]=av[i]-req4[i];
need[rf][i]=need[rf][i]-req4[i];
ac[rf][i]=ac[rf][i]+req4[i];
}


cout<<"\nUpdated Need matrix\n";
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
cout<<need[i][j]<<" ";
cout<<"\n";
}
cout<<"\nUpdated allocation matrix\n";
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
cout<<ac[i][j]<<" ";
cout<<"\n";
}
cout<<"\nUpdated Availability\n";
for(i=0;i<4;i++)
{
cout<<av[i]<<" ";
}
int npo[5];
q=0;
ct=0;
wt=0;
for(i=0;i<5;i++)
{
if((need[i][0]<=av[0]) && (need[i][1]<=av[1]) && (need[i][2]<=av[2]) && (need[i][3]<=av[3]))
{
for(j=0;j<4;j++)
{
    av[j]=av[j]+ac[i][j];
}
ss[i]=i;
npo[q]=i;
q++;
ct++;
}
else
    {
        ws[i]=i;
        wt++;
    }

}
if(wt>0)
for(m=0;m<wt;m++)
{
for(i=0;i<5;i++)
    if(ws[i]!=-1)
        {g=ws[i];
        break;}

if((need[g][0]<=av[0]) && (need[g][1]<=av[1]) && (need[g][2]<=av[2]) && (need[g][3]<=av[3]))
{
for(j=0;j<4;j++)
{
    av[j]=av[j]+ac[g][j];
}
ss[i]=i;
ct++;
wt--;
ws[g]=-1;
npo[q]=i;
q++;
}
}
cout<<"\n\nSequence Order:\n<";
for(i=0;i<5;i++)
{
if(npo[i]>=0)
cout<<npo[i]<<" ";
}
cout<<">";
if(ct!=5)
    cout<<"\nSystem is not in safe state\n";
else
    cout<<"\nSystem is in safe state\n";
}


					

Output

~/admin@codenaive/os $ g++ bank.cpp -o bank 
~/admin@codenaive/os $ ./bank 
Allocation Matrix 
0 0 1 2 
1 0 0 0 
1 3 5 4
0 6 3 2 
0 0 1 4 
Maximum Matrix 
0 0 1 2 
1 7 5 0 
2 3 5 6 
0 6 5 2 
0 6 5 6 
Availability
1 5 2 0
Need matrix 
0 0 0 0
0 7 5 0 
1 0 0 2 
0 0 2 0 
0 6 4 2 
Availability 
1 5 2 0 
Need matrix 
0 0 0 0 
0 7 5 0 
1 0 0 2 
0 0 2 0 
0 6 4 2 

Sequence Order: 
<0 2 3 4 1 > 
System is in safe state 

Requests 
PO 0 0 0 0
P1 0 1 0 0 
P2 1 0 0 0
P3 0 0 1 0
P4 0 1 0 0     
   
Updated Need matrix 
0 0 0 0
0 6 5 0 
0 0 0 2 

Updated allocation matrix 
0 0 1 2 
1 1 0 0 
2 3 5 4 
0 6 4 2 
0 1 1 4 

Updated Availability 
6 3 1 0 

Sequence Order: 
<0 2 3 4 1 > 
System is in safe state 
  
						 

Leave a Reply

Your email address will not be published. Required fields are marked *