简体   繁体   中英

Is child process pid() gets assigned to parent process?

I have read this in books and also in some online forums that child process pid is assigned to its parent. But I have this code:

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
    pid_t pid;
    pid=fork();
    if(pid==0)
        {
            printf("In Child Process\n");
            printf("Child process PID : %d\n",getpid());
            printf("Parent Process PID : %d\n",getppid());
        }
    else
        {
            printf("In Parent Process\n");
            printf("Child PID : %d\n",getpid());
            printf("Parent PID : %d\n",getppid());
        }
}

It outputs:

In Parent Process
Child PID : 2061
Parent PID : 1830
In Child Process
Child process PID : 2062
Parent Process PID : 1161

But if I write a wait() function in else block, ie:

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
    pid_t pid;
    pid=fork();
    if(pid==0)
        {
            printf("In Child Process\n");
            printf("Child process PID : %d\n",getpid());
            printf("Parent Process PID : %d\n",getppid());
        }
    else
        {
            wait();
            printf("In Parent Process\n");
            printf("Child PID : %d\n",getpid());
            printf("Parent PID : %d\n",getppid());
        }
}

It outputs-

In Child Process
Child process PID : 2044
Parent Process PID : 2043
In Parent Process
Child PID : 2043
Parent PID : 1830

I'm not getting why the pid value returned by child process in first code is not the same as parent pid. While in the second code, it is the same. Can someone please explain the reason for the above problem?

Remember that getpid returns the pid of the current process, and that getppid returns the parent pid of the current process.

So in the second example, when you call getpid in the parent process you get the pid of if itself (the parent process) and getppid gets the pid of the grand -parent.

The child pid is the value returned by fork .


More related to your problem is that you have no control over when a specific process runs in a modern multi-tasking system, which means that the child and parent processes may take turn printing out text. In your case it seems that the child process in the first example doesn't get to run until the parent process has printed its lines.

What the wait function does, is to actually wait until one child process has exited, and so the parent process will block until the child process has exited.

Joachim's answer is excellent. As additional note, you can get the child process' pid in the else branch by printing the result of fork :

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
    pid_t pid;
    pid=fork();
    if(pid==0)
        {
            printf("In Child Process\n");
            printf("Child process PID : %d\n",getpid());
            printf("Parent Process PID : %d\n",getppid());
        }
    else
        {
            wait();
            printf("In Parent Process\n");
            printf("Child PID : %d\n", pid); // fork returns the pid of the child
            printf("Parent PID : %d\n", getpid());  // because I'm the parent
        }
}

First Example: fork returns two processes, In your first example after fork the parent starts execution and prints its pid 2061 and parent pid 1830 (of bash) and the parent will terminate without waiting for the child to finish, So when child starts executing it will print its pid 2062 and its parent has already terminated it is printing some other pid.

You please sleep() before printing parent pid when child process is running and on other terminal use command ps -l to see which pid the child is printing as a parent pid.

Second example:

You are using wait in parent process that is parent will wait untill child terminates thus child process is printing the correct parentpid because parent is still not terminated.

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