我有这样的c代码
int childid;
int parentid;
void siginthandler(int param)
{
// if this is the parent process
if(getpid() == parentid){
//handler code which sends SIGINT signal to child process
kill(childid, SIGINT);
}
else // if this is the child process
exit(0);
}
int main()
{
parentid = getpid(); // store parent id in variable parentid, for the parent as well as child process
int pid = fork();
int breakpid;
if(pid != 0){
childid = pid;
breakpid = wait();
printf("pid = %d\n", breakpid);
}
else
sleep(1000);
}
现在,当我运行这段代码时,父进程等待,而子进程休眠。如果我现在中断(通过按ctrl+ c)父程序,UNIX(POSIX 标准)文档告诉我等待函数应该返回 -1,并将 errno 设置为 EINTR。但是,我的代码实现会杀死子进程,因为父进程将 SIGINT 信号发送给子进程。但是,令人惊讶的是,(在父进程中)wait 不会返回 pid = -1 并将 errno 设置为 EINTR。相反,它返回被杀死的子进程的 id。
对此有解释吗?