与 boost 中的 dup2 类似的功能

Similar functionality to dup2 in boost

我想做这样的事情

acceptSocket = accept(...);
if (fork() == 0) {
  // ..
  dup2(acceptSocket, 1);
  dup2(acceptSocket, 2);
  execvp(/*some command*/);
  
}

现在我正在转向 C++ boost,我也想做同样的事情。有没有类似的东西?可能是 Boost Process 和套接字流,但我不太明白。提前致谢。

您可以使用升压过程:https://www.boost.org/doc/libs/1_74_0/doc/html/boost/process/posix/fd.html

This property lets you modify file-descriptors other than the standard ones (0,1,2).

It provides the functions bind, which implements dup2 and close.

因此,例如:

#include <boost/process.hpp>

namespace bp = boost::process;
using bp::posix::fd;

int main() {
    int acceptSocket /* = accept(...) */;

    bp::child child(
        bp::search_path("someprogram.exe"),
        fd.bind(1, acceptSocket),
        fd.bind(2, acceptSocket));

    child.wait();
}