C++-在代码块中使用系统调用(fork、exec、wait等)

C++ - Use System Calls (fork, exec, wait etc) in Code Blocks

本文关键字:fork exec wait 系统调用 代码 C++-      更新时间:2023-10-16

我是C++的新手,很难找到一个好的IDE。现在我有代码块,但我不能使用像fork、wait等系统调用。

我用的是Windows7。

#include <iostream>
#include <stdlib.h>;
#include <sys/types.h>
#include <unistd.h>
using namespace std;
int main()
{
    cout << "I am " << (int) getpid() << endl;
    pid_t pid = fork();
    cout << "fork returned" << (int) pid << endl;
        return 0;
        if(pid<0) {
            cout << "Failed !" << endl;
            exit(3);
        }
        if(pid == 0) {
                cout << "i am the child" << (int) getpid() << endl;
                cout << "Child exiting" << endl;
                exit(0);
        }
        if(pid >= 0) {
            cout << "i am the parent" << (int) getpid() << endl;
            wait(NULL);
            cout << "Parent ending" << endl;
        }
        return 0;
}

我不能包含sys/wait.h错误:

错误:未在此作用域中声明fork错误:未在此作用域中声明等待

谢谢你的建议!

我用的是Windows7。

那么使用POSIX函数确实会很困难。Windows不是POSIX操作系统。

您将需要使用Windows API的函数。

列出项目需求,然后研究如何在Windows中实现每一个

例如:

  • windows最接近fork()的东西是什么