I can not understand the output for the next program. I noticed that after the child process returns, the parent process will not sleep for 3 seconds before waiting (). If SIGCHLD is set to the default handler, then it sleeps for 3 seconds, causing a wait and return, as expected. What's going on here?
# include <unistd.h> # include <sys/types.h> # include <stdio.h> # include <sys/wait.h> # include <signal.h> void handler(int sig) { printf("Iam in handler ...\n"); } main() { int status; pid_t pid; struct sigaction act; //act.sa_flags=SA_NOCLDSTOP; act.sa_handler=handler; sigaction(SIGCHLD,&act,NULL); if(!fork()) { printf("child process id is %d\n",getpid()); return 1; } printf("xxx ...\n"); sleep(3); pid = wait(&status); printf("process terminated is %d\n",pid); } output:: xxx ... child process id is 2445 Iam in handler ... process terminated is 2445
source share