95 lines
		
	
	
	
		
			3.7 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			95 lines
		
	
	
	
		
			3.7 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
#include <stdio.h>
 | 
						|
 | 
						|
// C program for Banker's Algorithm (Safety & Resource Request)
 | 
						|
// Optimized for minimal code size (e.g., for writing on paper)
 | 
						|
 | 
						|
int main() {
 | 
						|
    int p, r, i, j, k, pid, req_pid = -1; // p=procs, r=res; req_pid: -1=initial, >=0 processing req
 | 
						|
    printf("P R:"); scanf("%d%d", &p, &r); // Input num processes and resources
 | 
						|
    int av[r], max[p][r], al[p][r], nd[p][r], req[r]; // av=avail, al=alloc, nd=need
 | 
						|
    int w[r], fin[p], seq[p]; // w=work, fin=finish, seq=safe sequence
 | 
						|
 | 
						|
    // Input available, max, allocation matrices
 | 
						|
    printf("Av:"); for(j=0; j<r; j++) scanf("%d", &av[j]);
 | 
						|
    printf("Max:\n"); for(i=0; i<p; i++) for(j=0; j<r; j++) scanf("%d", &max[i][j]);
 | 
						|
    printf("Alloc:\n"); for(i=0; i<p; i++) for(j=0; j<r; j++) scanf("%d", &al[i][j]);
 | 
						|
 | 
						|
    // Calculate need matrix: need = max - alloc
 | 
						|
    for(i=0; i<p; i++) for(j=0; j<r; j++) nd[i][j] = max[i][j] - al[i][j];
 | 
						|
 | 
						|
S:; // Safety Check Algorithm Label
 | 
						|
    int s_idx = 0, c = 0, safe = 0; // s_idx=seq index, c=count finished, safe=flag
 | 
						|
    for(j=0; j<r; j++) w[j] = av[j]; // work = avail
 | 
						|
    for(i=0; i<p; i++) fin[i] = 0; // finish[p] = {false}
 | 
						|
    // Find sequence using safety algorithm logic
 | 
						|
    while(c < p) { // Loop until all processes are finished or deadlock
 | 
						|
        int found = 0; // Flag to check if a process was found in this pass
 | 
						|
        for(i=0; i<p; i++) { // Iterate through processes
 | 
						|
            if(!fin[i]) { // If process i not finished
 | 
						|
                int possible = 1; // Check if need <= work
 | 
						|
                for(j=0; j<r; j++) if(nd[i][j] > w[j]) { possible = 0; break; }
 | 
						|
                if(possible) { // If need <= work
 | 
						|
                    for(k=0; k<r; k++) w[k] += al[i][k]; // work = work + alloc
 | 
						|
                    fin[i] = 1; seq[s_idx++] = i; c++; found = 1; // Mark finished, add to seq
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
        if(!found) break; // If no process found in a full pass, break (unsafe state)
 | 
						|
    }
 | 
						|
    if(c == p) safe = 1; // If all processes finished, state is safe
 | 
						|
 | 
						|
    // --- End Safety Check ---
 | 
						|
 | 
						|
    // Handle result based on phase (initial check or request check)
 | 
						|
    if(req_pid == -1) { // Phase 1: Initial State Check
 | 
						|
        if(safe) {
 | 
						|
            printf("SAFE. Seq:"); for(i=0; i<p; i++) printf(" P%d", seq[i]); puts("");
 | 
						|
        } else { puts("UNSAFE"); goto end; } // If unsafe initially, exit
 | 
						|
 | 
						|
        // Phase 2: Resource Request
 | 
						|
        printf("PID Req:"); scanf("%d", &pid); req_pid = pid; // Get requesting proc ID
 | 
						|
        printf("Req:"); for(j=0; j<r; j++) scanf("%d", &req[j]); // Get request vector
 | 
						|
 | 
						|
        // Check 1: Request <= Need
 | 
						|
        for(j=0; j<r; j++) if(req[j] > nd[pid][j]) { puts("Err:Req>Need"); goto end; }
 | 
						|
        // Check 2: Request <= Available
 | 
						|
        for(j=0; j<r; j++) if(req[j] > av[j]) { puts("Wait:Req>Avail"); goto end; }
 | 
						|
 | 
						|
        // Tentatively allocate resources
 | 
						|
        for(j=0; j<r; j++) { av[j]-=req[j]; al[pid][j]+=req[j]; nd[pid][j]-=req[j]; }
 | 
						|
        puts("Checking req safety...");
 | 
						|
        goto S; // Re-run safety check on the new state
 | 
						|
 | 
						|
    } else { // Phase 3: Post-Request Safety Check Result
 | 
						|
        if(safe) { // Request is granted if new state is safe
 | 
						|
             printf("Req OK. Seq:"); for(i=0; i<p; i++) printf(" P%d", seq[i]); puts("");
 | 
						|
        } else { // Request denied if new state is unsafe
 | 
						|
            puts("Req DENIED (unsafe)");
 | 
						|
            // Rollback state to before tentative allocation
 | 
						|
            pid = req_pid; // Restore pid for rollback
 | 
						|
            for(j=0; j<r; j++) { av[j]+=req[j]; al[pid][j]-=req[j]; nd[pid][j]+=req[j]; }
 | 
						|
        }
 | 
						|
         // No further action needed after handling the single request
 | 
						|
    }
 | 
						|
 | 
						|
end: return 0; // End of program
 | 
						|
}
 | 
						|
 | 
						|
/*
 | 
						|
```
 | 
						|
P R: 5 3
 | 
						|
Av: 3 3 2
 | 
						|
Max:
 | 
						|
7 5 3
 | 
						|
3 2 2
 | 
						|
9 0 2
 | 
						|
2 2 2
 | 
						|
4 3 3
 | 
						|
Alloc:
 | 
						|
0 1 0
 | 
						|
2 0 0
 | 
						|
3 0 2
 | 
						|
2 1 1
 | 
						|
0 0 2
 | 
						|
```
 | 
						|
*/
 |