次のプログラムの出力を理解できません。子プロセスが戻った後、親プロセスはwait()の前に3秒間スリープしていないことがわかりました。 SIGCHLDがデフォルトのハンドラに設定されている場合、3秒間スリープ状態になり、waitを呼び出して期待どおりに戻ります。ここで何が起こっているのですか?子プロセスが終了したときのSIGCHLDの理解
# 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
回答ありがとうございます。もう1つの疑問 - 私はシステムコールの再起動が定義された実装までであり、システムコールのいくつかは全く再起動しないという記事から読みました。本当ですか?システムコールの再起動は同じコールを2回呼び出すのと同じですか? – Goutham
@Gouthamそれは同じではありません。それを「EINTRを返さないようにしようとする信号」と考えてください。そうですね、信号の再起動はほとんどが実装依存です。 – cnicutar