0% found this document useful (0 votes)
8 views1 page

Os Ex 7

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

Os Ex 7

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

7. WRITE C PROGRAM TO AVOID DEADLOCK USING BANKER’S ALGORITHM.

ALGORITHM:

1. Initialize:
- available: array of length R representing available instances of each resource
- max: 2D array of size P x R representing maximum demand of each process
- alloc: 2D array of size P x R representing resources currently allocated to each process
- need: 2D array of size P x R representing remaining need of each process
- finish: array of size P initialized to all zeros

2. Calculate the 'need' matrix by subtracting 'alloc' from 'max' for each process.

3. Initialize a work array as a copy of 'available'.

4. Initialize a 'finish' array to keep track of finished processes.

5. Loop until all processes are finished or no process can be allocated resources:
a. Find a process i such that 'finish[i] == 0' and 'need[i]' can be satisfied with the available resources.
b. If such a process is found:
i. Mark process i as finished.
ii. Add allocated resources of process i back to 'available'.
iii. Repeat the process until no more processes can be allocated resources.
c. If no such process is found, break the loop.

6. If all processes are finished, the system is in a safe state; otherwise, it's in an unsafe state.

You might also like