目前我正在编写一个 shell,我为我的子进程使用 waitpid()
函数。
我还安装了一个信号处理程序,这样我就可以捕获 SIGINT (CTRL+C) 信号。
所以我现在想要的是当有人按下 SIGINT (CTRL+C) 信号时它应该离开 waitpid()
函数并且像往常一样继续。
我正在寻找可以帮助我的功能。
最佳答案
安装您的信号处理程序不 SA_RESTART
标志:
void handler( int sig, siginfo_t *si, void *arg )
{
...
}
...
struct sigaction newact;
memset( &newact, 0, sizeof( newact ) );
sigemptyset( &newact.sa_mask );
newact.sa_flags = SA_SIGINFO; // note that **lack** of SA_RESTART
newact.sa_siginfo = handler;
sigaction( SIGINT, &newact, NULL );
您必须为其添加错误检查,并注意您现在必须在代码中处理中断的调用。
关于c - 如何离开(中断)waitpid()函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50425827/