Important alert: (current site time 12/20/2014 3:16:32 AM EDT) They're finally here! Check out our new 7 Source Code CDs!

# Banker's algorithm

Email
 Submitted on: 8/5/2011 8:20:37 AM By: Puneet Jindal Level: Advanced User Rating: Unrated Compatibility: UNIX C++ Views: 8996

Illustrate banker's algorithm

code:
Can't Copy and Paste this?
 Terms of Agreement:    By using this code, you agree to the following terms...    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. 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.    You may link to this code from another website, but ONLY if it is not wrapped in a frame.  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 http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=13616&lngWId=3//for details.//************************************** #include 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 { private: int allocated[3]; int max_needed[3]; public: Process() { 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"<>allocated[0]>>allocated[1]>>allocated[2]; cout<<"Enter the maximum number of resources needed for each of types A,B and C"<>max_needed[0]>>max_needed[1]>>max_needed[2]; } 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; public: Banker() { 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"<>A_avail>>B_avail>>C_avail; cout<<"Enter the total number of processes"<>total_processes; remaining_processes = total_processes; sequence = new int[total_processes]; cout<<"Please enter the required information of indivisual processes"<>sequence[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); } else { safe = false; cout<<"Unsafe condition encountered after "<

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:

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 ...)