Skip to content
Related Articles

Related Articles

Improve Article

Difference Between Process, Parent Process, and Child Process

  • Last Updated : 19 May, 2021

Running program is a process. From this process, another process can be created. There is a parent-child relationship between the two processes. This can be achieved using a library function called fork(). fork() function splits the running process into two processes, the existing one is known as parent and the new process is known as a child. Here is a program that demonstrates this:

C




// C program to demonstrate
// the above concept
#include <sys/types.h>
#include<stdio.h>
#include <unistd.h> 
  
// Driver code
int main()
{
  printf ("Before Forking\n");
  fork();
  printf ("After Forking\n");
}
Output
Before Forking
After Forking
Before Forking
After Forking

Explanation: All the statements after the fork() are executed twice: 

  1. Once by the parent process.
  2. The second time the statements are executed by the child process.

Let’s discuss the following concepts in more detail:

  1. Process.
  2. Parent Process.
  3. Child Process.

Process: A process is a program under execution i.e an active program. A process is more than the program code, it includes the following:



  1. Program counter.
  2. Process stack.
  3. Registers.
  4. Program code, etc.

On the contrary program code is only a text section.

A process changes its state as it executes. The new state partially depends on the current activity of a process. The different states of the process during its execution are:

  1. New
  2. Ready
  3. Running
  4. Blocked
  5. Terminated.

A process control block and process table are associated with each of the processes. It contains the following important information about the process:

  1. Process state.
  2. Process number.
  3. Program counter.
  4. List of files and registers.
  5. CPU information.
  6. Memory information, etc.

Parent Process: All the processes are created when a process executes the fork() system call except the startup process. The process that executes the fork() system call is the parent process. A parent process is one that creates a child process using a fork() system call. A parent process may have multiple child processes, but a child process only one parent process.

On the success of a fork() system call:

  • The Process ID (PID) of the child process is returned to the parent process.
  • 0 is returned to the child process.

On the failure of a fork() system call, 

  • -1 is returned to the parent process.
  • A child process is not created.

Child Process: A child process is created by a parent process in an operating system using a fork() system call. A child process may also be known as subprocess or a subtask.

  • A child process is created as a copy of its parent process.
  • The child process inherits most of its attributes.
  • If a child process has no parent process, then the child process is created directly by the kernel.
  • If a child process exits or is interrupted, then a SIGCHLD signal is sent to the parent process to inform about the termination or exit of the child process.

Why Do We Need to Create A Child Process?
Sometimes there is a need for a program to perform more than one function simultaneously. Since these jobs may be interrelated so two different programs to perform them cannot be created. For example: Suppose there are two jobs: copy contents of source file to target file and display an animated progress bar indicating that the file copy is in progress. The GIF progress bar file should continue to play till file copy is taking place. Once the copying process is finished the playing of the GIF progress bar file should be stopped. Since both these jobs are interrelated they cannot be performed in two different programs. Also, they cannot be performed one after another. Both jobs should be performed simultaneously. 



At such times fork() is used to create a child process and then write the program in such a manner that file copy is done by the parent and displaying of the animated GIF file is done by the child process.

Program 1: The task here is to show how to perform two different but interrelated jobs simultaneously. Hence, the actual code for file copying and playing the animated GIF file has been skipped only the approach for performing 2 jobs simultaneously is shown. 

C




// C program for the above approach
#include <sys/types.h>
  
// Driver Code
int main( )
{
  int pid;
  pid = fork();
  if (pid == 0)
  {
    printf ("In child process\n");
      
    /* code to play animated GIF file */
  }
  else
  {
    printf ("In parent process\n");
      
    /* code to copy file */
  }
}

Explanation: fork() creates a child process and duplicates the code of the parent process in the child process. There onwards the execution of the fork() function continues in both processes. Thus, the duplication code inside fork() is executed once, whereas the remaining code inside it is executed in both the parent and the child process. Hence, control would come back from the fork() twice, even though it is actually called only once. When control returns from the fork() of the parent process it returns the PID of the child process, whereas when control returns from the fork() of the child process it always returns a 0. This can be exploited by the program to segregate the code that we want to execute in the parent process from the code that we want to execute in the child process. This logic is implemented in the above program using an if statement. The ‘if block’ is executed in the case of the child process and the ‘else block’ is executed in the case of the parent process.

Program 2:
This program would use the fork() call to create a child process. In the child process, we would print the PID of the child and its parent, whereas in the parent process we would print the PID of the parent and its child. 

C




// C program to implement
// the above approach
# include <sys/types.h>
  
// Driver code
int main()
{
  int pid;
  pid = fork();
  
  if (pid == 0)
  {
    printf ("Child : I am the child process\n");
    printf ("Child : Child’s PID: %d\n", getpid());
    printf ("Child : Parent’s PID: %d\n", getppid());
  }
  else
  {
    printf ("Parent : I am the parent process\n");
    printf ("Parent : Parent’s PID: %d\n", getpid());
    printf ("Parent : Child’s PID: %d\n", pid);
  }
}

Output:

Child : I am the child process
Child : Child's PID: 4706
Child : Parent's PID: 4705
Parent : I am the Parent process
Parent : Parent's PID: 4705
Parent : Child's PID: 4706

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :