在这个部分中,我们将看到如何使用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中文网其它相关文章!