Free Essay

Submitted By ranjith23

Words 406

Pages 2

Words 406

Pages 2

Homework-5

Student Name | Student ID | Section | Date | | | | |

Bankers Algorithm

The Banker's algorithm is a resource allocation and deadlock avoidance algorithm. Using C++ programming language, implement the Banker's algorithm for deadlock avoidance.

5 <= # of process, P0 ~ P4

4 <= # of resource, A ~ D

0 0 1 2 <= Allocation

1 0 0 0

1 3 5 4

0 6 3 2

0 0 1 4

0 0 1 2 <= Max

1 7 5 0

2 3 5 6

0 6 5 2

0 6 5 6

1 5 2 0 <= Available

5 <= # of process, P0 ~ P4

4 <= # of resource, A ~ D

0 0 1 2 <= Allocation

1 0 0 0

1 3 5 4

0 6 3 2

0 0 1 4

0 0 1 2 <= Max

1 7 5 0

2 3 5 6

0 6 5 2

0 6 5 6

1 5 2 0 <= Available

Input a txt file that includes the number of processes, resources, and the matrixes for allocations, max, and available.

Output Safe or Unsafe 1) Read the # of processes and the # of resources 2) Read allocation, max and available for each process and each resource 3) Print whether this system is safe or not to the output file.

Data Structures for the Banker’s Algorithm

Input.txt

Input.txt

Let n = number of processes, and m = number of resources types.

Available: Vector of length m. If available [j] = k, there are k instances of resource type Rj available

Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances of resource type Rj

Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj

Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task

Need [i,j] = Max[i,j] – Allocation [i,j]

Safety Algorithm

1. Let Work and Finish be vectors of length m and n, respectively.

Initialize:

Work = Available

Finish [i] = false for i = 0, 1, …, n- 1

2. Find an i such that both:

(a) Finish [i] = false

(b) Needi ≤ Work

If no such i exists, go to step 4

3. Work = Work + Allocationi

Finish[i] = true go to step 2

4. If Finish [i] == true for all i, then the system is in a safe state