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

In C language, the fork() function

王林
Release: 2023-09-19 08:01:14
forward
1507 people have browsed it

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

Output

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

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!

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