🆔Process ID

In this section we shall see what is a PID (process id) and how we can retrieve the PID of a process as well as its parent process

A PID is a unique numerical identifier assigned to a running process in an operating system. It is used to distinguish one process from another and enables the operating system to manage and track individual processes.

#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[]){ 
    fork();
    printf("Current ID: %d Parent ID: %d\n",getpid(), getppid());
    return 0;
}

Outputs

Current ID: 8771 Parent ID: 2353
Current ID: 8772 Parent ID: 8771
Current ID: 8388 Parent ID: 2353
Current ID: 8389 Parent ID: 8388
Current ID: 8800 Parent ID: 2353
Current ID: 8801 Parent ID: 8800

Notice (in all the above 3 cases):

  • Parent ID of second line is equal to the Current ID of the first line

  • Current ID is always greater than Parent ID (since the Parent process is created first)

  • Parent ID of the first line is always same (2353) because that's the PID of the editor that's running the code

Inference drawn:

  • Current ID of the first line is the PID of the main thread

  • Current ID of the second line is the PID of the process created by the fork

  • Parent process is being executed before child process

  • Child process is being terminated before parent process

The command to check the process name using its PID is:

ps -p PID

Example:

ps -p 2353

PID TTY          TIME CMD
2353 pts/2    00:00:08 zsh

The following output occurs when the parent process terminates before the child process

Current ID: 8739 Parent ID: 2353                                      
Current ID: 8740 Parent ID: 1313

In such cases, the operating system spawns a separate process so that it can act as the parent process of the forked process.

In the last section we saw how the wait() system call works. It turns out that the wait() function returns the PID of the child process (if any) and returns -1 if there are no child processes running. Modifying the previous example:

#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[]){
    int id = fork();
    printf("Current ID: %d Parent ID: %d\n",getpid(), getppid());
    int n = (id == 0) ? 1 : 6;
    if(id != 0){
        int pid = wait();
        printf("\nWaiting for process %d to complete\n",pid);
    }
    for (int i = 0; i < 5; i++)
        printf("%d ", i + n);
    return 0;
}

Output

Current ID: 2814 Parent ID: 2599
Current ID: 2815 Parent ID: 2814
1 2 3 4 5
Waiting for process 2815 to complete
6 7 8 9 10

In fact, we don't even need to check if it's a child process or not. Instead, we can just check if the value returned by the wait() function is -1 or not

#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[]){
    int id = fork();
    printf("Current ID: %d Parent ID: %d\n", getpid(), getppid());
    int n = (id == 0) ? 1 : 6;

    int pid = wait();
    if (pid == -1)
        printf("\nNo child processes running\n");
    else
        printf("\nWaiting for process %d to complete\n", pid);

    for (int i = 0; i < 5; i++)
        printf("%d ", i + n);
    return 0;
}

Output

Current ID: 3072 Parent ID: 2599
Current ID: 3073 Parent ID: 3072

No child processes running
1 2 3 4 5 
Waiting for process 3073 to complete
6 7 8 9 10

Explanation

After the control reaches the forked process (PID : 3073), since there are not more child processes being forked, the process (PID : 3073) prints "No child processes running" . The main thread waits till all of this finishes

After the forked process is executed, it still takes some time to get terminated. That's why the main thread prints "Waiting for process 3073 to complete" .

Last updated