次のプログラムの出力を理解できません。子プロセスが戻った後、wait() の前に親プロセスが 3 秒間スリープしていないことがわかりました。SIGCHLD がデフォルト ハンドラーに設定されている場合、3 秒間スリープし、wait を呼び出して期待どおりに戻ります。ここで正確に何が起こっているのですか??
# 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