
在这个部分中,我们将看到如何使用fork()在C语言中创建子进程。我们还会在每个进程中执行一些不同的任务。所以在我们的父进程中,我们将打印不同的值。
当调用fork()时,它会返回一个值。如果这个值大于0,那么当前就在父进程中,否则就在子进程中。所以我们可以通过这个来区分进程。
#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;
}soumyadeep@soumyadeep-VirtualBox:~$ ./a.out Parent process soumyadeep@soumyadeep-VirtualBox:~$ Child process soumyadeep@soumyadeep-VirtualBox:~$
以上就是使用C中的fork()函数创建多个进程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号