UNIX信号处理。在SIGCHLD处理程序中等待。 C

时间:2019-03-13 17:10:51

标签: c unix signals wait sigchld

我有一个父母和一个孩子进程。在父级中,我为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);
    }
}

1 个答案:

答案 0 :(得分:5)

您必须使用waitpid()而不是wait(),并且需要指定选项WUNTRACED来停止报告了waitpid()的孩子,如下所示:

if (waitpid(-1, &statol, WUNTRACED) < 0) {

现在waitpid()应该立即返回,并且您的宏WIFSTOPPED(&statol)应该为true。

相关问题