-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphase1.c
96 lines (84 loc) · 2.09 KB
/
phase1.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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define TRUE 1
#define FALSE 0
#define NINJA_COUNT 14
#define PIRATE_COUNT 18
pthread_t ninjaThreads[NINJA_COUNT];
pthread_t pirateThreads[PIRATE_COUNT];
sem_t ninjas, pirates;
int ninjasInRoom = 0;
int piratesInRoom = 0;
// Ninjas
void *run_ninja(void *ninjaID) {
while (TRUE) {
int ninjaValue;
sem_wait(&ninjas);
ninjasInRoom++;
printf("Ninja %d is in the room\n", (int) ninjaID);
sleep(1); // Change costume
printf("Ninja %d is leaving the room\n", (int) ninjaID);
ninjasInRoom--;
sem_getvalue(&ninjas, &ninjaValue);
if (ninjasInRoom == 0 && ninjaValue == 0) {
sem_post(&pirates);
sem_post(&pirates);
}
}
pthread_exit(NULL);
}
// Pirates
void *run_pirate(void *pirateID) {
while (TRUE) {
int pirateValue;
sem_wait(&pirates);
piratesInRoom++;
printf("Pirate %d is in the room\n", (int) pirateID);
sleep(1); // Change costume
printf("Pirate %d is leaving the room\n", (int) pirateID);
piratesInRoom--;
sem_getvalue(&pirates, &pirateValue);
if (piratesInRoom == 0 && pirateValue == 0) {
sem_post(&ninjas);
sem_post(&ninjas);
}
}
pthread_exit(NULL);
}
int main(int argc, char *argv[]) {
// Create our threads
sem_init(&ninjas, 0, 2); // Start by allowing 2 ninjas
sem_init(&pirates, 0, 0); // Start by not allowing pirates
int i, status;
for (i=0; i < NINJA_COUNT; i++) {
status = pthread_create(&ninjaThreads[i], NULL, run_ninja, (void *) i);
if (status != 0) {
printf("Error creating ninja #%d!\n", i);
exit(-1);
}
}
for (i=0; i < PIRATE_COUNT; i++) {
status = pthread_create(&pirateThreads[i], NULL, run_pirate, (void *) i);
if (status != 0) {
printf("Error creating pirate #%d!\n", i);
exit(-1);
}
}
for (i=0; i < NINJA_COUNT; i++) {
status = pthread_join(ninjaThreads[i], NULL);
if (status != 0) {
printf("Error joining ninja #%d!\n", i);
exit(-1);
}
}
for (i=0; i < PIRATE_COUNT; i++) {
status = pthread_join(pirateThreads[i], NULL);
if (status != 0) {
printf("Error joining pirate #%d!\n", i);
exit(-1);
}
}
exit(0);
}