-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation.c
393 lines (341 loc) · 11.6 KB
/
simulation.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/**
* \brief Civilized philosophers buffet
*
* Simulation of SO's second assignment.
*
* \author Miguel Oliveira e Silva - 2016
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <assert.h>
#include <string.h>
#include "parameters.h"
#include "dining-room.h"
#include "logger.h"
#include <time.h>
#include "pthread.h"
/* internal functions */
static void help(char* prog);
static void processArgs(Parameters *params, int argc, char* argv[]);
static void showParams(Parameters *params);
static Threads* go(Simulation* sim, Threads* thr);
static void finish(Simulation* sim, Threads* thr);
int getLifeTime(Simulation* sim);
pthread_mutex_t accessCR = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t wantCutlery = PTHREAD_COND_INITIALIZER;
pthread_cond_t wantPizza = PTHREAD_COND_INITIALIZER;
pthread_cond_t wantSpaghetti = PTHREAD_COND_INITIALIZER;
pthread_cond_t waiterNeeded = PTHREAD_COND_INITIALIZER;
int main(int argc, char* argv[])
{
// default parameter values:
Parameters params = {5,10,100,10,10,10,10,20,50,10,15};
processArgs(¶ms, argc, argv);
showParams(¶ms);
printf("<press RETURN>");
getchar();
int i;
Simulation* sim = initSimulation(NULL, ¶ms);
/* initialize all auxiliar structures */
Threads* thr = mem_alloc(sizeof(Threads));
thr->t_philo = mem_alloc(sizeof(pthread_t) * sim->params->NUM_PHILOSOPHERS);
for(i = 0; i < sim->params->NUM_PHILOSOPHERS; i++)
thr->t_philo[i] = (pthread_t) mem_alloc(sizeof(pthread_t));
/* show updates */
logger(sim);
thr = go(sim, thr);
finish(sim, thr);
return 0;
}
/**
* launch threads/processes for philosophers and waiter
*/
static Threads* go(Simulation* sim, Threads* thr)
{
assert(sim != NULL);
int i, n;
n = sim->params->NUM_PHILOSOPHERS;
THREAD_PAR* val= (THREAD_PAR*) mem_alloc(sizeof(THREAD_PAR)*n);
/* create waiter thread */
if(pthread_create(&thr->t_waiter,NULL,(void *)waiterLife,(void*)sim) != 0)
{
perror("Erro na criação da thread!");
exit(EXIT_FAILURE);
}
/* create philosopher threads */
for(i = 0; i < n; i++)
{
val[i].phi_id=i;
val[i].sim=sim;
val[i].lifetime = getLifeTime(sim);
if(pthread_create(&thr->t_philo[i], NULL, life_cycle, &val[i]) != 0)
{
perror("Erro na criação da thread!");
exit(EXIT_FAILURE);
}
}
return thr;
}
/*
* obtain the randomly selected life time
*/
int getLifeTime(Simulation* sim)
{
time_t t;
int min = sim->params->PHILOSOPHER_MIN_LIVE;
int max = sim->params->PHILOSOPHER_MAX_LIVE;
srand((unsigned) time(&t));
return (unsigned int) rand() % max + min;
}
/**
* Wait for the death of all philosophers, and request and wait for waiter dead.
*/
static void finish(Simulation* sim, Threads* thr)
{
assert(sim != NULL);
int i, n;
void *status = 0;
n = sim->params->NUM_PHILOSOPHERS;
for(i = 0; i < n; i++)
{
if(pthread_join(thr->t_philo[i], &status) != 0)
{
perror("Error on waiting for philosopher thread");
exit(EXIT_FAILURE);
}
}
if(pthread_join(thr->t_waiter, &status) != 0)
{
perror("Error on waiting for waiter thread");
exit(EXIT_FAILURE);
}
}
Simulation* initSimulation(Simulation* sim, Parameters* params)
{
assert(params != NULL);
int i;
Simulation* result = sim;
if (result == NULL)
result = (Simulation*)mem_alloc(sizeof(Simulation));
// simulation parameters:
result->params = (Parameters*)mem_alloc(sizeof(Parameters));
memcpy(result->params, params, sizeof(Parameters));
// default DiningRoom values:
result->diningRoom = (DiningRoom*)mem_alloc(sizeof(DiningRoom));
DiningRoom s = {params->NUM_PIZZA, params->NUM_SPAGHETTI, params->NUM_FORKS, params->NUM_KNIVES, 0, 0, 0, 0, params->NUM_PHILOSOPHERS};
memcpy(result->diningRoom, &s, sizeof(DiningRoom));
// Philosopher:
Philosopher p = {P_BIRTH,P_NONE,{P_NOTHING,P_NOTHING}};
result->philosophers = (Philosopher**)mem_alloc(params->NUM_PHILOSOPHERS*sizeof(Philosopher*));
for(i = 0; i < params->NUM_PHILOSOPHERS; i++)
{
result->philosophers[i] = (Philosopher*)mem_alloc(sizeof(Philosopher));
memcpy(result->philosophers[i], &p, sizeof(Philosopher));
}
// Waiter:
Waiter w = {W_NONE,W_INACTIVE,W_INACTIVE,W_INACTIVE};
result->waiter = (Waiter*)mem_alloc(sizeof(Waiter));
memcpy(result->waiter, &w, sizeof(Waiter));
return result;
}
/*********************************************************************/
// No need to change remaining code!
static void help(char* prog)
{
assert(prog != NULL);
printf("\n");
printf("Usage: %s [OPTION] ...\n", prog);
printf("\n");
printf("Options:\n");
printf("\n");
printf(" -h, --help show this help\n");
printf(" -n, --num-philosophers set number of philosophers (default is 5)\n");
printf(" -l, --min-life set minimum number of iterations of philosophers life cycle (default is 10)\n");
printf(" -L, --max-life set maximum number of iterations of philosophers life cycle (default is 100)\n");
printf(" -f, --num-forks set number of forks (default is 3)\n");
printf(" -k, --num-knives set number of knives (default is 2)\n");
printf(" -p, --pizza set number of pizza meals in each replenish operation (default is 10)\n");
printf(" -s, --spaghetti set number of spaghetti meals in each replenish operation (default is 10)\n");
printf(" -t, --think-time set maximum milliseconds for thinking (default is 20)\n");
printf(" -c, --choose-pizza-prob set probability to choose a pizza meal against a spaghetti meal (default is 50)\n");
printf(" -e, --eat-time set maximum milliseconds for eating (default is 10)\n");
printf(" -w, --wash-time set maximum milliseconds for washing (default is 15)\n");
printf("\n");
}
static void processArgs(Parameters *params, int argc, char* argv[])
{
assert(params != NULL);
assert(argc >= 0 && argv != NULL && argv[0] != NULL);
static struct option long_options[] =
{
{"help", no_argument, NULL, 'h' },
{"num-philosophers", required_argument, NULL, 'n' },
{"min-life", required_argument, NULL, 'l' },
{"max-life", required_argument, NULL, 'L' },
{"num-forks", required_argument, NULL, 'f' },
{"num-knives", required_argument, NULL, 'k' },
{"pizza", required_argument, NULL, 'p' },
{"spaghetti", required_argument, NULL, 's' },
{"think-time", required_argument, NULL, 't' },
{"choose-pizza-prob",required_argument, NULL, 'c' },
{"eat-time", required_argument, NULL, 'e' },
{"wash-time", required_argument, NULL, 'w' },
{0, 0, NULL, 0 }
};
int op=0;
while (op != -1)
{
int option_index = 0;
op = getopt_long(argc, argv, "hn:l:L:f:k:p:s:t:c:e:w:", long_options, &option_index);
int n; // integer number
switch (op)
{
case -1:
break;
case 'h':
help(argv[0]);
exit(EXIT_SUCCESS);
case 'n':
n = atoi(optarg);
if (n < 1)
{
fprintf(stderr, "ERROR: invalid number of philosophers \"%s\"\n", optarg);
exit(EXIT_FAILURE);
}
params->NUM_PHILOSOPHERS = n;
break;
case 'l':
n = atoi(optarg);
if (n < 0 || (n == 0 && strcmp(optarg, "0") != 0))
{
fprintf(stderr, "ERROR: invalid minimum philosophers life \"%s\"\n", optarg);
exit(EXIT_FAILURE);
}
params->PHILOSOPHER_MIN_LIVE = n;
break;
case 'L':
n = atoi(optarg);
if (n < 0 || n < params->PHILOSOPHER_MIN_LIVE || (n == 0 && strcmp(optarg, "0") != 0))
{
fprintf(stderr, "ERROR: invalid maximum philosophers life \"%s\"\n", optarg);
exit(EXIT_FAILURE);
}
params->PHILOSOPHER_MAX_LIVE = n;
break;
case 'f':
n = atoi(optarg);
if (n < 2)
{
fprintf(stderr, "ERROR: invalid number of forks \"%s\"\n", optarg);
exit(EXIT_FAILURE);
}
params->NUM_FORKS = n;
break;
case 'k':
n = atoi(optarg);
if (n < 1)
{
fprintf(stderr, "ERROR: invalid number of knives \"%s\"\n", optarg);
exit(EXIT_FAILURE);
}
params->NUM_KNIVES = n;
break;
case 'p':
n = atoi(optarg);
if (n < 1)
{
fprintf(stderr, "ERROR: invalid number of pizza meals \"%s\"\n", optarg);
exit(EXIT_FAILURE);
}
params->NUM_PIZZA = n;
break;
case 's':
n = atoi(optarg);
if (n < 1)
{
fprintf(stderr, "ERROR: invalid number of spaghetti meals \"%s\"\n", optarg);
exit(EXIT_FAILURE);
}
params->NUM_SPAGHETTI = n;
break;
case 't':
n = atoi(optarg);
if (n < 0 || (n == 0 && strcmp(optarg, "0") != 0))
{
fprintf(stderr, "ERROR: invalid think time \"%s\"\n", optarg);
exit(EXIT_FAILURE);
}
params->THINK_TIME = n;
break;
case 'c':
n = atoi(optarg);
if (n < 0 || n > 100 || (n == 0 && strcmp(optarg, "0") != 0))
{
fprintf(stderr, "ERROR: invalid percentage for choosing pizza against spaghetti meals \"%s\"\n", optarg);
exit(EXIT_FAILURE);
}
params->CHOOSE_PIZZA_PROB = n;
break;
case 'e':
n = atoi(optarg);
if (n < 0 || (n == 0 && strcmp(optarg, "0") != 0))
{
fprintf(stderr, "ERROR: invalid eat time \"%s\"\n", optarg);
exit(EXIT_FAILURE);
}
params->EAT_TIME = n;
break;
case 'w':
n = atoi(optarg);
if (n < 0 || (n == 0 && strcmp(optarg, "0") != 0))
{
fprintf(stderr, "ERROR: invalid wash time \"%s\"\n", optarg);
exit(EXIT_FAILURE);
}
params->WASH_TIME = n;
break;
default:
help(argv[0]);
exit(EXIT_FAILURE);
break;
}
}
if (optind < argc)
{
fprintf(stderr, "ERROR: invalid extra arguments\n");
exit(EXIT_FAILURE);
}
}
static void showParams(Parameters *params)
{
assert(params != NULL);
printf("\n");
printf("Simulation parameters:\n");
printf(" --num-philosophers: %d\n", params->NUM_PHILOSOPHERS);
printf(" --min-life: %d\n", params->PHILOSOPHER_MIN_LIVE);
printf(" --max-life: %d\n", params->PHILOSOPHER_MAX_LIVE);
printf(" --num-forks: %d\n", params->NUM_FORKS);
printf(" --num-knives: %d\n", params->NUM_KNIVES);
printf(" --pizza: %d\n", params->NUM_PIZZA);
printf(" --spaghetti: %d\n", params->NUM_SPAGHETTI);
printf(" --think-time: %d\n", params->THINK_TIME);
printf(" --choose-pizza-prob: %d\n", params->CHOOSE_PIZZA_PROB);
printf(" --eat-time: %d\n", params->EAT_TIME);
printf(" --wash-time: %d\n", params->WASH_TIME);
printf("\n");
}
/**
* Memory error is not recoverable.
*/
void* mem_alloc(int size)
{
void* result = malloc(size);
if (result == NULL)
{
fprintf(stderr, "ERROR: no memory!\n");
exit(EXIT_FAILURE);
}
return result;
}