操作系统

use*_*660 1 c

我试图在C++中使用fork()wait()系统调用.

我的代码非常简单.但是我收到以下错误:

error C3861: 'fork': identifier not found 
Run Code Online (Sandbox Code Playgroud)

我已经包含以下头文件.我是否必须在此处添加其他标题?我做错了什么?

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

int main(){

    if(fork()==0)
    {
        printf("from child");
    }
    else
    {
        printf("from parent");
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 9

通常,您还需要以下内容才能获得fork():

 #include <unistd.h>
Run Code Online (Sandbox Code Playgroud)

但是,您似乎使用的是Windows,fork()但在Windows上不可用. 本页讨论Windows的解决方法.

最大的区别之一是过程模型.UNIX有fork; Win32没有.根据fork和代码库的使用,Win32有两个可以使用的API:CreateProcess和CreateThread.可以在Win32中重新编写一个分析自身多个副本的UNIX应用程序,以使其具有多个进程或具有多个线程的单个进程.如果使用多个进程,则可以使用多种IPC方法在进程之间进行通信(如果需要fork提供的功能,可能还要将新进程的代码和数据更新为父进程).有关IPC的更多信息,请参阅进程间通信.