0% found this document useful (0 votes)
9 views4 pages

Program No 11 Linux 80

Uploaded by

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

Program No 11 Linux 80

Uploaded by

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

KAJAL SAH 4C3 11232680

PROGRAM NO:11
AIM : To study Bankers Algorithm.
anker’s Algorithm
The Banker’s Algorithm is a deadlock avoidance algorithm used in operating systems to allocate resources
safely to processes. It was developed by Edsger Dijkstra and is named because it simulates a banking system
where a banker ensures that customers (processes) never exceed available resources.

The system has fixed instances of resources (like CPU, memory, printers, etc.). Processes request resources
dynamically, and the system decides whether to grant them safely. A safe sequence of process execution
ensures that all processes complete without deadlock.

Key Concepts
The algorithm uses four primary matrices:

• Available: Stores the number of available instances of each resource type.


• Max: Defines the maximum demand of each process.
• Allocation: Shows resources currently allocated to processes.
• Need: Shows remaining resource needs of each process, calculated as:
Need[i][j]=Max[i][j]−Allocation[i][j]Need[i][j] = Max[i][j] - Allocation[i][j]

Algorithm
Safety Algorithm

1. Initialize a Finish[] array to mark completed processes.


2. Find a process P[i] such that Need[i] ≤ Available and Finish[i] == false.
3. If found, allocate resources to P[i], update Available, and mark Finish[i] = true.
4. Repeat until all processes are marked Finish[i] = true or no process can proceed.
5. If all processes can finish, the system is safe; otherwise, it's in an unsafe state.

Resource Request Algorithm

1. If a process P[i] requests resources Request[i], check:


o Request[i] ≤ Need[i] (Process doesn't exceed declared max need).
o Request[i] ≤ Available (Sufficient resources available).
2. If both conditions hold, temporarily allocate resources.
3. Check if the system remains in a safe state using the safety algorithm.
4. If yes, grant the request; otherwise, deny it.

Java Code for Banker’s Algorithm


import java.util.Scanner;
public class BankersAlgorithm {
private int numProcesses, numResources;
private int[] available;
private int[][] max, allocation, need;
public BankersAlgorithm(int numProcesses, int numResources) {
this.numProcesses = numProcesses;

MMEC,MULLANA Page | 50
KAJAL SAH 4C3 11232680
this.numResources = numResources;
available = new int[numResources];
max = new int[numProcesses][numResources];
allocation = new int[numProcesses][numResources];
need = new int[numProcesses][numResources];
}
public void inputMatrices(Scanner scanner) {
System.out.println("Enter Available Resources: ");
for (int i = 0; i < numResources; i++) {
available[i] = scanner.nextInt();
}
System.out.println("Enter Maximum Resource Matrix:");
for (int i = 0; i < numProcesses; i++) {
for (int j = 0; j < numResources; j++) {
max[i][j] = scanner.nextInt();
}
}
System.out.println("Enter Allocation Matrix:");
for (int i = 0; i < numProcesses; i++) {
for (int j = 0; j < numResources; j++) {
allocation[i][j] = scanner.nextInt();
need[i][j] = max[i][j] - allocation[i][j];
}
}
}
public boolean isSafeState() {
boolean[] finish = new boolean[numProcesses];
int[] work = available.clone();
int[] safeSequence = new int[numProcesses];
int count = 0;
while (count < numProcesses) {
boolean found = false;
for (int i = 0; i < numProcesses; i++) {
if (!finish[i]) {
boolean canAllocate = true;
for (int j = 0; j < numResources; j++) {
if (need[i][j] > work[j]) {
canAllocate = false;
break;
}
}
if (canAllocate) {
for (int j = 0; j < numResources; j++) {
work[j] += allocation[i][j];
}
safeSequence[count++] = i;
finish[i] = true;
found = true;
}
}
}
if (!found) {
System.out.println("System is in an unsafe state!");
MMEC,MULLANA Page | 51
KAJAL SAH 4C3 11232680
return false;
}
}
System.out.print("Safe Sequence: ");
for (int i : safeSequence) {
System.out.print("P" + i + " ");
}
System.out.println();
return true;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of processes and resources: ");
int numProcesses = scanner.nextInt();
int numResources = scanner.nextInt();
BankersAlgorithm ba = new BankersAlgorithm(numProcesses, numResources);
ba.inputMatrices(scanner);
ba.isSafeState();
scanner.close();
}
}

Example Execution
Input:
Enter number of processes and resources: 5 3

Enter Available Resources:

332

Enter Maximum Resource Matrix:

753

322

902

222

433

Enter Allocation Matrix:

010

2 00
3 02
211

002

Calculation:

MMEC,MULLANA Page | 52
KAJAL SAH 4C3 11232680
Need Matrix (Max - Allocation):

743

122

600

011

431

Available Resources:

332
Safe Sequence Output:
Safe Sequence: P1 P3 P4 P0 P2

MMEC,MULLANA Page | 53

You might also like