Professional Documents
Culture Documents
Os Lab 10 M
Os Lab 10 M
Karachi Campus
01 Write a short note on Banker’s algorithm stating its main purpose and
working mechanism.
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:
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).
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 This simulation continues until a safe sequence is found (all processes can finish)
or no such sequence exists (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.
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];
finish[i] = false;
int work[MAX_RESOURCES];
work[i] = available[i];
int count = 0;
if (!finish[i]) {
can_allocate = false;
break;
if (can_allocate) {
work[j] += allocation[i][j];
finish[i] = true;
found = true;
count++;
if (!found) {
return false;
return true;
return false;
available[i] -= request[i];
allocation[pid][i] += request[i];
need[pid][i] -= request[i];
if (!safety(n, m)) {
available[i] += request[i];
allocation[pid][i] -= request[i];
need[pid][i] += request[i];
return false;
return true;
printf("%s:\n", name);
printf("\n");
printf("\n");
printf("%s:\n", name);
printf("\n\n");
int main() {
int n, m;
scanf("%d", &n);
scanf("%d", &m);
scanf("%d", &available[i]);
scanf("%d", &max[i][j]);
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)) {
} else {
int pid;
scanf("%d", &pid);
int request[MAX_RESOURCES];
scanf("%d", &request[i]);
} else {
return 0;
OUTPUT: