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

Alex.P

How does one identify if a process is a child/grandchild of another process using its pid?

Filip Allberg

Process IDs: Child- and parent processes

All running programs have a unique process ID. The process ID, a non-negative integer, is the only identifier of a process that is always unique. But, process IDs are reused.

As a process terminates its ID becomes available for reuse. Certain systems delay reuse so that newly created processes are not confused with old ones.

Certain IDs are "reserved" in the sense that they are being used by system processes, such as the scheduler process. Another example is the init process that always occupies PID 1. Depending on the system the ID might be actively reserved.

Running the commands

> ps -eaf | head -n 5
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 11:49 ?        00:00:02 /sbin/init splash
root         2     0  0 11:49 ?        00:00:00 [kthreadd]
root         3     2  0 11:49 ?        00:00:00 [ksoftirqd/0]
root         5     2  0 11:49 ?        00:00:00 [kworker/0:0H]

and

> pidof init
1

will allow you to independently verify this.1

In C we can use the following functions to get the process ID of the calling process and the parent process ID of the calling process,

#include <unistd.h>

pid_t getpid(void);
pid_t getppid(void);

A process can create other processes. The created processes are called "child processes" and we refer to the process that created them as the "parent process".

Creating a new process using fork()

To create a child process we use the system call fork()

#include <unistd.h>

pid_t fork(void);

The function is called once, by the parent process, but it returns twice. The return value in the child process is 0, and the return value in the parent process is the process ID of the new child.1

A process can have multiple child processes but there is no system call for a process to get the process IDs of all of its children, so the parent observes the return value of the child process and can use these identifiers to manage them.

A process can only have a single parent process, which is always obtainable by calling getppid.

The child is a copy of the parent, it gets a copy of the parent's data space, heap and stack. They do not share these portions of memory! 2

We will compile and execute the following code snippet to see how this works,

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>

int main(void) {
    int var = 42; // This variable is created on the stack
    pid_t pid;

    // Two processes are created here
    //                 v~~~~~~~~~~|
    if ((pid = fork()) < 0) {
        perror("Fork failed");
    } else if (pid == 0) { // <- Both processes continue executing here
        // This variable gets copied
        var++; 

        printf("This is the child process:\n"
               "\t my pid=%d\n"
               "\t parent pid=%d\n"
               "\t var=%d\n", getpid(), getppid(), var);

    } else {
        printf("This is the parent process:\n"
               "\t my pid=%d\n"
               "\t child pid=%d\n"
               "\t var=%d\n", getpid(), pid, var);

    }


    return 0;
}

We will see when we execute the program that there are no guarantees as to which process gets to execute first. They may even operate simultaneously, effectively interleaving their output. 3

$ # Standard compilation
$ gcc -std=c99 -Wall fork_example1.c -o fork_example1
$ # Sometimes the child executes in its entirety first
$ ./fork_example1
This is the child process:
     my pid=26485
     parent pid=26484
     var=43
This is the parent process:
     my pid=26484
     child pid=26485
     var=42
$ # and sometimes the parent executes in its entirety first
$ ./fork_example1
This is the parent process:
     my pid=26461
     child pid=26462
     var=42
This is the child process:
     my pid=26462
     parent pid=26461
     var=43
$ # At times the two might interleave
$ ./fork_example1
This is the parent process:
     my pid=26455
This is the child process:
     my pid=26456
     parent pid=26455
     var=43
     child pid=26456
     var=42

1 PID stands for Process ID and PPID stands for Parent Process ID.

2 The process ID 0 is reserved for use by the kernel, so it is not possible for 0 to be the process ID of a child.

3 Many systems do not perform a complete copy of these memory segments and instead only creates a copy when either process performs a write. Initially, the shared regions are marked by the kernel as "read-only" and whenever a process tries to modify these regions the kernel awards each process their own copy of that memory.

4 Standard out is buffered so it's not a perfect example.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How does the fork() know whether it is in child process and in parent process?

From Dev

How does a parent process know the process IDs of the child processes it started?

From Dev

How to know the child number of parent?

From Java

How to get child process from parent process

From Dev

How to debug child and parent process using windbg?

From Dev

How to wait for parent process to exit in a child?

From Dev

How can child kill parent process while parent process waits until child process to terminate?

From Dev

How to let the child process live when parent process exited?

From Dev

How to find all child process of a parent process (in C)

From Dev

How to discard stdout of child process but keep stdout of parent process?

From Dev

How to know, which child are called static parent method?

From Dev

How to know which child sending event to parent with @Output on Angular

From Dev

For waitpid, how do I know if the child finishes its process?

From Dev

If you fork() and exec() from the child process, and wait in the parent, how does the parent get a return code from the child?

From Dev

Python: how to kill child process(es) when parent dies?

From Dev

How to run a process with ShellExecuteEx as as a child that closes after parent exit?

From Dev

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

From Dev

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

From Dev

How to IPC with the parent process when creating child processes in a loop (Ruby)

From Dev

How to get the child nodes of a parent and process it with jquery ajax

From Dev

Why is child process running code of its parent and how to prevent it?

From Dev

How to share numpy random state of a parent process with child processes?

From Dev

How to get the id of a very short child process if the parent is known?

From Dev

when parent process is killed by 'kill -9', how to terminate child processes

From Dev

How to get the child nodes of a parent and process it with jquery ajax

From Dev

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

From Dev

How to run a process with ShellExecuteEx as as a child that closes after parent exit?

From Dev

C Programing : Parent & Child Process

From Dev

Kill the parent of a child pipe process

Related Related

  1. 1

    How does the fork() know whether it is in child process and in parent process?

  2. 2

    How does a parent process know the process IDs of the child processes it started?

  3. 3

    How to know the child number of parent?

  4. 4

    How to get child process from parent process

  5. 5

    How to debug child and parent process using windbg?

  6. 6

    How to wait for parent process to exit in a child?

  7. 7

    How can child kill parent process while parent process waits until child process to terminate?

  8. 8

    How to let the child process live when parent process exited?

  9. 9

    How to find all child process of a parent process (in C)

  10. 10

    How to discard stdout of child process but keep stdout of parent process?

  11. 11

    How to know, which child are called static parent method?

  12. 12

    How to know which child sending event to parent with @Output on Angular

  13. 13

    For waitpid, how do I know if the child finishes its process?

  14. 14

    If you fork() and exec() from the child process, and wait in the parent, how does the parent get a return code from the child?

  15. 15

    Python: how to kill child process(es) when parent dies?

  16. 16

    How to run a process with ShellExecuteEx as as a child that closes after parent exit?

  17. 17

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

  18. 18

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

  19. 19

    How to IPC with the parent process when creating child processes in a loop (Ruby)

  20. 20

    How to get the child nodes of a parent and process it with jquery ajax

  21. 21

    Why is child process running code of its parent and how to prevent it?

  22. 22

    How to share numpy random state of a parent process with child processes?

  23. 23

    How to get the id of a very short child process if the parent is known?

  24. 24

    when parent process is killed by 'kill -9', how to terminate child processes

  25. 25

    How to get the child nodes of a parent and process it with jquery ajax

  26. 26

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

  27. 27

    How to run a process with ShellExecuteEx as as a child that closes after parent exit?

  28. 28

    C Programing : Parent & Child Process

  29. 29

    Kill the parent of a child pipe process

HotTag

Archive