繁体   English   中英

wait()函数(在LINUX中)何时响应中断?

[英]When does the wait() function (in LINUX) respond to interrupts?

我有这样的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标准)文档告诉我wait函数应返回-1,并将errno设置为EINTR。 但是,我的代码实现会杀死子进程,因为父进程将SIGINT信号发送到子进程。 但是,令人惊讶的是,(在父进程中)等待不会返回pid = -1并且errno设置为EINTR。 相反,它返回被杀死的子进程的id。

对此有解释吗?

使用signal()安装信号处理程序时,会发生以下两种情况之一

  1. 发生信号时,大多数系统调用都会中断,并将errno设置为EINTR。
  2. 当信号发生时,大多数系统调用会自动重启(因此它们不会失败并将errno设置为EINTR)。

这两个中的哪一个取决于您的平台,在您的平台上它是2 (可能,您没有显示任何安装信号处理程序的代码。)

如果您使用sigaction ()安装信号处理程序,则可以使用SA_RESTART标志控制所需的12行为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM