c++ - fork() 未声明的错误

标签 c++ c 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 ‘fork’ undeclared (first use this function)”

但是我在网上看到它应该位于#include <unistd.h> .

有什么想法吗?谢谢!

最佳答案

在 Windows 上,您不能使用 fork()。请改用 CreateProcess()/CreateProcessEx()

关于c++ - fork() 未声明的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15554872/

相关文章:

C:将 stdin、stdout 重定向到管道

c - mvaddch() 不打印到控制台

c++ - 如何知道线程上下文切换何时发生?

c++ - boost::variant, shared-ptr 赋值错误

C 中的常量字符串数组

c - Intel i5 处理器中的线程数

c - 基于管道的进程间通信程序

c - fork调用后的地址空间

c++ - throw() (即 __declspec(nothrow)) 是否在 Visual C++ 中提供了真正的好处?

C++:通过 ref 交换字符