A practical explanation of difference between parent and child process. C/C++ Demo code (Linux Programming)

This post will give you the basic understanding of "PROCESSES" from practical point of view.

What it is ?
Parent vs child process .

C/C++ code in support of theoretical definition.

What it is ? - If two or more media player or terminal or browser or any other application are running in background. This means two or more threads of that particular application are running in parallel.  " A Thread is a running instance of any program" .

#1  Every process is unique hence it has unique process ID ( 16 bit unique identifier).  (in Linux referred to as pid ).
#2  Every process have its own parent process except init process. Visualize it as , init process  is the root and expand it as a tree.
#3  Parent process ID is same as normal process id but we refer it as ppid .

example1.c  Printing process id of program itself

 #include <stdio.h>  
 #include <unistd.h>  //Unix standard header file   
 int main()  
 {  
    pid_t child_pid =getpid();     // pid_t is a typedef type of process id  
    pid_t parent_pid =getppid();  
   printf("Child Process id is - %d \n",(int)child_pid);   //we type caseted our result to int otherwise  
   printf("Parent process id is- %d", (int)parent_pid);   // it is in hexa .  
 return 0;  
 }  
 
Running and Analysis:

# In practical what is actually parent process?


save above code with file name example1.c and compile and run as shown in above image.

Here parent process id is the process ID of terminal.

# So if you run it again in the same terminal only process ID will change but parent process ID remains the same as shown in image below.


Below it is shown that if you run example1 in two different terminal then parent process ID will also change also the processes running in the particular terminal.



Hope you enjoy this post, share this

In next post we will go little deeper , how we can create individual processes. what are different ways of creating it .

No comments:

Post a Comment