Important alert: (current site time 10/26/2014 1:34:05 AM EDT)

VB icon

Banker's algorithm

Submitted on: 8/5/2011 8:20:37 AM
By: Puneet Jindal 
Level: Advanced
User Rating: Unrated
Compatibility: UNIX C++
Views: 8674
     Illustrate banker's algorithm
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
// Name: Banker's algorithm
// Description:Illustrate banker's algorithm
// By: Puneet Jindal
//This code is copyrighted and has// limited warranties.Please see details.//**************************************

using namespace std;
//	Tells whether a given sequence is safe or not
class Banker
	int total_processes;
	int remaining_processes;
	int A_avail,B_avail,C_avail;
	int A,B,C;
	class Process
			int allocated[3];
			int max_needed[3];
				allocated[0] = allocated[1] = allocated[2] = max_needed[0] = max_needed[1] = max_needed[2] = 0;
				cout<<"Enter the allocated resources of types A,B and C"<<endl;
				cout<<"Enter the maximum number of resources needed for each of types A,B and C"<<endl;
			int Allocated(int temp)
				return allocated[temp];
			int Need(int temp)
				return max_needed[temp] - allocated[temp];
			int Max(int temp)
				return max_needed[temp];
	Process *process;
	int *sequence;
		A_avail = B_avail = C_avail = 0 ;
		A = B = C = 0 ;
		process = NULL;
		sequence = NULL;
		total_processes = remaining_processes = 0;
		int a,b,c;
		a = b = c = 0;
		cout<<"Available resources of type A,B and C"<<endl;
		cout<<"Enter the total number of processes"<<endl;
		remaining_processes = total_processes;
		sequence = new int[total_processes];
		cout<<"Please enter the required information of indivisual processes"<<endl;
		process = new Process[total_processes];
		cout<<"Enter the sequence(0,1,2 ... so on[in this format])"<<endl;
		for(int i = 0 ; i < total_processes ; ++i)
			a += process[i].Allocated(0);
			b += process[i].Allocated(1);
			c += process[i].Allocated(2);
		//	Calculating total number of resources of each type
		A = A_avail + a;
		B = B_avail + b;
		C = C_avail + c;
		bool safe = true;
		for(int i = 0 ; i < total_processes ; ++i)	//	Check fesibility of each process
			if( process[sequence[i]].Need(0) <= A_avail && process[sequence[i]].Need(1) <= B_avail && process[sequence[i]].Need(2) <= C_avail )
				//	i'th process can be executed safely
				A_avail += process[sequence[i]].Allocated(0);
				B_avail += process[sequence[i]].Allocated(1);
				C_avail += process[sequence[i]].Allocated(2);
				safe = false;
				cout<<"Unsafe condition encountered after "<<i<<" process"<<endl;
		if(safe == true)
			cout<<"Sequence is safe"<<endl;
			if(! (A_avail == A && B_avail == B && C_avail == C) )
				cout<<"Error encountered in the procedure.Not all resources deallocated."<<endl;
		delete []process;
		delete []sequence;
int main()
	Banker b;
	return 0;

Other 10 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.