Implementing Bankers Algorithm in C++

Here is C++ implementation of Bankers Algorithm.

#include<iostream.h>
#include<conio.h>
void main()
{
	int avres[4]={3,1,1,2};
	int alres[4][4]={ {1,2,2,1},{1,1,3,3},{1,1,1,0},{3,2,1,1}};
	int mres[4][4]={ {3,3,2,2},{1,7,3,4},{1,1,5,0},{2,2,3,3}};
	int i,j,r;
	int flagVar=0; 
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{ 
			if( mres[i][j]-alres[i][j]>avres[j])
			{
				flagVar=1;
				r=i;
				break;
			}
		}
	}
	if(r==0)
	{
		cout<<"\n The state is in safe mode and no deadlock happened";
	}
	else
	{
		cout<<"\n The process no "<<r<<" is fails to safe";
	}
	getch();
}

/* Output
 The process no 2 is fails to safe
*/

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.