diffworld
V2EX  ›  问与答

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

  •  
  •   diffworld · Feb 28, 2017 · 2013 views
    This topic created in 3364 days ago, the information mentioned may be changed or developed.

    我按照书上的代码执行,但是子进程里面的 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 没有执行,请问有人能帮我解答这个问题吗?

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1049 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 621ms · UTC 23:31 · PVG 07:31 · LAX 16:31 · JFK 19:31
    ♥ Do have faith in what you're doing.