[go: up one dir, main page]

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

Exp 8

The document explains deadlock detection in computer systems, defining deadlock and its conditions such as mutual exclusion, hold & wait, and preemption. It includes a C program that simulates deadlock detection by taking user inputs for processes and resources, and checks for deadlock conditions based on allocation and request matrices. The program outputs whether a deadlock is detected or not based on the inputs provided.

Uploaded by

aishwaryakeshi
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)
6 views4 pages

Exp 8

The document explains deadlock detection in computer systems, defining deadlock and its conditions such as mutual exclusion, hold & wait, and preemption. It includes a C program that simulates deadlock detection by taking user inputs for processes and resources, and checks for deadlock conditions based on allocation and request matrices. The program outputs whether a deadlock is detected or not based on the inputs provided.

Uploaded by

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

Experiment 08:

Write a program to simulate deadlock detection

What is a Deadlock?
A deadlock occurs when a group is stuck in a process because everyone is
hanging onto resources while waiting for other ways to get them.
Conditions for Deadlock:
There are several conditions for deadlock. Some main deadlock conditions are
as follows:
Mutual exclusion: A resource can only be held by one process at a time. In
other words, if Process P1 uses a resource R at a certain time, Process
P2 cannot simultaneously use an identical resource R. Process P1 cannot utilize
the resource R simultaneously with Process P2, yet Process P2 has access to
it.
Hold & Wait: "Hold & Wait" refers to a situation where a process holds
resources and requests additional ones held by other processes. For instance,
a process P1 may be demanding a resource R3 that the process P2 already
has while preserving two resources, R1 and R2.
Preemption is not permitted: Another process cannot require a resource to
leave a process. For instance, if resource R is used by process P1, process
P2 cannot take it. If so, it is not required to use various scheduling methods. To
obtain the resource R, process P2 must first wait for process P1 to stop
using it.

C program for implementation of the Deadlock detection Algorithm

#include <stdio.h>
#define MAX_PROCESSES 10
#define MAX_RESOURCES 10

int allocation[MAX_PROCESSES][MAX_RESOURCES];
int request[MAX_PROCESSES][MAX_RESOURCES];
int available[MAX_RESOURCES];
int resources[MAX_RESOURCES];
int work[MAX_RESOURCES];
int marked[MAX_PROCESSES];

int main() {
int num_processes, num_resources;

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


scanf("%d", &num_processes);

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


scanf("%d", &num_resources);

// Input total resources


for (int i = 0; i < num_resources; i++) {
printf("Enter the total amount of Resource R%d: ", i + 1);
scanf("%d", &resources[i]);
}

// Input request matrix


printf("Enter the request matrix:\n");
for (int i = 0; i < num_processes; i++) {
for (int j = 0; j < num_resources; j++) {
scanf("%d", &request[i][j]);
}
}

// User Input allocation matrix


printf("Enter the allocation matrix:\n");
for (int i = 0; i < num_processes; i++) {
for (int j = 0; j < num_resources; j++) {
scanf("%d", &allocation[i][j]);
}
}

// Initialization of the available resources


for (int j = 0; j < num_resources; j++) {
available[j] = resources[j];
for (int i = 0; i < num_processes; i++) {
available[j] -= allocation[i][j];
}
}

// Mark processes with zero allocation


for (int i = 0; i < num_processes; i++) {
int count = 0;
for (int j = 0; j < num_resources; j++) {
if (allocation[i][j] == 0) {
count++;
} else {
break;
}
}
if (count == num_resources) {
marked[i] = 1;
}
}

// Initialize work with available


for (int j = 0; j < num_resources; j++) {
work[j] = available[j];
}

// Mark processes with requests <= work


for (int i = 0; i < num_processes; i++) {
int can_be_processed = 1;
if (marked[i] != 1) {
for (int j = 0; j < num_resources; j++) {
if (request[i][j] > work[j]) {
can_be_processed = 0;
break;
}
}
if (can_be_processed) {
marked[i] = 1;
for (int j = 0; j < num_resources; j++) {
work[j] += allocation[i][j];
}
}
}
}

// Check for unmarked processes (deadlock)


int deadlock = 0;
for (int i = 0; i < num_processes; i++) {
if (marked[i] != 1) {
deadlock = 1;
break;
}
}

if (deadlock) {
printf("Deadlock detected\n");
} else {
printf("No deadlock possible\n");
}

return 0;
}

Output:
Enter the number of processes: 3
Enter the number of resources: 3
Enter the total amount of Resource R1: 4
Enter the total amount of Resource R2: 5
Enter the total amount of Resource R3: 7
Enter the request matrix:
123
456
789
Enter the allocation matrix:
567123789
Deadlock detected

You might also like