V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
diffworld
V2EX  ›  问与答

请教一个 Linux c kill 和 raise 信号的问题

  •  
  •   diffworld · 2017-02-28 10:09:01 +08:00 · 1363 次点击
    这是一个创建于 2607 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我按照书上的代码执行,但是子进程里面的 printf 没有被打印,请问这是为什么 代码如下:

    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    
    int main(int argc, char *argv[])
    {
        pid_t pid;
        int res;
    
        pid = fork();
        if (pid < 0)
        {
            perror ("Fork error\n");
            exit(0);
        }
        
        if (pid == 0)
        {
            printf ("child(pid:%d) is waiting for signal\n", getpid());
            raise(SIGSTOP);
            exit(0);
        }
    
        else
        {
            if ((waitpid(pid, NULL, WNOHANG)) == 0)
            {
                if ((res = kill(pid, SIGKILL)) == 0)
                {
                   printf ("parent kill %d\n", pid); 
                }
            }
    
            waitpid(pid, NULL, 0);
            exit(0);
            
        }
    }
    

    输出结果如下:

    parent kill 20551

    为什么子进程里面的 printf 没有执行,请问有人能帮我解答这个问题吗?

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2735 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 12:59 · PVG 20:59 · LAX 05:59 · JFK 08:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.