2011-04-02 266 views
1

我試圖在C++中使用fork()wait()系統調用。操作系統

我的代碼非常簡單。但是,我收到以下錯誤:

error C3861: 'fork': identifier not found 

我已經包含以下頭文件。我必須在這裏包含其他標題嗎?我做錯了什麼?

#include<stdafx.h> 
#include <sys/types.h> 
#include <signal.h> 

int main(){ 

    if(fork()==0) 
    { 
     printf("from child"); 
    } 
    else 
    { 
     printf("from parent"); 
    } 
} 
+0

您使用的是什麼操作系統?在Windows上,fork()不起作用。嘗試使用cygwin。 – Alan 2011-04-02 03:03:08

+0

http://stackoverflow.com/questions/15393218/error-c3861-tcsdup-identifier-not-found - 檢查這裏,如果這可以幫助你有點......! – 2015-01-14 11:06:57

回答

4

fork()僅適用於posix系統。它當然不適用於Windows。你確定你的操作系統提供fork嗎?

9

通常情況下,你還需要以下獲得fork()

#include <unistd.h> 

但是,你似乎是使用Windows和fork()並不適用於Windows。 This page討論Windows解決方法。

One of the largest areas of difference is in the process model. UNIX has fork; Win32 does not. Depending on the use of fork and the code base, Win32 has two APIs that can be used: CreateProcess and CreateThread. A UNIX application that forks multiple copies of itself can be reworked in Win32 to have either multiple processes or a single process with multiple threads. If multiple processes are used, there are multiple methods of IPC that can be used to communicate between the processes (and perhaps to update the code and data of the new process to be like the parent, if the functionality that fork provides is needed). For more on IPC, see Interprocess Commuications.

+1

呼叫良好。 C3861絕對是一個微軟錯誤。 – paxdiablo 2011-04-02 03:11:53