gpt4 book ai didi

c - 操作系统

转载 作者:太空宇宙 更新时间:2023-11-04 00:15:44 26 4
gpt4 key购买 nike

我正在尝试在 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");
}
}

最佳答案

通常,您还需要以下内容来获取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.

关于c - 操作系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5520823/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com