Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

#Bahria University

Karachi Campus

LAB EXPERIMENT NO.


10
LIST OF TASKS
TASK NO OBJECTIVE

01 Write a short note on Banker’s algorithm stating its main purpose and
working mechanism.

02 Implement the Banker’s Algorithm explained above in C language.

Submitted On:
Date: __ __

1
[LAB 10] [INTER PROCESS COMMUNICATION II]
[OPERATING SYSTEM]

TASK NO. 01: Write a short note on Banker’s algorithm stating its main purpose and working
mechanism.

SOLUTION:

Banker's Algorithm: Safe Resource Allocation and Deadlock Avoidance

The Banker's Algorithm is a resource allocation and deadlock avoidance algorithm used in
operating systems. It prevents situations where processes become deadlocked, waiting for
resources held by each other. Here's a breakdown of its functionality, mechanism, structures,
and usage:

Mechanism:

1. Data Gathering: The Banker's Algorithm gathers information about resources and
processes:

o Max: A matrix (Max) stores the maximum resource requirement for each process
(how much each process might need at most).

o Allocation: Another matrix (Allocation) tracks the resources currently allocated


to each process.

o Need: The Need matrix is calculated as Max - Allocation, representing the


remaining resources each process might still need to finish.

o Available: A vector (Available) represents the resources currently available in the


system.

2. Safe State Check: The algorithm checks if the system is in a safe state. A safe state exists
if a safe sequence of processes can be found, where each process in the sequence can
finish its execution using only the available resources and resources held by processes
that appear earlier in the sequence.

o The algorithm simulates process execution by assuming a process can acquire its
remaining needs from available resources.

o If a process can finish execution, it is removed from consideration, and its


released resources are added back to Available.

o This simulation continues until a safe sequence is found (all processes can finish)
or no such sequence exists (potential deadlock).

MINHAJ UR RIYAN 02-131222-080


[LAB 10] [INTER PROCESS COMMUNICATION II]
[OPERATING SYSTEM]

3. Resource Request Handling: When a process requests additional resources, the


Banker's Algorithm checks if granting the request would leave the system in a safe state.
If not, the request is rejected to prevent potential deadlock.

Data Structures:

 Max Matrix: A 2D matrix where rows represent processes and columns represent
resource types. Each entry (Max[i][j]) specifies the maximum amount of resource j that
process i can ever request.

 Allocation Matrix: Another 2D matrix where rows represent processes and columns
represent resource types. Each entry (Allocation[i][j]) specifies the amount of resource j
currently allocated to process i.

 Need Matrix: Derived from Max and Allocation (Need = Max - Allocation). It represents
the remaining resources each process might still need to finish.

 Available Vector: A 1D vector where each element represents the amount of a specific
resource currently available in the system.

Usage:

The Banker's Algorithm is primarily used in research and theoretical contexts due to its
overhead and limitations. However, it provides a valuable foundation for understanding
resource allocation and deadlock prevention in operating systems. Some systems might
implement variations or inspiration from the Banker's Algorithm for deadlock avoidance or
resource management.

MINHAJ UR RIYAN 02-131222-080


[LAB 10] [INTER PROCESS COMMUNICATION II]
[OPERATING SYSTEM]

TASK NO. 02: Implement the Banker’s Algorithm explained above in C language.

SOLUTION:

CODE:

#include <stdio.h>

#include <stdbool.h>

#define MAX_PROCESSES 10

#define MAX_RESOURCES 10

int available[MAX_RESOURCES];

int max[MAX_PROCESSES][MAX_RESOURCES];

int allocation[MAX_PROCESSES][MAX_RESOURCES];

int need[MAX_PROCESSES][MAX_RESOURCES];

bool finish[MAX_PROCESSES];

void initialize(int n, int m) {

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

finish[i] = false;

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

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

MINHAJ UR RIYAN 02-131222-080


[LAB 10] [INTER PROCESS COMMUNICATION II]
[OPERATING SYSTEM]

bool safety(int n, int m) {

int work[MAX_RESOURCES];

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

work[i] = available[i];

int count = 0;

while (count < n) {

bool found = false;

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

if (!finish[i]) {

bool can_allocate = true;

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

if (need[i][j] > work[j]) {

can_allocate = false;

break;

if (can_allocate) {

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

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

finish[i] = true;

found = true;

MINHAJ UR RIYAN 02-131222-080


[LAB 10] [INTER PROCESS COMMUNICATION II]
[OPERATING SYSTEM]

count++;

if (!found) {

return false;

return true;

bool request_resources(int pid, int request[], int n, int m) {

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

if (request[i] > need[pid][i] || request[i] > available[i]) {

return false;

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

available[i] -= request[i];

allocation[pid][i] += request[i];

need[pid][i] -= request[i];

if (!safety(n, m)) {

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

MINHAJ UR RIYAN 02-131222-080


[LAB 10] [INTER PROCESS COMMUNICATION II]
[OPERATING SYSTEM]

available[i] += request[i];

allocation[pid][i] -= request[i];

need[pid][i] += request[i];

return false;

return true;

void print_matrix(int matrix[MAX_PROCESSES][MAX_RESOURCES], int n, int m, char* name) {

printf("%s:\n", name);

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

printf("For process %d: ", i);

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

printf("%d ", matrix[i][j]);

printf("\n");

printf("\n");

void print_available(int available[], int m, char* name) {

printf("%s:\n", name);

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

printf("%d ", available[i]);

MINHAJ UR RIYAN 02-131222-080


[LAB 10] [INTER PROCESS COMMUNICATION II]
[OPERATING SYSTEM]

printf("\n\n");

int main() {

int n, m;

printf("Enter the number of processes: ");

scanf("%d", &n);

printf("Enter the number of resources: ");

scanf("%d", &m);

printf("Enter the available resources:\n");

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

scanf("%d", &available[i]);

printf("Enter the maximum demand of each process:\n");

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

printf("For process %d: ", i);

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

scanf("%d", &max[i][j]);

printf("Enter the allocation of each process:\n");

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

printf("For process %d: ", i);

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

MINHAJ UR RIYAN 02-131222-080


[LAB 10] [INTER PROCESS COMMUNICATION II]
[OPERATING SYSTEM]

scanf("%d", &allocation[i][j]);

initialize(n, m);

print_available(available, m, "Available");

print_matrix(max, n, m, "Max");

print_matrix(allocation, n, m, "Allocation");

print_matrix(need, n, m, "Need");

if (safety(n, m)) {

printf("System is in a safe state.\n");

} else {

printf("System is not in a safe state.\n");

int pid;

printf("Enter the process ID for resource request: ");

scanf("%d", &pid);

int request[MAX_RESOURCES];

printf("Enter the request for process %d: ", pid);

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

scanf("%d", &request[i]);

MINHAJ UR RIYAN 02-131222-080


[LAB 10] [INTER PROCESS COMMUNICATION II]
[OPERATING SYSTEM]

if (request_resources(pid, request, n, m)) {

printf("Resource request granted.\n");

} else {

printf("Resource request denied.\n");

return 0;

OUTPUT:

MINHAJ UR RIYAN 02-131222-080

You might also like