-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSantaClausProblem.c
136 lines (120 loc) · 2.53 KB
/
SantaClausProblem.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <pthread.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <stdbool.h>
#include <semaphore.h>
#include <stdlib.h>
#include <time.h>
pthread_t *CreateThread(void *(*f)(void *), void *a)
{
pthread_t *t = malloc(sizeof(pthread_t));
assert(t != NULL);
int ret = pthread_create(t, NULL, f, a);
assert(ret == 0);
return t;
}
static int elves;
static int reindeer;
static sem_t santaSem;
static sem_t reindeerSem;
static sem_t elfTex;
static sem_t mutex;
static sem_t c_Elves;
void *SantaClaus(void *arg)
{
while (true)
{
sem_wait(&santaSem);
sem_wait(&mutex);
if (reindeer == 9)
{
printf("\nSanta Claus: Preparing Sleigh!! And Giving Gifts\n\n");
for (int r = 0; r < 9; r++)
sem_post(&reindeerSem);
reindeer=0;
}
else if (elves == 3)
{
printf("\nSanta Claus: helping elves\n\n");
for (int r = 0; r < 3; r++)
sem_post(&c_Elves);
}
sem_post(&mutex);
}
return arg;
}
void *Reindeer(void *arg)
{
int id = (int)arg;
sleep(rand() % 5);
while (true)
{
sem_wait(&mutex);
if(reindeer==0){
printf("\n-----Christmas is coming!!------\n\n");
}
printf("The reindeer %d arrives\n", id);
reindeer++;
if (reindeer == 9)
sem_post(&santaSem);
sem_post(&mutex);
sem_wait(&reindeerSem);
printf("Reindeer %d getting hitched\n", id);
sleep(3+rand() % 5);
}
return arg;
}
void *Elve(void *arg)
{
while (true)
{
int id = (int)arg;
srand((unsigned int)time(NULL));
int random = rand() % (30+id*2);
if (random<10)
{
printf("Elve %d need help from Santa\n", id);
sem_wait(&elfTex);
sem_wait(&mutex);
elves++;
if (elves == 3)
sem_post(&santaSem);
else
sem_post(&elfTex);
sem_post(&mutex);
sem_wait(&c_Elves);
printf("Elve %d will get help from Santa Claus\n", id);
sleep(2);
printf("Done!! Elve %d go back to making toy\n", id);
sleep(2);
sem_wait(&mutex);
elves--;
if (elves == 0)
sem_post(&elfTex);
sem_post(&mutex);
sleep(rand() % (5));
}
}
return arg;
}
int main(int ac, char **av)
{
elves = 0;
reindeer = 0;
sem_init(&santaSem, 0, 0);
sem_init(&reindeerSem, 0, 0);
sem_init(&elfTex, 0, 1);
sem_init(&mutex, 0, 1);
sem_init(&c_Elves, 0, 0);
pthread_t *santa_claus = CreateThread(SantaClaus, 0);
pthread_t *reindeers[9];
for (int r = 0; r < 9; r++)
reindeers[r] = CreateThread(Reindeer, (void *)r + 1);
pthread_t *elves[10];
for (int e = 0; e < 10; e++)
elves[e] = CreateThread(Elve, (void *)e + 1);
int ret = pthread_join(*santa_claus, NULL);
assert(ret == 0);
}