Difference Between Process, Parent Process, and Child Process
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:
Before Forking After Forking Before Forking After Forking
Explanation: All the statements after the fork() are executed twice:
- Once by the parent process.
- The second time the statements are executed by the child process.
Let’s discuss the following concepts in more detail:
- Parent Process.
- 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:
- Program counter.
- Process stack.
- 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:
A process control block and process table are associated with each of the processes. It contains the following important information about the process:
- Process state.
- Process number.
- Program counter.
- List of files and registers.
- CPU information.
- 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.
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.
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.
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.
- A process is an instance of a program that is currently being executed.
- A parent process is a process that creates one or more child processes.
- A child process is a new process that is created by a parent process, and inherits certain attributes from its parent.
- In summary, processes, parent processes, and child processes are fundamental concepts in modern operating systems that enable
- 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:
- 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.
- 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.
- 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.
- 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:
- 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.
- 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.
- Synchronization: Coordination between processes can be difficult and requires careful design to avoid issues such as deadlocks and race conditions.
- 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.
- 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.
Please Login to comment...