小编Иго*_*нко的帖子

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

我有一个父母和一个孩子的过程。在父级中,我为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 …
Run Code Online (Sandbox Code Playgroud)

c unix signals wait sigchld

2
推荐指数
1
解决办法
229
查看次数

标签 统计

c ×1

sigchld ×1

signals ×1

unix ×1

wait ×1