2011-04-02 202 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