SIGTSTP信号处理程序根据条件忽略/作用于SIGTSTP

时间:2014-10-13 02:36:06

标签: c unix signals

就像问题所说的那样,我想根据某些条件不同地处理SIGTSTP信号。我的最终代码不会那么简单,但我试图让它首先工作:

static void stphandler(int signo) {

  sigset_t mask;

  //Unblock SIGTSTP

  sigemptyset( & mask);
  sigaddset( & mask, SIGTSTP);
  sigprocmask(SIG_UNBLOCK, & mask, NULL);

  if (getpid() != ultimateParent) {
    printf("Not the parent process\n");
    exit(0);
  } 
  
  else {
    printf("This is the parent process");
  }

}

父进程初始化的代码:

ultimateParent=getpid();


sa_stp.sa_handler = stphandler;
sigemptyset(&sa_stp.sa_mask);
sa_stp.sa_flags = SA_RESTART;
sigaction(SIGTSTP, &sa_stp, NULL);

创建子代码:

//leave args blank. Keep wc waiting for an input
execErr=execvp("wc",args);

if(errno==EACCES)
{
    fprintf(stderr,"%s: Permission Denied.\n",command);

}

else
{

    fprintf(stderr,"%s: Command not found.\n",command);
}

exit(0);

这是我做的以及我得到的输出:

在家长中:

当父进程运行并按CTRL + Z时,输出为: ^ Z

当我按下返回键时,printf命令在下一行执行:

“这是父流程”

问题:为什么它等待我按回车键继续?

在孩子中

它只是在生成信号时打印^ Z次数,使用返回键转到下一行 - 但忽略ctrl + D(这通常会导致wc读取EOF并完成执行)或任何其他信号那个问题,除了SIGQUIT。

问题:为什么在ctrl + z之后忽略了更多信号?有没有办法简单地让孩子退出?

我觉得我遗漏了一些关于信号和信号处理程序的重要信息。任何帮助,文章或其他参考资料都表示赞赏。

0 个答案:

没有答案
相关问题