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.
Outputs
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:
Example:
The following output occurs when the parent process terminates before the child process
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:
Output
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
Output
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