Exp 8
Exp 8
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.
#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;
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