[go: up one dir, main page]

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

Exp 10

The document outlines two experiments related to deadlock management in C programming: the implementation of the Banker's algorithm for deadlock avoidance and a method for deadlock detection. The Banker's algorithm checks resource allocation to ensure the system remains in a safe state, while the deadlock detection algorithm periodically checks if any processes can complete execution. Both implementations involve user input for processes, resources, and their respective allocations and requests.

Uploaded by

iyxlo126mr
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)
13 views5 pages

Exp 10

The document outlines two experiments related to deadlock management in C programming: the implementation of the Banker's algorithm for deadlock avoidance and a method for deadlock detection. The Banker's algorithm checks resource allocation to ensure the system remains in a safe state, while the deadlock detection algorithm periodically checks if any processes can complete execution. Both implementations involve user input for processes, resources, and their respective allocations and requests.

Uploaded by

iyxlo126mr
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/ 5

Experiment-10.

1
AIM:

Write a program to implement banker's algorithm for deadlock avoidance in C.

Theory:

The Banker's algorithm operates by considering the current allocation, maximum demand, and available
resources. It maintains several data structures, including the finish array, the work array, and the need
matrix.

1. finish array: Indicates whether a process has completed execution.

2. work array: Represents the available resources.

3. need matrix: Represents the maximum resources a process can request.

The algorithm proceeds by simulating the resource allocation and checking if the system remains in a safe
state after each allocation. If at any point the system is not in a safe state, the algorithm stops and reports that
the system is not in a safe state.

The implementation takes user input for the number of processes, resources, maximum resources required
by each process, resources currently allocated to each process, and available resources. It then calls the
isSafe function to check if the system is in a safe state or not. If it is, the system is considered to be free from
deadlock.

Code:-

#include <stdio.h>

#define MAX_PROCESSES 10

#define MAX_RESOURCES 10

int isSafe(int processes, int resources, int max[processes][resources], int alloc[processes][resources],


int avail[resources]) {
int finish[processes]; int work[resources];
int need[processes][resources];

// Initialize finish and work arrays for (int i = 0; i < processes; i++) {
finish[i] = 0;

for (int i = 0; i < resources; i++) { work[i] = avail[i];


}

for (int i = 0; i < processes; i++) { for (int j = 0; j < resources; j++) {
need[i][j] = max[i][j] - alloc[i][j];

}
}

int count = 0;

while (count < processes) { int found = 0;


for (int i = 0; i < processes; i++) { if (finish[i] == 0) {
int j;

for (j = 0; j < resources; j++) { if (need[i][j] > work[j])


break;

if (j == resources) {

for (int k = 0; k < resources; k++) { work[k] += alloc[i][k];


}

finish[i] = 1;

found = 1;
count++;

if (found == 0) {

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


}

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


}

int main() {

int processes, resources;

printf("Enter the number of processes: "); scanf("%d", &processes);


printf("Enter the number of resources: "); scanf("%d", &resources);
int max[MAX_PROCESSES][MAX_RESOURCES]; int alloc[MAX_PROCESSES][MAX_RESOURCES];
int avail[MAX_RESOURCES];

printf("Enter the maximum resources required by each process:\n"); for (int i = 0; i < processes; i++) {
for (int j = 0; j < resources; j++) { scanf("%d", &max[i][j]);
}
}

printf("Enter the resources currently allocated to each process:\n"); for (int i = 0; i < processes; i+
+) {
for (int j = 0; j < resources; j++) { scanf("%d", &alloc[i][j]);
}

printf("Enter the available resources:\n"); for (int i = 0; i < resources; i++) {


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

isSafe(processes, resources, max, alloc, avail); return 0;


}
Output

Result :-

The following program has been successfully implemented.


Experiment 10.2
Aim:-

Write a program to implement Deadlock Detection in C.

Theory:-

Deadlock detection involves periodically checking the system to determine whether a deadlock has occurred.
The Banker's algorithm can be adapted for deadlock detection by checking if there is any process that can
finish execution. If no such process is found, a deadlock is detected.

Code:-

#include <stdio.h>

#define MAX_PROCESSES 10

#define MAX_RESOURCES 10

int isDeadlock(int processes, int resources, int allocation[MAX_PROCESSES][MAX_RESOURCES], int


request[MAX_PROCESSES][MAX_RESOURCES], int available[MAX_RESOURCES]) {
int finish[MAX_PROCESSES] = {0}; int work[MAX_RESOURCES];
for (int i = 0; i < resources; i++) { work[i] = available[i];
}

int count = 0;

while (count < processes) { int found = 0;


for (int i = 0; i < processes; i++) { if (finish[i] == 0) {
int j;

for (j = 0; j < resources; j++) {


if (request[i][j] > work[j]) break;
}

if (j == resources) {

for (int k = 0; k < resources; k++) { work[k] += allocation[i][k];


}

finish[i] = 1;

found = 1; count++;
}

if (found == 0) { printf("Deadlock detected!\n"); return 1;


}

printf("No deadlock detected.\n"); return 0;


}

int main() {
int processes, resources;

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


scanf("%d", &processes);

printf("Enter the number of resources: "); scanf("%d", &resources);


int allocation[MAX_PROCESSES][MAX_RESOURCES]; int request[MAX_PROCESSES][MAX_RESOURCES];
int available[MAX_RESOURCES];

printf("Enter the resources currently allocated to each process:\n"); for (int i = 0; i < processes; i+
+) {
for (int j = 0; j < resources; j++) { scanf("%d", &allocation[i][j]);
}

printf("Enter the resources requested by each process:\n"); for (int i = 0; i < processes; i++) {
for (int j = 0; j < resources; j++) { scanf("%d", &request[i][j]);
}

printf("Enter the available resources:\n"); for (int i = 0; i < resources; i++) {


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

isDeadlock(processes, resources, allocation, request, available); return 0;


}

Output :-

Result :-

The following code has been successfully implemented.

You might also like