2

I have tow handlers for each one of them (SIGTSTP, SIGCHLD), the thing is that when I pause a process using SIGTSTP the handler function of SIGCHLD run too. what should I do to prevent this .

signal handlers :

void signalHandler(int signal) {
int pid, cstatus;
if (signal == SIGCHLD) {
    susp = 0;
    pid = waitpid(-1, &cstatus, WNOHANG);
    printf("[[child %d terminated]]\n", pid);
    DelPID(&JobsList, pid);
}
}

void ctrlZsignal(int signal){
    kill(Susp_Bg_Pid, SIGTSTP);
    susp = 0;
    printf("\nchild %d suspended\n", Susp_Bg_Pid);
}

Susp_Bg_Pid used to save the paused process id.
susp indicates the state of the "smash" the parent process if it is suspended or not .

4

1 回答 1

4

sigaction使用with设置您的 SIGCHLD 处理程序SA_NOCLDSTOP

来自 sigaction (2)

SA_NOCLDSTOP - 如果 signum 是 SIGCHLD,当子进程停止(即,当它们接收到 SIGSTOP、SIGTSTP、SIGTTIN 或 SIGTTOU 之一)或恢复(即,它们接收到 SIGCONT)(参见 wait(2))时,不会收到通知。此标志仅在为 SIGCHLD 建立处理程序时才有意义。

更新

void signalHandler(int sig)
{
  //...
}

struct sigaction act;

act.sa_handler = signalHandler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_NOCLDSTOP;

if (sigaction(SIGCHLD, &act, 0) == -1)
{
  perror("sigaction");
  exit(1);
}

如果你不熟悉,sigaction你应该阅读它,因为它有几个不同的选项和行为,这些选项和行为远远优于signal但在你弄清楚如何使用它之前会以复杂性和混乱为代价。我对您似乎想要做的事情做出了最好的猜测,但您需要尽早学习这一点。

于 2013-11-09T23:07:03.170 回答