-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphase1_notes.c
57 lines (51 loc) · 1.09 KB
/
phase1_notes.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
mutex mutex;
int[PIRATE_COUNT] piratesInRoom;
int[NINJA_COUNT] ninjasInRoom;
int ninjaAccess;
int pirateAccess;
// Ninja Code
int ninjaID = 0; // Some real value
while(TRUE) {
// Code to enter the costume department
down(mutex);
if (ninjaAccess && !ninjasInRoom[ninjaID]) {
ninjas++;
ninjasInRoom[ninjaID] = TRUE;
}
up(mutex);
// Code to get dressed and leave
down(mutex);
if (ninjaAccess && ninjasInRoom[ninjaID]) {
sleep(1); // Perform costume change
ninjas--;
ninjasInRoom[ninjaID] = FALSE;
if (ninjas == 0) {
ninjaAccess = FALSE;
pirateAccess = TRUE;
}
}
up(mutex);
}
// Pirate Code
int pirateID = 0; // Some real value
while(TRUE) {
// Code to enter the costume department
down(mutex);
if (pirateAccess && !piratesInRoom[pirateID]) {
pirates++;
piratesInRoom[pirateID] = TRUE;
}
up(mutex);
// Code to get dressed and leave
down(mutex);
if (pirateAccess && piratesInRoom[pirateID]) {
sleep(1); // Perform costume change
pirates--;
piratesInRoom[pirateID] = FALSE;
if (pirates == 0) {
ninjaAccess = TRUE;
pirateAccess = FALSE;
}
}
up(mutex);
}