Home  >  Article  >  Backend Development  >  Create multiple processes using fork() function in C

Create multiple processes using fork() function in C

WBOY
WBOYforward
2023-09-08 18:17:031814browse

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.

Sample code

#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;
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete