该程序在主函数中等待SIGCHLD,直到信号处理函数对某个全局变量进行修改后,主函数捕捉该变化,再继续运行。
该函数使用mask代替当前的信号屏蔽字,将程序挂起,直到捕获到信号,其行为要么运行一个handler,要么终止程序。
Hello from child
# Got signal from child!
# Done fork()
Hello from child
# Got signal from child!
# Done fork()
Hello from child
# Got signal from child!
# Done fork()
Hello from child
# Got signal from child!
# Done fork()
Hello from child
# Got signal from child!
# Done fork()
Hello from child
# Got signal from child!
# Done fork()
Hello from child
# Got signal from child!
# Done fork()
Hello from child
# Got signal from child!
# Done fork()
Hello from child
# Got signal from child!
# Done fork()
Hello from child
# Got signal from child!
# Done fork()
total count: 10
#include <sys/wait.h>
#include <sys/signal.h>
#include <errno.h>
#include <stdbool.h>
#include "apue.h"
volatile int counter = 0;
volatile bool flag = 0;
void handler(int sig)
{
sigset_t mask_all, mask_pre;
sigfillset(&mask_all);
sigprocmask(SIG_BLOCK, &mask_all, &mask_pre);
char msg[] = "# Got signal from child!\n";
write(STDOUT_FILENO, msg, strlen(msg));
++counter;
flag = 1;
sigprocmask(SIG_UNBLOCK, &mask_pre, NULL);
}
int main()
{
if (signal(SIGCHLD, handler) == SIG_ERR) {
printf("signal error\n");
}
sigset_t mask_one, mask_pre;
sigemptyset(&mask_one);
sigaddset(&mask_one, SIGCHLD);
signal(SIGCHLD, handler);
for (int i = 0; i < 10; ++i) {
sigprocmask(SIG_BLOCK, &mask_one, &mask_pre);
if (fork() == 0) {
// child
char msg[] = "Hello from child\n";
write(STDOUT_FILENO, msg, strlen(msg));
exit(0);
}
while (!flag) {
sigsuspend(&mask_pre);
}
flag = 0;
char msg[] = "# Done fork()\n";
write(STDOUT_FILENO, msg, strlen(msg));
sigprocmask(SIG_SETMASK, &mask_pre, NULL);
}
printf("total count: %d\n", counter);
exit(0);
}