Not very familiar with the algorithm but I think you deallocate the resources for every process when you should only deallocate one process.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
if(possible == m)
{
for(int k = 0; k < n; k++) // you deallocate all processes here
{
for(int j = 0; j < m; j++)
{
//do math calculations
available[j] = holder[k][j] + available[j];
}
}
kickout++;
possible = 0;
safe_state[i] = true;
cout << "\nupdated available vector" << endl;
cout << available[0] << " " << available[1] << " " << available[2] << " " << available[3] << endl;
}
Also I think possible should be reset to zero for every iteration of the for loop. You only reset it when it equals m so for the next iteration sometimes it won't be zero.
1 2 3 4 5
if (!filename) // I think this should be !infile
{
cout << "ERROR READING FILE! " << endl;
return 0;
}
You should close infile somewhere too once you don't need it.
Say m is 4 and process one has 2 needs <= available and process two has 2 needs <= available.
First time thru theif(available[j] >= need[i][j]) loop
possible will be 2.
Possible != m so possible is not reset to zero.
Second time thru the loop m is still 2 and after the loop possible will be 4 (2+2). Now possible == m but process 2 cannot finish (2 out of 4 needs > available) but your code assumes it does and then you make available all the resources of every process.