我已经在这个问题上挣扎了一段时间了。我正在尝试编写一个围绕运行子进程的 C++ 类。我正在使用fork()、pipe()、dup2()和execv()启动子进程并将其 stdout 和 stderr 重定向到父进程。据我所知,一切正常,直到dup2()被调用并且失败EINVAL(在 macOS 上,我不认为这是 Linux 中允许的错误类型)。如果我删除管道周围的所有逻辑,该类将按预期工作。
类声明:
class PosixSubprocess : public Subprocess {
std::string _cmd = {};
std::string _stdout = "";
std::string _stderr = "";
std::vector<std::string> _args = {};
long _pid = -1;
int _status = -1;
public:
void cmd(std::string val) override;
void addArg(std::string val) override;
void run() override;
int status() const override;
std::string out() const override;
std::string err() const override;
};
Run Code Online (Sandbox Code Playgroud)
的定义 …