-
Notifications
You must be signed in to change notification settings - Fork 1
/
deadLockDetection.c
115 lines (95 loc) · 3.02 KB
/
deadLockDetection.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <stdio.h>
// Global variables declaration
int i, j, k, nop, nor, completed = 0;
int alloc[10][10], request[10][10], avail[10], finish[10] = {0};
int main()
{
// Prompt for the number of processes and resources
printf("Enter the Number of Processes : ");
scanf("%d", &nop);
printf("Enter the Number of Resources : ");
scanf("%d", &nor);
// Prompt for the allocation matrix
printf("Enter the Allocation Matrix : \n");
for (i = 0; i < nop; i++)
for (j = 0; j < nor; j++)
scanf("%d", &alloc[i][j]);
// Prompt for the request matrix
printf("Enter the Request Matrix : \n");
for (i = 0; i < nop; i++)
for (j = 0; j < nor; j++)
scanf("%d", &request[i][j]);
// Prompt for the available matrix
printf("Enter the Available Matrix : ");
for (i = 0; i < nor; i++)
scanf("%d", &avail[i]);
// Deadlock detection algorithm
for (k = 0; k < nop; k++) {
for (i = 0; i < nop; i++) {
if (finish[i] == 0) {
int flag = 0;
// Check if the current process can be satisfied with available resources
for (j = 0; j < nor; j++) {
if (request[i][j] > avail[j]) {
flag = 1;
break;
}
}
// If all the requested resources can be allocated, mark the process as finished
if (flag == 0) {
for (j = 0; j < nor; j++)
avail[j] += alloc[i][j];
finish[i] = 1;
completed++;
}
}
}
}
// Check if all processes have been completed to determine the system's state
if (completed == nop)
printf("No DeadLock - System is in Safe mode :)");
else
{
printf("\nThe System is in DeadLock STATE :(\n");
printf("DeadLock Causing Process are : ");
// Print the processes causing deadlock
for (i = 0; i < nop; i++)
{
if (finish[i] == 0)
printf(" P%d", i);
}
}
}
/*
-----------------------Output -1--------------------
Enter the Number of Processes : 5
Enter the Number of Resources : 3
Enter the Allocation Matrix :
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Enter the Request Matrix :
0 0 0
2 0 2
0 0 0
1 0 0
0 0 2
Enter the Available Matrix : 0 0 0
No DeadLock - System is in Safe mode :)
--------------------Output-2-----------------------
Enter the Number of Processes : 3
Enter the Number of Resources : 3
Enter the Allocation Matrix :
3 6 8
4 3 3
3 4 4
Enter the Request Matrix :
3 3 3
2 0 3
1 2 4
Enter the Available Matrix : 1 2 0
The System is in DeadLock STATE :(
DeadLock Causing Process are : P0 P1 P2
*/