[英]When does the wait() function (in LINUX) respond to interrupts?
I have c code as such 我有这样的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);
}
Now, when I run this code, the parent process waits, while the child process sleeps. 现在,当我运行此代码时,父进程会等待,而子进程会休眠。 If I now interrupt (by pressing ctrl + c ) the parent program, UNIX (POSIX standard) documentation tells me that the wait function should return -1, with errno set to EINTR. 如果我现在中断(通过按ctrl + c )父程序,UNIX(POSIX标准)文档告诉我wait函数应返回-1,并将errno设置为EINTR。 My code implementation however, kills the child process since the parent process sends the SIGINT signal to the child process. 但是,我的代码实现会杀死子进程,因为父进程将SIGINT信号发送到子进程。 But, surprisingly,(in the parent process) wait does not return pid = -1 with errno set to EINTR. 但是,令人惊讶的是,(在父进程中)等待不会返回pid = -1并且errno设置为EINTR。 Instead, it returns the id of the child process, which got killed. 相反,它返回被杀死的子进程的id。
Is there an explanation for this? 对此有解释吗?
When you install a signal handler with signal()
, one of 2 things occur 使用signal()
安装信号处理程序时,会发生以下两种情况之一
Which of these 2 depends on your platform, on yours it's 2
(probably, you don't show any code that installs a signal handler.) 这两个中的哪一个取决于您的平台,在您的平台上它是2
(可能,您没有显示任何安装信号处理程序的代码。)
If you instead install a signal handler with sigaction (), you can control which behavior of 1
or 2
you want with the SA_RESTART
flag. 如果您使用sigaction ()安装信号处理程序,则可以使用SA_RESTART
标志控制所需的1
或2
行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.