我需要能够将 fork() 用于一个小项目。问题是示例代码不起作用:
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
pid_t pID = fork();
if (pID == 0) // child
{
printf("CHILD\n");
// Code only executed by child process
}
else if (pID < 0) // failed to fork
{
printf("FAIL\n");
}
else // parent
{
printf("PARENT\n"); // Code only executed by parent process
}
// Code executed by both parent and child.
system("PAUSE");
return 0;
}
编译器说:“20 D:\Untitled1.cpp `fork' undeclared (first use this function)”
但我在互联网上读到它应该位于#include <unistd.h>
.
有任何想法吗?谢谢!