在本节中,我们将了解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</p><p>");
return 0;
}
输出
soumyadeep@soumyadeep-VirtualBox:~$ ./a.out
Fork testing code
soumyadeep@soumyadeep-VirtualBox:~$ Fork testing code
soumyadeep@soumyadeep-VirtualBox:~$