-
Notifications
You must be signed in to change notification settings - Fork 0
/
symposium.c
234 lines (182 loc) · 5.31 KB
/
symposium.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <string.h>
#include "util.h"
#include "bios.h"
#include "tinyos.h"
#include "symposium.h"
#define QUIET 0 /* Use 1 for supperssing printing (for timing tests), 0 for normal printing */
/*
This file contains a number of example programs for tinyos.
*/
void adjust_symposium(symposium_t* symp, int dBASE, int dGAP)
{
symp->fmin = FBASE + dBASE -
(int)( log((double)(2*symp->N*symp->bites))/log(.5+.5*sqrt(5.)));
symp->fmax = symp->fmin + FGAP +dGAP;
}
/* We use the recursive definition of Fibonacci numbers, implemented by the routines below,
to "burn" CPU cycles. The complexity of the routine is exponential in n.
*/
int fiborand(int fmin, int fmax) { return lrand48() % (fmax-fmin+1) + fmin; }
unsigned int fibo(unsigned int n) /* Very slow routine */
{
if(n<2) return n;
else return fibo(n-1)+fibo(n-2);
}
/*
Dining Philosophers.
In this implementation of the Dining Philosohpers problem, each philosopher
is thinking about Fibonacci numbers...
The implementation follows the monitor pattern.
*/
/* utilities */
int LEFT(int i, int N) { return (i+1) % N; }
int RIGHT(int i, int N) { return (i+N-1) % N; }
/* Prints the current state given a change (described by fmt) for
philosopher ph */
void print_state(int N, PHIL* state, const char* fmt, int ph)
{
#if QUIET==0
int i;
if(N<100) {
for(i=0;i<N;i++) {
char c= (".THE")[state[i]];
if(i==ph) printf("[%c]", c);
else printf(" %c ", c);
}
}
printf(fmt, ph);
#endif
}
/* Functions think and eat (just burn CPU cycles). */
void think(int fmin, int fmax) { fibo(fiborand(fmin, fmax)); }
void eat(int fmin, int fmax) { think(fmin, fmax); }
/* Attempt to make a (hungry) philosopher i to start eating */
void trytoeat(SymposiumTable* S, int i)
{
int N = S->symp->N;
PHIL* state = S->state;
if(state[i]==HUNGRY && state[LEFT(i,N)]!=EATING && state[RIGHT(i,N)]!=EATING) {
state[i] = EATING;
print_state(N, state, " %d is eating\n",i);
Cond_Signal(&(S->hungry[i]));
}
}
void SymposiumTable_philosopher(SymposiumTable* S, int i)
{
/* cache locally for convenience */
int N = S->symp->N;
int bites = S->symp->bites;
int fmin = S->symp->fmin;
int fmax = S->symp->fmax;
PHIL* state = S->state;
Mutex_Lock(& S->mx); /* Philosopher arrives in thinking state */
state[i] = THINKING;
print_state(N, state, " %d has arrived\n",i);
Mutex_Unlock(& S->mx);
for(int j=0; j<bites; j++) { /* Number of bites (mpoykies) */
think(fmin, fmax);
Mutex_Lock(& S->mx);
state[i] = HUNGRY;
trytoeat(S,i); /* This may not succeed */
while(state[i]==HUNGRY) {
print_state(N, state, " %d waits hungry\n",i);
Cond_Wait(& S->mx, &(S->hungry[i])); /* If hungry we sleep. trytoeat(i) will wake us. */
}
assert(state[i]==EATING);
Mutex_Unlock(& S->mx);
eat(fmin, fmax);
Mutex_Lock(& S->mx);
state[i] = THINKING; /* We are done eating, think again */
print_state(N, state, " %d is thinking\n",i);
trytoeat(S, LEFT(i,N)); /* Check if our left and right can eat NOW. */
trytoeat(S, RIGHT(i,N));
Mutex_Unlock(& S->mx);
}
Mutex_Lock(& S->mx);
state[i] = NOTHERE; /* We are done (eaten all the bites) */
print_state(N, state, " %d is leaving\n",i);
Mutex_Unlock(& S->mx);
}
void SymposiumTable_init(SymposiumTable* table, symposium_t* symp)
{
table->symp = symp;
table->mx = MUTEX_INIT;
table->state = (PHIL*) xmalloc(symp->N * sizeof(PHIL));
table->hungry = (CondVar*) xmalloc(symp->N * sizeof(CondVar));
for(int i=0; i<symp->N; i++) {
table->state[i] = NOTHERE;
table->hungry[i] = COND_INIT;
}
}
void SymposiumTable_destroy(SymposiumTable* table)
{
free(table->state);
free(table->hungry);
}
typedef struct { int i; SymposiumTable* S; } philosopher_args;
/* Philosopher process */
int PhilosopherProcess(int argl, void* args)
{
assert(argl == sizeof(philosopher_args));
philosopher_args* A = args;
SymposiumTable_philosopher(A->S, A->i);
return 0;
}
/*
This process executes a "symposium" for a number of philosophers.
*/
int SymposiumOfProcesses(int argl, void* args)
{
assert(argl == sizeof(symposium_t));
symposium_t* symp = args;
int N = symp->N;
/* Initialize structures */
SymposiumTable S;
SymposiumTable_init(&S, symp);
/* Execute philosophers */
for(int i=0;i<N;i++) {
philosopher_args Args;
Args.i = i;
Args.S = &S;
Exec(PhilosopherProcess, sizeof(Args), &Args);
}
/* Wait for philosophers to exit */
for(int i=0;i<N;i++) {
WaitChild(NOPROC, NULL);
}
SymposiumTable_destroy(&S);
return 0;
}
int PhilosopherThread(int i, void* symp)
{
SymposiumTable_philosopher((SymposiumTable*) symp, i);
return 0;
}
/*
This process executes a "symposium" for a number of philosophers.
Each philosopher is a thread.
*/
int SymposiumOfThreads(int argl, void* args)
{
assert(argl == sizeof(symposium_t));
symposium_t* symp = args;
int N = symp->N;
/* Initialize structures */
SymposiumTable S;
SymposiumTable_init(&S, symp);
/* Execute philosophers */
Tid_t thread[symp->N];
for(int i=0;i<N;i++) {
thread[i] = CreateThread(PhilosopherThread, i, &S);
}
/* Wait for philosophers to exit */
for(int i=0;i<N;i++) {
ThreadJoin(thread[i],NULL);
}
SymposiumTable_destroy(&S);
return 0;
}