Home > Article > Backend Development > Create multiple processes using fork() function in C
In this section, we will see how to create a child process in C language using fork(). We also perform a few different tasks in each process. So in our parent process, we're going to print different values.
When fork() is called, it returns a value. If this value is greater than 0, then it is currently in the parent process, otherwise it is in the child process. So we can differentiate processes through this.
#include <stdio.h> #include <unistd.h> int main() { int n = fork(); //subdivide process if (n > 0) { //when n is not 0, then it is parent process printf("Parent process </p><p>"; } else { //when n is 0, then it is child process printf("Child process </p><p>"); } return 0; }
soumyadeep@soumyadeep-VirtualBox:~$ ./a.out Parent process soumyadeep@soumyadeep-VirtualBox:~$ Child process soumyadeep@soumyadeep-VirtualBox:~$
The above is the detailed content of Create multiple processes using fork() function in C. For more information, please follow other related articles on the PHP Chinese website!