Safety Algorithm
1. Let Work and Finish be vectors of length
m and n, respectively. Initialize:
Work = Available
Finish [i] = false for i - 1,3, …, n.
2. Input from the user
(a) Allocation Matrix
(b) Allocation Matrix
(c) Available Resources
3. Calculate Need Matrix:
for each index (i,j):
Need[i][j] = Maximum[i][j] - Allocation[i][j]
4. If Finish [i] == true
for all i, then the system is in a safe state.
Resource-Request Algorithm for Process Pi
Request = request vector for process Pi.
If Request i [j] = k then
process Pi wants k instances of resource type Rj.
5. If Request [i]=Need [i] go to step 4.
Otherwise, raise error condition,
since process has exceeded its maximum claim.
6. If Request [i]=Available, go to step 4.
Otherwise Pi must wait,
since resources are not available.
Pretend to allocate requested resources to
Pi by modifying the state as follows:
Available = Request [i];
Allocation [i] = Allocation [i] + Request [i];
Need [i] = Need [i] – Request [i];
If safe the resources are allocated to Pi.
If unsafe Pi must wait,
and the old resource-allocation state is restored.
Enter the Number of Process n:
Enter the Number of Resources m: