-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathsync.c
122 lines (91 loc) · 3.47 KB
/
sync.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
/* adapted from http://man7.org/tlpi/code/online/book/procexec/fork_sig_sync.c.html */
/*************************************************************************\
* Copyright (C) Michael Kerrisk, 2018. *
* *
* This program is free software. You may use, modify, and redistribute it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation, either version 3 or (at your option) any *
* later version. This program is distributed without any warranty. See *
* the file COPYING.gpl-v3 for details. *
\*************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
//#include "curr_time.h" /* Declaration of currTime() */
//#include "tlpi_hdr.h"
#define SYNC_SIG SIGUSR1 /* Synchronization signal */
void errExit(char *s) {
exit(-1);
}
char *currTime(char *s) {
return "";
}
void printMasks(void) {
// blocked
sigset_t myBlkMask;
sigprocmask(SIG_SETMASK, NULL, &myBlkMask);
printf("blocked %08x (%ld)\n", myBlkMask, sizeof(sigset_t));
// pending
sigset_t myPndMask;
sigpending(&myPndMask);
printf("pending %08x\n", myPndMask);
}
static void /* Signal handler - does nothing but return */
handler(int sig)
{
}
int
main(int argc, char *argv[])
{
printf("main\n");
pid_t childPid;
sigset_t blockMask, origMask, emptyMask;
struct sigaction sa;
setbuf(stdout, NULL); /* Disable buffering of stdout */
sigemptyset(&blockMask);
sigaddset(&blockMask, SYNC_SIG); /* Block signal */
if (sigprocmask(SIG_BLOCK, &blockMask, &origMask) == -1)
errExit("sigprocmask");
printMasks();
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sa.sa_handler = handler;
if (sigaction(SYNC_SIG, &sa, NULL) == -1)
errExit("sigaction");
switch (childPid = fork()) {
case -1:
errExit("fork");
case 0: /* Child */
/* Child does some required action here... */
printf("[%s %ld] Child started - doing some work\n",
currTime("%T"), (long) getpid());
sleep(2); /* Simulate time spent doing some work */
/* And then signals parent that it's done */
printf("[%s %ld] Child about to signal parent\n",
currTime("%T"), (long) getpid());
if (kill(getppid(), SYNC_SIG) == -1)
errExit("kill");
/* Now child can do other things... */
_exit(EXIT_SUCCESS);
default: /* Parent */
/* Parent may do some work here, and then waits for child to
complete the required action */
printf("[%s %ld] Parent about to wait for signal\n",
currTime("%T"), (long) getpid());
sleep(3);
printMasks();
sigemptyset(&emptyMask);
if (sigsuspend(&emptyMask) == -1 && errno != EINTR)
errExit("sigsuspend");
printf("[%s %ld] Parent got signal\n", currTime("%T"), (long) getpid());
/* If required, return signal mask to its original state */
if (sigprocmask(SIG_SETMASK, &origMask, NULL) == -1)
errExit("sigprocmask");
printMasks();
/* Parent carries on to do other things... */
exit(EXIT_SUCCESS);
}
}