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

COS Exp7 Vijit

Uploaded by

Vijit Trivedi
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)
10 views5 pages

COS Exp7 Vijit

Uploaded by

Vijit Trivedi
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/ 5

K. J.

Somaiya College of Engineering, Mumbai-77


(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

Course Name: Computer Operating System (Elective) Semester: VII


Date of Performance: 8 / 10 / 2024 Batch No: A1
Faculty Name: Prof. Nilesh Lakde Roll No: 16010221018
Faculty Sign & Date: Grade/Marks: / 25

Expt No: 7
Title: Simulate Bankers Algorithm for Deadlock Avoidance.

Aim and Objective of the Experiment:


To implement Banker’s Algorithm for Deadlock Avoidance.

COs to be achieved:
CO 2: To illustrate and analyze the process, threads, process scheduling and thread
scheduling.

Theory:

The Banker's algorithm is a resource allocation and deadlock avoidance algorithm developed by
Edsger Dijkstra.

DATA STRUCTURES
(where n is the number of processes in the system and m is the number of resource types)
Implementation details:
#include <stdio.h>
#define P 3 // Number of processes
#define R 3 // Number of resource types
int main() {
int available[R] = {3, 3, 2}; // Initial available resources
int max[P][R] = {
{7, 5, 3},
{3, 2, 2},
{9, 0, 2}
};
int allocation[P][R] = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 2}
};

Computer Operating System Semester: VII Academic Year: 2024-25


Roll No:
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

int need[P][R]; // Need matrix


for (int i = 0; i < P; i++) {
for (int j = 0; j < R; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
int finish[P] = {0}; // Initially, no processes are finished
int safeSequence[P]; // To store the safe sequence
int work[R]; // Work array to store available resources during the check
// Copy available to work
for (int i = 0; i < R; i++) {
work[i] = available[i];
}
int count = 0; // Count of processes in the safe sequence
// Safety algorithm loop
while (count < P) {
int found = 0; // Flag to check if a process was found in this iteration
for (int p = 0; p < P; p++) {
if (finish[p] == 0) { // If the process is not finished
int canProceed = 1; // Assume the process can proceed
// Check if all resources needed by the process are available
for (int j = 0; j < R; j++) {
if (need[p][j] > work[j]) {
canProceed = 0; // The process can't proceed
printf("Process %d cannot proceed because it needs more
resources than available.\n", p);
printf("Needs: {");

for (int k = 0; k < R; k++) {


printf("%d ", need[p][k]);
}
printf("}, Available: {");
for (int k = 0; k < R; k++) {
printf("%d ", work[k]);
}
printf("}\n");
break; // No need to check further resources
}
}
// If the process can proceed
if (canProceed) {
printf("Process %d can proceed and will be allocated resources.\n",
p);
// Simulate the process completion by adding its allocated

Computer Operating System Semester: VII Academic Year: 2024-25


Roll No:
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

resources to work
for (int j = 0; j < R; j++) {
work[j] += allocation[p][j];
}
// Add the process to the safe sequence and mark it as finished
safeSequence[count++] = p;
finish[p] = 1;
found = 1;
printf("After Process %d completes, available resources are: {", p);
for (int k = 0; k < R; k++) {
printf("%d ", work[k]);
}
printf("}\n");
}
}
}

// If no process could proceed in this round, it means the system is unsafe


if (found == 0) {
printf("System is not in a safe state!\n");
return 0;
}
}
// If we get here, the system is in a safe state
printf("System is in a safe state.\nSafe sequence is: ");
for (int i = 0; i < P; i++) {
printf("%d ", safeSequence[i]);
}
printf("\n");
return 0;
}

Output:

Computer Operating System Semester: VII Academic Year: 2024-25


Roll No:
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

Post Lab Subjective/Objective type Questions:


1) Q1 The wait-for graph is a deadlock detection algorithm that is applicable when:
a) All resources have a single instance
b) All resources have multiple instances
c) Both a and b
d) None of the above

2) Resources are allocated to the process on non-sharable basis is _


a) Hold and Wait
b) Mutual Exclusion
c) No pre-emption
d) Circular Wait

3) Which of the following approaches require knowledge of the system state?


a) Deadlock Detection
b) Deadlock Prevention
c) Deadlock Avoidance
d) All of the above

4) Consider a system having ‘m’ resources of the same type. These resources are
shared by 3 processes A, B, C which have peak time demands of 3, 4, 6 respectively. The
minimum value of ‘m’ that ensures that deadlock will never occur is

Computer Operating System Semester: VII Academic Year: 2024-25


Roll No:
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

a) 11
b) 12
c) 13
d) 14

Conclusion:
In this experiment, we explored key concepts related to deadlock in operating systems, including
detection, prevention, and avoidance techniques. We implemented the Banker's Algorithm, which
ensures safe resource allocation by checking system states to avoid deadlock.

Signature of faculty in-charge with Date:

Computer Operating System Semester: VII Academic Year: 2024-25


Roll No:

You might also like