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

Create multiple processes using fork() function in C

WBOY
Release: 2023-09-08 18:17:03
forward
1860 people have browsed it

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;
}
Copy after login

Output

soumyadeep@soumyadeep-VirtualBox:~$ ./a.out
Parent process
soumyadeep@soumyadeep-VirtualBox:~$ Child process
soumyadeep@soumyadeep-VirtualBox:~$
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template