fork() is a system call that creates a child process from the parent process. Whenever we call fork() from the parent program, a child process is created that has the exact copy of the address space. The important thing to remember is it shares the copy of the address space, not the copy itself.
Syntax:
#include<unistd.h>
pid_t fork(void);
Significance:
- Both the parent and child processes are two separate threads that execute in different address space
- Threads are similar to the process except that they share the address space
- fork function can run concurrently in the same program or can run an executable from the file system
- It helps to distinguish parent and child processes using pid. It returns 0 for the child process and a positive integer for the parent
The following example demonstrates a multiprocessing environment. fork() call has no/null arguments and returns in both processes. In the parent process, the return id (pid) is that of a child process. if that fails, -1 is returned in the parent
Example:
C++
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
pid_t c_pid = fork();
if (c_pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
else if (c_pid > 0) {
cout << "printed from parent process " << getpid()
<< endl;
}
else {
cout << "printed from child process " << getpid()
<< endl;
}
return 0;
}
|
Output
printed from parent process 33
printed from child process 34
Create Multiple Processes using a fork
When a parent forks a child, there are two processes, parent and child. But when a child creates another child, the number increases in the power of 2. For example, when there are two forks( ) calls, n=2 and the total number of processes is 2^2 =4.
Example:
C++
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
fork();
fork();
cout << "GeeksforGeeks" << endl;
return 0;
}
|
Output
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
The number of times our output statement is executed is the number of times the child process is created. Here in our example, n =3, so total process = 2^3 = 8. So there will be 8 lines in the output. Here total child process = 2^n – 1 = 8-1 = 7 (since one of them will be a parent process).
C++
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
fork();
fork();
fork();
cout << "GeeksforGeeks" << endl;
return 0;
}
|
Output
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
24 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...