获取子进程的退出状态,并从父进程获取并在父进程中打印。

时间:2017-04-02 07:29:54

标签: c process

我正在探索c中的流程。我想以一种方式更新以下程序,子程序以退出状态终止(例如10)。从父进程获取此退出状态并将其打印在父进程中。我尝试了以下方式,但是从父母那里获得的状态是错误的。哪里出错了?请帮帮我吧。提前致谢。

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    int pid;
    int ppid;
    int status;
    pid=vfork();
    pid_t return_pid = waitpid(pid, &status, WNOHANG);
    //printf("%d\n", pid); --> value = 0

    if (pid < 0) {
        printf("\n Error ");
        exit(1);
    } else if (pid==0) {
        printf("\n Hello I am the child process\n");
        printf("\n My pid is:  %d\n", getpid());
        printf("\n My parent pid is:  %d\n", getppid());
        printf("\n My return_pid is:  %d\n", return_pid);
        printf("_______________________________________\n");
        int es = WEXITSTATUS(status);
        printf ("\n Child exit status:  %d\n", es);
        status = es;
        exit(0);
    } else {
        printf("The child has terminated!\n");
        printf("_______________________________________\n");
        printf("\n Hello I am the parent process\n");
        printf("\n My actual pid is %d\n" , getpid());
        printf("\n My parent pid is:  %d\n", getppid());
        printf("\n My return_pid is:  %d\n", return_pid);
        printf("_______________________________________\n");
        printf ("\n Child's Exit staus from parent:  %d\n", status);
        exit(1);
    }
}

1 个答案:

答案 0 :(得分:1)

此代码使用fork()而非vfork(),与您之后的内容非常接近。你想等待孩子死去 - 不要使用“如果孩子已经死了就不要等孩子死了”的选项。最好只在父母身边等待。此代码有时也会通过中断信号杀死孩子 - 主要是因为您可以看到退出状态是如何编码的。

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

int main(void)
{
    pid_t pid = fork();

    if (pid < 0)
    {
        perror("fork()");
        exit(1);
    }
    else if (pid == 0)
    {
        printf("Hello I am the child process\n");
        printf("My pid is:  %d\n", (int)getpid());
        printf("My parent pid is:  %d\n", (int)getppid());
        int status = getpid() % 256;
        printf("I am about to exit with status 0x%.2X (%d)\n", status, status);
        printf("_______________________________________\n");
        exit(status);
    }
    else
    {
        int status;
        if (pid % 10 == 0)
            kill(pid, SIGINT);
        pid_t return_pid = waitpid(pid, &status, 0);
        printf("The child has terminated!\n");
        printf("Hello I am the parent process\n");
        printf("My actual pid is %d\n", (int)getpid());
        printf("My parent pid is:  %d\n", (int)getppid());
        printf("My return_pid is:  %d\n", (int)return_pid);
        printf("Child's raw exit status:  0x%.4X (%d)\n", status, status);
        if (WIFEXITED(status))
            printf("Child's exit status: 0x%.2X (%d)\n",
                   WEXITSTATUS(status), WEXITSTATUS(status));
        if (WIFSIGNALED(status))
            printf("Child exited because of signal %d\n", WTERMSIG(status));
        printf("_______________________________________\n");
    }
    return 0;
}

样本输出:

正常运行:

Hello I am the child process
My pid is:  40377
My parent pid is:  40376
I am about to exit with status 0xB9 (185)
_______________________________________
The child has terminated!
Hello I am the parent process
My actual pid is 40376
My parent pid is:  847
My return_pid is:  40377
Child's raw exit status:  0xB900 (47360)
Child's exit status: 0xB9 (185)
_______________________________________

信号运行:

The child has terminated!
Hello I am the parent process
My actual pid is 40379
My parent pid is:  847
My return_pid is:  40380
Child's raw exit status:  0x0002 (2)
Child exited because of signal 2
_______________________________________