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

Nishtha OS7

The document outlines an assignment to implement the Banker’s algorithm for deadlock avoidance in C/C++/Java. It includes a code example that simulates two scenarios: one where a safe sequence exists and another where it does not. The program uses predefined values for resource allocation and availability to demonstrate the algorithm's functionality.

Uploaded by

nmehta1be23
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)
10 views4 pages

Nishtha OS7

The document outlines an assignment to implement the Banker’s algorithm for deadlock avoidance in C/C++/Java. It includes a code example that simulates two scenarios: one where a safe sequence exists and another where it does not. The program uses predefined values for resource allocation and availability to demonstrate the algorithm's functionality.

Uploaded by

nmehta1be23
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/ 4

ASSIGNMENT 7

Q1. Write a program in C/C++/Java to simulate the Banker’s algorithm for deadlock avoidance.
Consider at least 3 processes in the system, with 4 resource classes having at least one resource
instance for each class. Assume the values for Available, Allocation, MAX, and request from a
particular process from your side. The program must reflect two cases, where a safe sequence
exists for one and a safe sequence does not exist for another.

Code :

#include <iostream>

#include <vector>

using namespace std;

// Function to check if a process's request can be granted

bool canAllocate(vector<int> available, vector<int> need) {

for (size_t i = 0; i < need.size(); i++) {

if (need[i] > available[i]) {

return false;

return true;

// Function to check for a safe sequence

bool isSafe(vector<vector<int>> allocation, vector<vector<int>> max, vector<int> available) {

int n = allocation.size(); // Number of processes

int m = available.size(); // Number of resource types


vector<int> work = available;

vector<bool> finish(n, false);

vector<int> safeSequence;

while (safeSequence.size() < n) {

bool found = false;

for (int i = 0; i < n; i++) {

if (!finish[i] && canAllocate(work, max[i])) {

// Grant resources

for (int j = 0; j < m; j++) {

work[j] += allocation[i][j];

finish[i] = true;

safeSequence.push_back(i);

found = true;

break;

if (!found) {

return false; // No safe sequence found

cout << "Safe Sequence: ";

for (int process : safeSequence) {


cout << "P" << process << " ";

cout << endl;

return true;

int main() {

// Number of processes and resource types

int processes = 3;

int resources = 4;

// Initializing data

vector<vector<int>> allocation = {{0, 1, 0, 3},

{2, 0, 0, 1},

{3, 0, 2, 1}};

vector<vector<int>> max = {{7, 5, 3, 6},

{3, 2, 2, 2},

{9, 0, 2, 4}};

vector<vector<int>> need = max; // Calculate need from max and allocation

vector<int> available = {3, 3, 2, 2};

for (size_t i = 0; i < need.size(); i++) {

for (size_t j = 0; j < need[0].size(); j++) {

need[i][j] -= allocation[i][j];

}
cout << "Case 1 (Safe sequence exists):" << endl;

isSafe(allocation, need, available);

// Modify available to reflect unsafe case

available = {1, 0, 0, 0};

cout << "\nCase 2 (No safe sequence exists):" << endl;

if (!isSafe(allocation, need, available)) {

cout << "No safe sequence is possible with these resource allocations." << endl;

return 0;

Output :

You might also like