THINK FIRST·CODE LATER

← All labs

The Banker's algorithm: safety and requests

Problem

Implement the Banker's algorithm.

Input: n m; the Available vector; then n rows of Max; then n rows of Allocation; then zero or more lines request i r1 … rm.

  1. Print Need: and one line per process: P0: (7 4 3).
  2. Run the safety algorithm with Work = Available, scanning P0…Pn−1 in passes (in each pass take, in index order, every unfinished process whose Need ≤ Work). For each process taken print P1 can finish, work = (5 3 2) (two leading spaces, Work after adding its allocation). Then State: SAFE, sequence P1 P3 P4 P0 P2 or State: UNSAFE.
  3. For each request (processed in order; a granted request changes the state), print Request P1 (1 0 2): followed by:
    • ERROR, exceeds maximum claim if Req > Need (any component);
    • WAIT, resources not available if Req > Available;
    • GRANTED, safe sequence …, available now (2 3 0) if the pretend-state is safe (no trace lines);
    • DENIED, would be unsafe (P0 waits) otherwise (the state is rolled back).

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.