18,772
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
void p_action(int signo){
printf("parent process %d recieve SIGUSR2 signal,signo is %d\n",getpid(),signo);
}
void c_action(int signo){
printf("child process %d recieve SIGUSR1 signal,signo is %d\n",getpid(),signo);
}
int main(int argc, char *argv[])
{
int pid,ppid;
signal(SIGCHLD,SIG_IGN);
switch(pid=fork()){
case -1:
printf("create child process error!\n");
break;
case 0:
signal(SIGUSR1,c_action);
ppid=getppid();
sleep(5);
printf("child process %d send signal %d to process %d...\n",getpid(),SIGUSR2,ppid);
kill(ppid,SIGUSR2);
break;
default:
signal(SIGUSR2,p_action);
sleep(2);
printf("parent process %d send signal %d to process %d...\n",getpid(),SIGUSR1,pid);
kill(pid,SIGUSR1);
pause();
break;
}
return 0;
}
POSIX.1-2001 specifies that if the disposition of SIGCHLD is set to SIG_IGN or the SA_NOCLDWAIT flag is set for SIGCHLD (see sigaction(2)), then
children that terminate do not become zombies and a call to wait() or waitpid() will block until all children have terminated, and then fail with
errno set to ECHILD. (The original POSIX standard left the behavior of setting SIGCHLD to SIG_IGN unspecified. Note that even though the default
disposition of SIGCHLD is "ignore", explicitly setting the disposition to SIG_IGN results in different treatment of zombie process children.)
Linux 2.6 conforms to this specification. However, Linux 2.4 (and earlier) does not: if a wait() or waitpid() call is made while SIGCHLD is being
ignored, the call behaves just as though SIGCHLD were not being ignored, that is, the call blocks until the next child terminates and then returns
the process ID and status of that child.