我有一个父进程和一个子进程。在父级中,我为 SIGCHLD 建立了一个信号处理程序。我向孩子发送 SIGTSTP 信号,触发 SIGCHLD 并在父母的 SIGCHLD siganl 处理程序中调用等待函数来获取停止的孩子的状态。但不是立即返回,而是阻塞。然后我向孩子发送一个 SIGCONT 信号并等待返回,并将 errno 设置为 Interuppted 系统调用。我无法理解我错过了什么。
pid_t pid;
static void sig_chld(int signo);
int main() {
struct sigaction act, savechld;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = sig_chld;
if (sigaction(SIGCHLD, &act, &savechld) < 0){
return errno;
}
pid = fork();
switch (pid){
case -1:{
perror("fork failed");
return errno;
}
case 0:{ //child
if (sigaction(SIGCHLD, &savechld, NULL) < 0)
return errno;
execlp(path, name_of_executable, (char*)NULL);
return errno;
}
default:{
for (;;)
pause();
}
}
return 0;
}
void sig_chld(int signo) {
int statol;
if (wait(&statol) < 0){
perror("waitt error");
exit(errno);
}
if (WIFSTOPPED(statol)){
puts("Child is stopped");
} else if (WIFEXITED(statol)){
puts("Child exited");
} else if (WIFCONTINUED(statol)){
puts("Child continued");
} else if (WIFSIGNALED(statol)){
puts("Child is signaled");
int sig = WTERMSIG(statol);
psignal(sig, NULL);
}
}