Skip to content

Instantly share code, notes, and snippets.

@StefanoFiumara
Created October 9, 2013 04:02
Show Gist options
  • Save StefanoFiumara/6896016 to your computer and use it in GitHub Desktop.
Save StefanoFiumara/6896016 to your computer and use it in GitHub Desktop.
Check safe procedure for banker's algorithm
bool process_manager::check_safe(int* _avail, int** _alloc, int** _max) {
int i, j; //loop counters
int* work = new int[num_resources];
bool* finish = new bool[num_processes];
//set finish[i] to false
for(i = 0; i < num_processes; i++) {
finish[i] = false;
}
//set work = available
for(i = 0; i < num_resources; i++) {
work[i] = _avail[i];
}
//for each process, check if they are able to request the maximum amount of needed resources
for(i = 0; i < num_processes; i++) {
//Don't need to bother check if the program isn't running
if(running[i] == false) {
finish[i] = true;
continue;
}
for(j = 0; j < num_resources; j++) {
if(finish[i] == false && _max[i][j] <= work[j]) {
work[j] += _alloc[i][j];
finish[i] = true;
}
}
}
//if finish[i] == true for all i, system is safe, return true.
for(i = 0; i < num_processes; i++) {
if(finish[i] == false) {
cout << "System is not safe, this process must wait! " << endl;
delete work;
delete finish;
return false;
}
}
delete work;
delete finish;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment