一、多进程中的信号同步机制:
基于signal interrupt的例子,用于防止race condition
先看以下例子:
#include "csapp.h"
void handler(int sig)
{
int olderrno = errno;
sigset_t mask_all, prev_all;
pid_t pid;
Sigfillset(&mask_all);
while((pid = waitpid(-1, NULL, 0)) > 0) { /* Reap a zombie child */
Sigprocmask(SIG_BLOCK, &mask_all, &prev_all);
deletejob(pid); /* Delete the child from the job list */
Sigprocmask(SIG_SETMASK, &prev_all, NULL);
}
if(errno != ECHILD)
Sio_error("waitpid error");
errno = olderrno;
}
int main(int argc, char ** argv)
{
int pid;
sigset_t mask_all, prev_all;
Sigfillset(&mask_all);
Signal(SIGCHLD, handler);
initjobs(); /* Initialize the job list */
while(1) {
if((pid == Fork()) == 0) {