Open In App

Difference Between Process, Parent Process, and Child Process

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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

In computer science, a process is an instance of a program that is currently being executed. It is a fundamental concept in modern operating systems, which are designed to allow multiple processes to run concurrently. Each process has its own memory space, program code, and data, and communicates with other processes through interprocess communication (IPC) mechanisms.

In the context of process management, a parent process is a process that creates one or more child processes. The parent process may also control and monitor the execution of its child processes. When a new process is created, it is called a child process, and it inherits certain attributes from its parent, such as its environment variables and open file descriptors.

The relationship between a parent process and a child process is sometimes referred to as a “parent-child” relationship. The child process can also become a parent process and create its own child processes, creating a hierarchy of processes.

To summarize:

  1. A process is an instance of a program that is currently being executed.
  2. A parent process is a process that creates one or more child processes.
  3. A child process is a new process that is created by a parent process, and inherits certain attributes from its parent.
  4. In summary, processes, parent processes, and child processes are fundamental concepts in modern operating systems that enable
  5. concurrent execution of multiple programs, and allow for a hierarchical organization of processes.

Advantages of using parent and child processes in modern operating systems include:

  1. Concurrency: By allowing multiple processes to run concurrently, the system can make better use of available resources such as CPU time, memory, and disk I/O.
  2. Isolation: Each process has its own memory space, program code, and data, which provides a degree of isolation and protection from other processes in the system.
  3. Fault tolerance: If a child process fails, the parent process can detect the failure and take appropriate action, such as restarting the child process or terminating the program.
  4. Modularity: By breaking down a program into smaller, independent processes, it becomes easier to modify, maintain, and test each process separately.

However, there are also some disadvantages of using parent and child processes:

  1. Overhead: Creating and managing multiple processes requires additional system resources, such as memory, CPU time, and disk I/O, which can slow down the system.
  2. Communication overhead: Interprocess communication (IPC) can be a complex and time-consuming task, particularly when large amounts of data need to be transferred between processes.
  3. Synchronization: Coordination between processes can be difficult and requires careful design to avoid issues such as deadlocks and race conditions.
  4. Complexity: Designing and debugging a program that uses multiple processes can be more complex than designing a single-threaded program, and may require additional skills and expertise.
  5. Overall, the benefits of using parent and child processes usually outweigh the disadvantages, particularly in systems that require high levels of concurrency, fault tolerance, and modularity.


Last Updated : 24 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads