Home  >  Article  >  Backend Development  >  In C language, the fork() function

In C language, the fork() function

王林
王林forward
2023-09-19 08:01:141357browse

In C language, the fork() function

In this section, we will learn about the fork system call in C language. The fork system call is used to create a new process. This newly created process is called a child process. The current process that creates another child process is called the parent process.

The child process uses the same program counter, CPU registers, and the same files used by the parent process.

The fork() function does not accept any parameters, it returns an integer value. It may return three types of integer values.

  • Negative number: When the child process creation fails, a negative number is returned

  • Zero value: For newly created child processes, return zero

  • positive number: A positive number is returned to the parent process.

Sample code

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
   fork(); //make a child process of same type
   printf("Fork testing code</p><p>");
   return 0;
}

Output

soumyadeep@soumyadeep-VirtualBox:~$ ./a.out
Fork testing code
soumyadeep@soumyadeep-VirtualBox:~$ Fork testing code
soumyadeep@soumyadeep-VirtualBox:~$

The above is the detailed content of In C language, the fork() function. 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