![]() |
VOOZH | about |
Running program is a. 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:
Output
Before Forking
After Forking
After Forking
Note: Due to the concurrent nature of the parent and child processes, the outputs can appear in different orders each time you run the program.
Explanation: All the statements after the fork() are executed twice:
Let's discuss the following concepts in more detail:
: A process is a program under execution i.e an active program. A process is more than the program code, it includes the following:
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:
: All the processes are created when a process executes the fork() system call except the startup process. The process that executes the 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:
On the failure of a fork() system call,
: 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.
?
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.
Output
In parent process
In child process
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.
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: