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

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


Process IDs: Child- and parent processes

All running programs have a unique process ID. Theprocess ID, anon-negative integer, is the only identifier of a process that isalways unique. But, process IDs are reused.

As a process terminates its ID becomes available for reuse. Certainsystems delay reuse so that newly created processes are not confusedwith old ones.

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

Running the commands

> ps -eaf | head -n 5UID        PID  PPID  C STIME TTY          TIME CMDroot         1     0  0 11:49 ?        00:00:02 /sbin/init splashroot         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 init1

will allow you to independently verify this.1

In C we can use the following functions to get the process ID of thecalling 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 asthe "parent process".

Creating a new process using fork()

To create a child process we use the system callfork()

#include <unistd.h>pid_t fork(void);

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

A process can have multiple child processes but there is no systemcall for a process to get the process IDs of all of its children, sothe parent observes the return value of the child process and can usethese identifiers to manage them.

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

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

We will compile and execute the following code snippet to see howthis 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 guaranteesas to which process gets to execute first. They may even operatesimultaneously, 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_example1This is the child process:     my pid=26485     parent pid=26484     var=43This is the parent process:     my pid=26484     child pid=26485     var=42$ # and sometimes the parent executes in its entirety first$ ./fork_example1This is the parent process:     my pid=26461     child pid=26462     var=42This is the child process:     my pid=26462     parent pid=26461     var=43$ # At times the two might interleave$ ./fork_example1This is the parent process:     my pid=26455This 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 forParent Process ID.

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

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

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


Use getpid() and getppid() function to get process id and parent process id.