我需要能夠將fork()用於小型項目。問題是,示例代碼不工作: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'叉」未申報(第一次使用此功能)」
但我已經在讀互聯網,它應該位於#include <unistd.h>
。
任何想法?謝謝!
IIRC,Windows沒有'fork()'。 * *在* POSIX *系統的'unistd.h'中。 (Windows不是100%符合POSIX的。) – cdhowie 2013-03-21 18:04:24
那麼這個速度很快,你知道我可以使用的其他功能做同樣的事嗎?謝謝! – 2013-03-21 18:05:35
請參閱[此問題](http://stackoverflow.com/q/985281/501250)。如果你想要一個類似POSIX的分叉,那麼使用Cygwin可能是你最好的選擇。 – cdhowie 2013-03-21 18:06:10