-
Notifications
You must be signed in to change notification settings - Fork 1
/
safe.c
73 lines (63 loc) · 2.1 KB
/
safe.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
#include "matrix.h"
/* safe.c: A key sub-task within the Banker's Algorithm is
determining if a system is in a safe state or an unsafe
state. This "safety algorithm" is used to determine if resource
allocation requests from processes or threads can be granted
without resulting in an unsafe system state. This is the
essence of how the Banker's Algorithm achieves deadlock avoidance.
Author: Jason Franklin. */
/* declare external, global data structures */
extern int NUMBER_OF_PROCESSES;
extern int NUMBER_OF_RESOURCES;
extern Vector AVAILABLE;
extern Matrix MAX;
extern Matrix ALLOCATION;
extern Matrix NEED;
/* prototypes for helper functions */
Vector finish(void);
Vector work(void);
/* safe: return 1 if system state is safe, 0 if system state is
unsafe */
int safe(void)
{
int result = 1;
/* create and initialize WORK and FINISH */
Vector WORK = work();
Vector FINISH = finish();
/* establish the existence of a safe sequence */
int i, j;
for (i = 0; i < NUMBER_OF_PROCESSES; i++)
if (FINISH[i] == 0 && vector_lte(NEED[i], WORK, NUMBER_OF_RESOURCES)) {
for (j = 0; j < NUMBER_OF_RESOURCES; j++)
WORK[j] += ALLOCATION[i][j];
FINISH[i] = 1;
i = -1; /* after one process "finishes" search for another */
}
for (i = 0; i < NUMBER_OF_PROCESSES; i++)
if (!FINISH[i]) {
result = 0; /* no safe sequence! */
break;
}
/* de-allocate heap memory space */
vector_destroy(WORK);
vector_destroy(FINISH);
return result;
}
/* finish: helper function to create and initialize FINISH vector */
Vector finish(void)
{
Vector FINISH = vector_create(NUMBER_OF_PROCESSES);
int i;
for (i = 0; i < NUMBER_OF_PROCESSES; i++)
FINISH[i] = 0; /* 0 represents "false" */
return FINISH;
}
/* work: helper function to create and initialize WORK vector */
Vector work(void)
{
Vector WORK = vector_create(NUMBER_OF_RESOURCES);
int i;
for (i = 0; i < NUMBER_OF_RESOURCES; i++)
WORK[i] = AVAILABLE[i];
return WORK;
}