This repository was archived by the owner on Sep 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
646 lines (552 loc) · 19.7 KB
/
main.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#include <stdio.h>
#include <math.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <memory.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#ifdef DEBUG
#define DERROR(fmt, args...) fprintf(stderr, "DEBUG: %s:%d:%s(): " fmt, __FILE__, __LINE__, __func__, ##args)
#else
#define DERROR(fmt, args...)
#endif
#define strcmpSafe(str1, str2, len1, len2) strncmp(str1, str2, sizeof(char) * (len1 < len2 ? len1 : len2))
void producer(int const numc, int const linec, int const fd, pid_t const child);
void consumer(int const numc, int const fd, pid_t const parent);
void signalHandler(int const sig);
bool systemCallFailed(char *const source, char *const msg, int error);
void teardown(char *const source);
bool readAll(int fd, void *buffer, size_t size);
bool writeAll(int fd, void *buffer, size_t size);
void dft(double *numbers, size_t N, size_t k, double *real, double *imag);
void cleanFP(FILE **fpp);
void cleanDouble(double **dpp);
#define R_SIGINT 1
#define R_SIGUSR2 2
#define R_SIGUSR1 3
volatile sig_atomic_t loop = true;
volatile sig_atomic_t reason = 0;
int main(int argc, char *argv[]) {
int pid;
int n = 0;
int m = 0;
char *x = NULL;
int f;
sigset_t sigbl;
if (argc < 7) {
printf("Usage: %s -N n -X x -M m\n", argv[0]);
printf(" -N n: Number of real numbers per line.\n");
printf(" -X x: Name of the communication file.\n");
printf(" -M m: Maximum number of lines in file.\n");
return 1;
}
srand(time(NULL));
for (int i = 0; i < argc - 1; ++i) {
int len = strlen(argv[i]);
if (strcmpSafe(argv[i], "-N", len, 2) == 0) {
sscanf(argv[i + 1], "%d", &n);
if (n == 0) {
printf("-N cannot be: %s\n", argv[i + 1]);
return 1;
}
} else if (strcmpSafe(argv[i], "-X", len, 2) == 0) {
x = argv[i + 1];
} else if (strcmpSafe(argv[i], "-M", len, 2) == 0) {
sscanf(argv[i + 1], "%d", &m);
if (m == 0) {
printf("-M cannot be: %s\n", argv[i + 1]);
return 1;
}
}
}
int fd = open(x, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd == -1) {
perror("[OPEN]");
return 1;
}
if (unlink(x) == -1) {
perror("[UNLINK]");
return 1;
}
struct sigaction handler;
handler.sa_handler = signalHandler;
handler.sa_flags = SA_RESTART;
int error = sigemptyset(&handler.sa_mask);
if (error == -1) {
DERROR("[INIT] Cannot empty signal mask.\n");
perror("[INIT] sigemptyset failed");
return 1;
}
error = sigaction(SIGINT, &handler, NULL);
if (error == -1) {
DERROR("[INIT] Cannot set sigaction(SIGINT).\n");
perror("[INIT] sigaction failed(SIGINT)");
return 1;
}
error = sigaction(SIGUSR1, &handler, NULL);
if (error == -1) {
DERROR("[INIT] Cannot set sigaction.\n");
perror("[INIT] sigaction failed");
return 1;
}
error = sigaction(SIGUSR2, &handler, NULL);
if (error == -1) {
DERROR("[INIT] Cannot set sigaction.\n");
perror("[INIT] sigaction failed");
return 1;
}
/* Initialize the sigprocmask sigset */
error = sigemptyset(&sigbl);
if (error == -1) {
perror("[INIT] sigemptyset failed");
return 1;
}
error = sigaddset(&sigbl, SIGINT);
if (error == -1) {
perror("[INIT] sigaddset failed(SIGINT)");
return 1;
}
error = sigaddset(&sigbl, SIGUSR1);
if (error == -1) {
perror("[INIT] sigaddset failed(SIGUSR1)");
return 1;
}
error = sigaddset(&sigbl, SIGUSR2);
if (error == -1) {
perror("[INIT] sigaddset failed(SIGUSR2)");
return 1;
}
/* Block signals to make every system call as safe as possible. */
DERROR("[INIT] Blocking signals.\n");
error = sigprocmask(SIG_BLOCK, &sigbl, NULL);
if (error == -1) {
perror("[INIT] SIG_BLOCK failed");
return 1;
}
switch (pid = fork()) {
case 0:
/* Child process: Consumer */
DERROR("[CHILD] pid: %d\n", getpid());
consumer(n, fd, getppid());
close(fd);
teardown("[CHILD]");
DERROR("[CHILD] shutdown reason: %s\n",
(reason == R_SIGINT ? "SIGINT" : (reason == R_SIGUSR1 ? "SIGUSR1" : "SIGUSR2")));
return 0;
case -1:
/* Error */
break;
default:
/* Parent process: Producer */
DERROR("[PARENT] child pid: %d\n", pid);
producer(n, m, fd, pid);
close(fd);
teardown("[PARENT]");
while ((pid = wait(NULL)) != -1)
DERROR("[PARENT] child handled: %d\n", pid);
DERROR("[PARENT] all child are handled.\n");
DERROR("[PARENT] shutdown reason: %s\n",
(reason == R_SIGINT ? "SIGINT" : (reason == R_SIGUSR1 ? "SIGUSR1" : "SIGUSR2")));
return 0;
}
}
void producer(int const numc, int const linec, int const fd, pid_t const child) {
struct flock lock;
off_t curr = 0;
int error;
sigset_t sigsp;
double number;
sigset_t sigbl;
FILE *logfp __attribute__((__cleanup__(cleanFP))) = fopen("producer.log", "w");
if (logfp == NULL) {
perror("[PARENT] Logfile");
teardown("[PARENT]");
return;
}
/* Initialize the sigsuspend sigset */
error = sigfillset(&sigsp);
if (systemCallFailed("[PARENT]", "sigfillset failed", error)) {
fprintf(logfp, "sigfillset failed.\n");
return;
}
error = sigdelset(&sigsp, SIGINT);
if (systemCallFailed("[PARENT]", "sigdelset failed(SIGINT)", error)) {
fprintf(logfp, "sigdelset failed(SIGINT).\n");
return;
}
error = sigdelset(&sigsp, SIGUSR1);
if (systemCallFailed("[PARENT]", "sigdelset failed(SIGUSR1)", error)) {
fprintf(logfp, "sigdelset failed(SIGUSR1).\n");
return;
}
error = sigdelset(&sigsp, SIGUSR2);
if (systemCallFailed("[PARENT]", "sigdelset failed(SIGUSR2)", error)) {
fprintf(logfp, "sigdelset failed(SIGUSR2).\n");
return;
}
/* Initialize the sigprocmask sigset */
error = sigemptyset(&sigbl);
if (error == -1) {
fprintf(logfp, "[PARENT] sigemptyset failed");
return;
}
error = sigaddset(&sigbl, SIGINT);
if (error == -1) {
fprintf(logfp, "[PARENT] sigaddset failed(SIGINT)");
return;
}
error = sigaddset(&sigbl, SIGUSR2);
if (error == -1) {
fprintf(logfp, "[PARENT] sigaddset failed(SIGUSR2)");
return;
}
memset(&lock, 0, sizeof(lock));
int f = true;
/* Infinite Loop */
while (loop) {
/* Block the SIGINT & SIGUSR2 while in critical section. */
DERROR("[PARENT] Blocking signals.\n");
fprintf(logfp, "Blocking signals.\n");
error = sigprocmask(SIG_BLOCK, &sigbl, NULL);
if (systemCallFailed("[PARENT]", "SIG_BLOCK failed", error)) {
fprintf(logfp, "SIG_BLOCK failed.\n");
return;
}
DERROR("[PARENT] Waiting for lock.\n");
fprintf(logfp, "Waiting for lock.\n");
/* Lock file so only producer can access. */
lock.l_type = F_WRLCK;
error = fcntl(fd, F_SETLKW, &lock);
if (systemCallFailed("[PARENT]", "F_SETLCKW failed", error)) {
fprintf(logfp, "F_SETLCKW failed.\n");
return;
}
DERROR("[PARENT] Lock acquired.\n");
fprintf(logfp, "Lock acquired.\n");
/* Seek to end of the file to find file size */
curr = lseek(fd, 0, SEEK_END);
if (systemCallFailed("[PARENT]", "Seek failed", curr)) {
fprintf(logfp, "Seek failed.\n");
return;
}
/* Stop if file is filled. */
if (curr == numc * linec * sizeof(number)) {
DERROR("[PARENT] File is filled.\n");
fprintf(logfp, "File is filled.\n");
f = false;
DERROR("[PARENT] Releasing lock.\n");
fprintf(logfp, "Releasing lock.\n");
/* Breifly unlock the file so consumer can read. */
lock.l_type = F_UNLCK;
error = fcntl(fd, F_SETLKW, &lock);
if (systemCallFailed("[PARENT]", "F_SETLCKW failed", error)) {
fprintf(logfp, "F_SETLCKW failed.\n");
return;
}
DERROR("[PARENT] Lock released.\n");
fprintf(logfp, "Lock released.\n");
DERROR("[PARENT] Suspending execution till: SIGINT | SIGUSR1 | SIGUSR2\n");
fprintf(logfp, "Suspending execution till: SIGINT | SIGUSR1 | SIGUSR2\n");
error = sigsuspend(&sigsp);
if (systemCallFailed("[PARENT]", "sigsuspend failed", (error != -1 ? -1 : 0))) {
fprintf(logfp, "sigsuspend failed.\n");
return;
}
DERROR("[PARENT] Signal: %s\n",
(reason == R_SIGINT ? "SIGINT" : (reason == R_SIGUSR1 ? "SIGUSR1" : "SIGUSR2")));
fprintf(logfp, "Signal: %s\n",
(reason == R_SIGINT ? "SIGINT" : (reason == R_SIGUSR1 ? "SIGUSR1" : "SIGUSR2")));
} else {
fprintf(logfp, "I'm producing a random sequence for line %3lu:", curr / numc / sizeof(number) + 1);
fprintf(stdout, "I'm producing a random sequence for line %3lu:", curr / numc / sizeof(number) + 1);
for (int i = 0; i < numc; ++i) {
number = ((double) (rand() % 100 + 1)) + ((double) (rand() % 100)) / 100;
DERROR("[PARENT] Number generated: %6.2f\n", number);
fprintf(logfp, " %6.2f", number);
fprintf(stdout, " %6.2f", number);
error = writeAll(fd, &number, sizeof(number));
if (error) {
perror("[PARENT] write failed");
return;
}
}
fprintf(logfp, "\n");
fprintf(stdout, "\n");
fflush(stdout);
kill(child, SIGUSR1);
}
if (f) {
DERROR("[PARENT] Releasing lock.\n");
fprintf(logfp, "Releasing lock.\n");
/* Breifly unlock the file so consumer can read. */
lock.l_type = F_UNLCK;
error = fcntl(fd, F_SETLKW, &lock);
if (systemCallFailed("[PARENT]", "F_SETLCKW failed", error)) {
fprintf(logfp, "F_SETLCKW failed.\n");
return;
}
DERROR("[PARENT] Lock released.\n");
fprintf(logfp, "Lock released.\n");
}
f = true;
/* Unblock signals so we can see if something important happened. */
DERROR("[PARENT] Unblocking signals.\n");
fprintf(logfp, "Unblocking signals.\n");
error = sigprocmask(SIG_UNBLOCK, &sigbl, NULL);
if (systemCallFailed("[PARENT]", "SIG_UNBLOCK failed", error)) {
fprintf(logfp, "SIG_UNBLOCK failed.\n");
return;
}
}
}
void consumer(int const numc, int const fd, pid_t const parent) {
struct flock lock;
off_t curr = 0;
int error;
sigset_t sigsp;
sigset_t sigbl;
double *numbers __attribute__((__cleanup__(cleanDouble))) = malloc(sizeof(*numbers) * numc);
double real, imag;
FILE *logfp __attribute__((__cleanup__(cleanFP))) = fopen("consumer.log", "w");
if (logfp == NULL) {
perror("[OPEN]");
teardown("[CHILD]");
return;
}
/* Initialize the sigsuspend sigset */
error = sigfillset(&sigsp);
if (systemCallFailed("[CHILD]", "sigfillset failed", error)) {
fprintf(logfp, "sigfillset failed.\n");
return;
}
error = sigdelset(&sigsp, SIGINT);
if (systemCallFailed("[CHILD]", "sigdelset failed(SIGINT)", error)) {
fprintf(logfp, "sigdelset failed(SIGINT)\n");
return;
}
error = sigdelset(&sigsp, SIGUSR1);
if (systemCallFailed("[CHILD]", "sigdelset failed(SIGUSR1)", error)) {
fprintf(logfp, "sigdelset failed(SIGUSR1)\n");
return;
}
error = sigdelset(&sigsp, SIGUSR2);
if (systemCallFailed("[CHILD]", "sigdelset failed(SIGUSR2)", error)) {
fprintf(logfp, "sigdelset failed(SIGUSR2)\n");
return;
}
/* Initialize the sigprocmask sigset */
error = sigemptyset(&sigbl);
if (error == -1) {
fprintf(logfp, "[CHILD] sigemptyset failed");
return;
}
error = sigaddset(&sigbl, SIGINT);
if (error == -1) {
fprintf(logfp, "[CHILD] sigaddset failed(SIGINT)");
return;
}
error = sigaddset(&sigbl, SIGUSR2);
if (error == -1) {
fprintf(logfp, "[CHILD] sigaddset failed(SIGUSR2)");
return;
}
memset(&lock, 0, sizeof(lock));
int f = true;
/* Infinite Loop */
while (loop) {
/* Block the SIGINT & SIGUSR2 while in critical section. */
DERROR("[CHILD] Blocking signals.\n");
fprintf(logfp, "Blocking signals.\n");
error = sigprocmask(SIG_BLOCK, &sigbl, NULL);
if (systemCallFailed("[CHILD]", "SIG_BLOCK failed", error)) {
fprintf(logfp, "SIG_BLOCK failed.\n");
return;
}
DERROR("[CHILD] Waiting for lock.\n");
fprintf(logfp, "Waiting for lock.\n");
/* Lock file so only consumer can access. */
lock.l_type = F_WRLCK;
error = fcntl(fd, F_SETLKW, &lock);
if (systemCallFailed("[CHILD]", "F_SETLCKW failed", error)) {
fprintf(logfp, "F_SETLCKW failed.\n");
return;
}
DERROR("[CHILD] Lock acquired.\n");
fprintf(logfp, "Lock acquired.\n");
/* Seek to end of the file to find file size */
curr = lseek(fd, 0, SEEK_END);
if (systemCallFailed("[CHILD]", "Seek failed", curr)) {
fprintf(logfp, "Seek failed.\n");
return;
}
/* Stop if file is filled. */
if (curr == 0) {
DERROR("[CHILD] File is empty.\n");
fprintf(logfp, "File is empty.\n");
f = false;
DERROR("[CHILD] Releasing lock.\n");
fprintf(logfp, "Releasing lock.\n");
/* Breifly unlock the file so producer can write. */
lock.l_type = F_UNLCK;
error = fcntl(fd, F_SETLKW, &lock);
if (systemCallFailed("[CHILD]", "F_SETLCKW failed", error)) {
fprintf(logfp, "F_SETLCKW failed.\n");
return;
}
DERROR("[CHILD] Lock released.\n");
fprintf(logfp, "Lock released.\n");
error = sigsuspend(&sigsp);
if (systemCallFailed("[CHILD]", "sigsuspend failed", (error != -1 ? -1 : 0))) {
fprintf(logfp, "sigsuspend failed.\n");
return;
}
DERROR("[CHILD] Signal: %s\n",
(reason == R_SIGINT ? "SIGINT" : (reason == R_SIGUSR1 ? "SIGUSR1" : "SIGUSR2")));
fprintf(logfp, "Signal: %s\n",
(reason == R_SIGINT ? "SIGINT" : (reason == R_SIGUSR1 ? "SIGUSR1" : "SIGUSR2")));
} else {
/* Seek to end of the file to find file size */
curr = lseek(fd, -numc * sizeof(*numbers), SEEK_END);
if (systemCallFailed("[CHILD]", "Seek failed", curr)) {
fprintf(logfp, "Seek failed.\n");
return;
}
fprintf(logfp, "The dft of line %3lu:\n", curr / numc / sizeof(*numbers) + 1);
fprintf(stdout, "The dft of line %3lu:\n", curr / numc / sizeof(*numbers) + 1);
error = readAll(fd, numbers, sizeof(*numbers) * numc);
if (error) {
perror("[CHILD] readall failed");
teardown("[CHILD]");
fprintf(logfp, "Readall failed.\n");
return;
}
for (int i = 0; i < numc; ++i) {
dft(numbers, numc, i, &real, &imag);
DERROR("[CHILD] DFT(%6.2f) = %6.2f + i * %6.2f\n", numbers[i], real, imag);
fprintf(logfp, " DFT(%6.2f) = %6.2f + i * %6.2f\n", numbers[i], real, imag);
fprintf(stdout, " DFT(%6.2f) = %6.2f + i * %6.2f\n", numbers[i], real, imag);
}
fflush(stdout);
ftruncate(fd, curr);
kill(parent, SIGUSR1);
}
if (f) {
DERROR("[CHILD] Releasing lock.\n");
fprintf(logfp, "Releasing lock.\n");
/* Breifly unlock the file so consumer can read. */
lock.l_type = F_UNLCK;
error = fcntl(fd, F_SETLKW, &lock);
if (systemCallFailed("[CHILD]", "F_SETLCKW failed", error)) {
fprintf(logfp, "F_SETLCKW failed.\n");
return;
}
DERROR("[CHILD] Lock released.\n");
fprintf(logfp, "Lock released.\n");
}
f = true;
/* Unblock signals so we can see if something important happened. */
DERROR("[CHILD] Unblocking signals.\n");
fprintf(logfp, "Unblocking signals.\n");
error = sigprocmask(SIG_UNBLOCK, &sigbl, NULL);
if (systemCallFailed("[CHILD]", "SIG_UNBLOCK failed", error)) {
fprintf(logfp, "SIG_UNBLOCK failed.\n");
return;
}
}
}
void signalHandler(int const sig) {
switch (sig) {
case SIGINT:
DERROR("[%d] SIGINT\n", getpid());
loop = false;
reason = R_SIGINT;
break;
case SIGUSR1:
/* Just knowing it came is ok. */
DERROR("[%d] SIGUSR1\n", getpid());
if (!(reason < R_SIGUSR1))
reason = R_SIGUSR1;
break;
case SIGUSR2:
DERROR("[%d] SIGUSR2\n", getpid());
loop = false;
if (!(reason < R_SIGUSR2))
reason = R_SIGUSR2;
break;
}
}
void teardown(char *const source) {
/* Ignore SIGUSR2 and use it to kill child processes. */
struct sigaction dfl;
dfl.sa_handler = SIG_IGN;
dfl.sa_flags = SA_RESTART;
int error = sigemptyset(&dfl.sa_mask);
if (error == -1) {
DERROR("%s sigemptyset failed.\n", source);
perror("sigemptyset failed");
kill(0, SIGINT);
return;
}
error = sigaction(SIGUSR2, &dfl, NULL);
if (error == -1) {
DERROR("%s sigaction failed.\n", source);
perror("sigaction failed");
kill(0, SIGINT);
return;
}
DERROR("[%d] Generating SIGUSR2.\n", getpid());
kill(0, SIGUSR2); /* Tell the child processes to exit. */
}
bool systemCallFailed(char *const source, char *const msg, int error) {
/* Handle if the lseek fails. */
if (error == -1) {
DERROR("%s %s.\n", source, msg);
perror(msg);
teardown(source);
return 1;
}
return 0;
}
bool readAll(int fd, void *buffer, size_t size) {
size_t total = 0;
int n;
do {
n = read(fd, (uint8_t *) buffer + total, size - total);
if (n <= 0) {
return true;
}
total += n;
} while (total != size);
return false;
}
bool writeAll(int fd, void *buffer, size_t size) {
size_t total = 0;
int n;
do {
n = write(fd, (uint8_t *) buffer + total, size - total);
if (n <= 0) {
return true;
}
total += n;
} while (total != size);
return false;
}
void dft(double *numbers, size_t N, size_t k, double *real, double *imag) {
*real = 0;
*imag = 0;
for (int n = 0; n < N; ++n) {
*real += numbers[n] * cos(k * n * 2 * M_PI / N);
*imag -= numbers[n] * sin(k * n * 2 * M_PI / N);
}
}
void cleanFP(FILE **fpp){
fclose(*fpp);
*fpp = NULL;
}
void cleanDouble(double **dpp){
free(*dpp);
*dpp = NULL;
}