PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

使用C中的fork()函数创建多个进程

WBOY
WBOY 转载
2023-09-08 18:17:03 1377浏览

使用C中的fork()函数创建多个进程

在这个部分中,我们将看到如何使用fork()在C语言中创建子进程。我们还会在每个进程中执行一些不同的任务。所以在我们的父进程中,我们将打印不同的值。

当调用fork()时,它会返回一个值。如果这个值大于0,那么当前就在父进程中,否则就在子进程中。所以我们可以通过这个来区分进程。

示例代码

#include 
#include 
int main() {
   int n = fork(); //subdivide process
   if (n > 0) { //when n is not 0, then it is parent process
      printf("Parent process 

";    } else { //when n is 0, then it is child process       printf("Child process

");    }    return 0; }

输出

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

以上就是使用C中的fork()函数创建多个进程的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除