- Make a loop while the length of
safe_seq_list
is not equal to theno_of_job
, - Inside the loop, make a for loop, so as to access keys (
job_name
) and values (need_list
) over each iteration, - Make a
flag
(initialize it asTrue
which means it resource needed is less than available CPU resources) for checking whether the resources is lesser or greater than available CPU resources. - Make another
for
loop, just to make sure the process (job_name
) is not already in the safe_seq_list, - Check for the resources needed and available CPU available conditions if satisfy, pass it else make the
flag = False
- Outside
for
loop, check forflag
status, if it isTrue
, append the process (job_name
) to thesafe_seq_list
. Update (new_cpu_available
) the new CPU resources by adding the current process resources and available CPU resources. Also, delete the process (job_name
) from theres_need_dict_copy
dictionary, as the process is safe and has been added to thesafe_seq_list
. - Finally, outside
while
loop, print the safe sequence. - Run the above, recursively, till the
safe_seq_list
is equal to theno_of_jobs
.
Run the program using: python3 bankers.py
CPU Resources:
Available: 2 1 0
Number of jobs: 5
Enter currentlly allocated resources for P0: 1 1 2
Enter currentlly allocated resources for P1: 2 1 2
Enter currentlly allocated resources for P2: 4 0 1
Enter currentlly allocated resources for P3: 0 2 0
Enter currentlly allocated resources for P4: 1 1 2
Current resources dict: {'P0': [1, 1, 2], 'P1': [2, 1, 2], 'P2': [4, 0, 1], 'P3': [0, 2, 0], 'P4': [1, 1, 2]}
Enter max resource for P0: 4 3 3
Enter max resource for P1: 3 2 2
Enter max resource for P2: 9 0 2
Enter max resource for P3: 7 5 3
Enter max resource for P4: 1 1 2
Resources needed dict: {'P0': [3, 2, 1], 'P1': [1, 1, 0], 'P2': [5, 0, 1], 'P3': [7, 3, 3], 'P4': [0, 0, 0]}
No safe sequence found!
New safe seq. added: ['P1']
No safe sequence found!
No safe sequence found!
New safe seq. added: ['P1', 'P4']
New safe seq. added: ['P1', 'P4', 'P0']
New safe seq. added: ['P1', 'P4', 'P0', 'P2']
New safe seq. added: ['P1', 'P4', 'P0', 'P2', 'P3']
Safe sequence: ['P1', 'P4', 'P0', 'P2', 'P3']