如何离开(中断)waitpid() 函数? [英] How to leave (interrupt) the waitpid() function?

查看:32
本文介绍了如何离开(中断)waitpid() 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在编写一个 shell,我为我的子进程使用了​​ waitpid() 函数.

Currently I am programming a shell and I use the waitpid() function for my children processes.

我还安装了一个信号处理程序,因此我可以捕获 SIGINT (CTRL+C) 信号.

I also installed a signal handler so I can trap the SIGINT (CTRL+C) signal.

所以我现在想要的是当有人按下 SIGINT (CTRL+C) 信号时,它应该离开 waitpid() 函数和像往常一样继续.

So what I want now is when someone presses the SIGINT (CTRL+C) signal it should leave the waitpid() functions and keep going as usually.

我正在寻找可以帮助我解决这个问题的函数.

I am looking for a function who can help me about that.

推荐答案

waitpid() 将在接收到信号时返回并将 errno 设置为 EINTR.

为此,信号处理程序我没有安装选项SA_RESTART.

For this to work the signal handler my not have been installed with the option SA_RESTART.

要在不设置选项 SA_RESTART 的情况下安装信号句柄,首选函数是 sigaction().

To install a signal handle without setting the option SA_RESTART the prefered function is sigaction().

void handle(int sig)
{
  /* Do nothing. */
}

int main(void)
{
  sigaction(SIGINT, (struct sigaction){handler}, NULL);

  /* fork and bla ... */

  if (-1 == waitpid(..., /*no WNOHANG here */))
  {
    if (EINTR == errno)
    {
      /* process received signal */
      ...

这篇关于如何离开(中断)waitpid() 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆