Process parent ID of child process is different from PID of parent

argamanza

I'm trying to work with multiple processes in Linux using fork() function in C, this is my code:

p1 = fork();

if(p1 != 0){
    p2 = fork();
}

printf("My PID is %d\n",getpid());
printf("My parent PID is %d\n",getppid());

Now let's assume the parent process ID is 100, and the two child processes (p1, p2) IDs are 101 & 102, and the init process PID will be 0 my expected output is:

My PID is 100
My parent PID is 0

My PID is 101
My parent PID is 100

My PID is 102
My parent PID is 100

Instead i see something different, the two child processes have the same PPID but the first process has a different PID from that. Here is a sample output i got:

My PID is 3383
My parent PID is 3381

My PID is 3387
My parent PID is 1508

My PID is 3386
My parent PID is 1508

My question is, shouldn't the parent PID of the two child processes be 3383? Hope someone can explain how it all works here and what am i doing (or thinking) wrong.

Mohit Jain

[Confirmed from the comments]

Your output is timing dependant. If the parent process finishes after the children process, your output will be as expected.

If parent process finishes before the children process, output may be surprising (Before parent does not exist any more, parent id would be different). Once parent process dies (ends), init or some other implementation-defined process(1508 in your case), becomes the new parent of the child(ren). Such children are called orphan process(es).

According to the exit man page from The Single UNIX Specification, Version 2:

The parent process ID of all of the existing child processes and zombie processes of the calling process shall be set to the process ID of an implementation-defined system process. That is, these processes shall be inherited by a special system process.

To avoid this, ensure that parent process is alive at the time of fetching parent pid. One way to do this is to add a wait in parent (or all) process before exiting.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to get child process from parent process

From Dev

C Programing : Parent & Child Process

From Dev

After starting process, how to get parent's PID in the child?

From Dev

Using Debugger how to get child process's PID from Parent

From Dev

Change directory of a parent process from a child process

From Dev

Child process starts after parent process

From Dev

Why is output of parent process blocked by child process?

From Dev

python kill parent process but child process left

From Dev

Does the PID of a child process become available for reuse if the parent process is still running?

From Dev

Pass data from parent process to its child process — IPC, UNIX

From Dev

Replace Parent Process with Child Process After Death

From Dev

Zombie process (kill both child and parent process)

From Dev

Kill the parent of a child pipe process

From Dev

Using Debugger how to get child process's PID from Parent

From Dev

Send messages from child process to parent

From Dev

Child shell process bidirectional redirection to parent process

From Dev

Is the PID of a child process always greater than the PID of its parent on Linux?

From Dev

The file used by parent and child process

From Dev

send signal from parent process to child in C

From Dev

Accessing memory of parent process from child process

From Dev

Replace Parent Process with Child Process After Death

From Dev

Webcam stream from parent process to child process

From Dev

Get child process operation result from parent process

From Dev

How to know if a process is a parent or a child

From Dev

Execl() in a child process and wait() in the parent process

From Dev

Is it possible to check the process id of child in different pid namespace?

From Dev

Piping from parent process to child and back

From Dev

Child process inherits a handle from its parent process but fails to write in it

From Dev

Piping from parent parent to child process not working

Related Related

  1. 1

    How to get child process from parent process

  2. 2

    C Programing : Parent & Child Process

  3. 3

    After starting process, how to get parent's PID in the child?

  4. 4

    Using Debugger how to get child process's PID from Parent

  5. 5

    Change directory of a parent process from a child process

  6. 6

    Child process starts after parent process

  7. 7

    Why is output of parent process blocked by child process?

  8. 8

    python kill parent process but child process left

  9. 9

    Does the PID of a child process become available for reuse if the parent process is still running?

  10. 10

    Pass data from parent process to its child process — IPC, UNIX

  11. 11

    Replace Parent Process with Child Process After Death

  12. 12

    Zombie process (kill both child and parent process)

  13. 13

    Kill the parent of a child pipe process

  14. 14

    Using Debugger how to get child process's PID from Parent

  15. 15

    Send messages from child process to parent

  16. 16

    Child shell process bidirectional redirection to parent process

  17. 17

    Is the PID of a child process always greater than the PID of its parent on Linux?

  18. 18

    The file used by parent and child process

  19. 19

    send signal from parent process to child in C

  20. 20

    Accessing memory of parent process from child process

  21. 21

    Replace Parent Process with Child Process After Death

  22. 22

    Webcam stream from parent process to child process

  23. 23

    Get child process operation result from parent process

  24. 24

    How to know if a process is a parent or a child

  25. 25

    Execl() in a child process and wait() in the parent process

  26. 26

    Is it possible to check the process id of child in different pid namespace?

  27. 27

    Piping from parent process to child and back

  28. 28

    Child process inherits a handle from its parent process but fails to write in it

  29. 29

    Piping from parent parent to child process not working

HotTag

Archive