简体   繁体   中英

c fork's child ppid does not match parent's pid

I'm totally new to C. I tried the following code, expecting that the child's ppid would match the parent's pid, but this is not the case.

int main() {


    int pid;

    printf("I'm process with pid=%d\n", getpid());

    switch (pid = fork()) {
        case -1:
            perror("fork");
            exit(1);
        case 0:
            printf("I'm the child process: pid=%d, ppid=%d\n", getpid(), getppid());
            break;
        default:
            printf("I'm the parent process: pid=%d, ppid=%d\n", getpid(), getppid());
            break;
    }

    exit(0);

}
> gcc -o fork fork.c 
> ./fork 
I'm process with pid=16907
I'm the parent process: pid=16907, ppid=6604
I'm the child process: pid=16908, ppid=1 // <-- expected ppid=16907, why 1?
>

What did I do wrong ?

It is likely the parent process has already exited and no longer exists. You could try some delay in the parent.

'init' which is the root process running in a linux system has pid 1 .

When a process's parent gets terminated before itself(ie the child) , the child becomes an 'orphan' process and is taken up by the root process or the process just above the hierarchy of the process which created it(parent process) .

Hence , here it is taken up by and executed under init which has pid = 1 . So, delay your parent process for solution.

Like others have mentioned, looks like the parent process has terminated while the child process is still under execution making it(the child process) an orphan . Adding delay before exiting may work.

But an elegant way of doing it is, the parent process has to wait till the child process terminates.

This can be achieved by calling waitpid() with the pid of the child (the value returned by fork() ). When the control comes out of this function, you can be sure that the child process has terminated. Also, waitpid() returns the status of the process termination. Based on the status that it returns, you can get to know the normal/abnormal child termination.

Here is the code that does it:

int main() {


    int pid;
    int status = 0;

    printf("I'm process with pid=%d\n", getpid());

    switch (pid = fork()) {
        case -1:
            perror("fork");
            exit(1);
        case 0:
            printf("I'm the child process: pid=%d, ppid=%d\n", getpid(), getppid());
            break;
        default:
            waitpid(pid, &status, 0);
            if(WIFEXITED(status)
            {
                printf("Normal termination of child process %d\n", pid);
            }
            else
            {
                printf("Abormal termination of child process %d\n", pid);
            }
            printf("I'm the parent process: pid=%d, ppid=%d\n", getpid(), getppid());
            break;
    }

    exit(0);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM