C++ Fork
A new process known as a "child process" is created with the fork system function and runs concurrently with the process that invoked fork() (parent process). Both processes will carry out the next instruction following the fork() system call once a new child process has been started. The same CPU registers, program counter, and open files that the parent process utilises are used by the child process. In this article, we will discuss the fork in detail. Keep reading to know more about this thing.
What is the fork?
A new child process is created via fork(). When fork() is used in the parent program, it produces a child process that uses a distinct address space but shares an exact copy of the parent program's address space. The same memory region is shared by the parent and child processes, but their address spaces are distinct. It returns an integer value and requires no arguments. Various values returned by the fork are listed below ().
- Negative Value: The procedure of producing a child was unsuccessful.
- Zero: The child process that was just started was returned.
- Returned to the caller or parent with a positive value. The newly generated child process's process ID is contained in the value.
Example 1:
#include<iostream>
#include<unistd.h>
#include<sys/types.h>
using namespace std;
int main()
{
fork();
cout<<"World is beatiful"<<endl;
return 0;
}
Output:

Example 2:
#include <string>
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "err.h"
using namespace std;
int main ()
{
pid_t pid;
int i;
cout << "My process id = " << getpid() << endl;
for (i = 1; i <= 4; i++)
switch ( pid = fork() ) {
case -1:
printf("Error in fork");
case 0:
cout << "Child process: My process id = " << getpid() << endl;
cout << "Child process: Value returned by fork() = " << pid << endl;
return 0;
default:
cout << "Parent process. My process id = " << getpid() << endl;
cout << "Parent process. Value returned by fork() = " << pid << endl;
}
return 0;
}
Output:

The number of forks
This is a fascinating query. Because the parent process and the child process both share the same memory section, we already know this. This indicates that the same program is being run by both of these processes. However, inside the child process, the fork() function just returns 0. (zero). The fork() call is followed by the continuation of the execution. When one child process generates a new set of child processes, that's when things get interesting. The power of two is increased since there are now two processes when one parent process generates a second child process.
Example 1:
#include<iostream>
#include<unistd.h>
#include<sys/types.h>
int main()
{
fork();
fork();
fork();
cout<<"World is beautiful"<<endl;
return 0;
}
Output:

Code Explanation:
The number of processes created is equal to how many times the word "hello" is printed. Total Processes = 2n, where n is the number of system calls for forking. As a result, n = 3 and 2^3 equals 8. There are so eight steps in total (new child processes and one original process). The following would be a tree hierarchy representation of the relationship between the processes:
The key procedure: P0
Processes brought about by the first fork: P1
Processes produced by the second fork: P2, P3.
Third-fork processes include P4, P5, P6, and P7.
Example 2:
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
void forkexample()
{
if (fork()==0)
printf("It is Child!\n");
else
printf("It is Parent!\n");
}
int main()
{
forkexample();
return 0;
}
Output:

Code Explanation:
The code above creates a child process. Fork() produces a result of 0 for the parent process and a positive integer for the child process. Due to the parallel operation of the parent process and child process, in this case, two outputs are available. Therefore, we are unsure of which process will receive control from the OS first: the parent process or the child process. Although the same program is being performed by both the parent and child processes, they are not identical. These two processes receive different amounts of data and states from the OS. Therefore their control flows may differ.
Example 3:
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
void forkexample()
{
int x = 1;
if (fork() == 0)
printf("Child has x = %d\n", ++x);
else
printf("Parent has x = %d\n", --x);
}
int main()
{
forkexample();
return 0;
}
Output:

Code Explanation:
Because the data and states of the two processes are separate, a global variable change in one process has no effect on the other two processes. Additionally, the parent and child operate concurrently, allowing for two outputs.