在本节中,我们将了解C中的fork系统调用。此fork系统调用用于创建新进程。这个新创建的进程称为子进程。当前正在创建另一个子进程的进程称为父进程。
子进程使用与父进程相同的程序计数器,CPU寄存器和相同的文件。
在fork()
不带任何参数,它返回整数值。它可能返回三种类型的整数值。
负数:创建子进程失败时返回负数
零值:为新创建的子进程返回零
正值:正值将返回到父流程。
#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main() { fork(); //make a child process of same type printf("Fork testing code\n"); return 0; }
输出结果
soumyadeep@soumyadeep-VirtualBox:~$ ./a.out Fork testing code soumyadeep@soumyadeep-VirtualBox:~$ Fork testing code soumyadeep@soumyadeep-VirtualBox:~$