c – 在多线程程序中如何使用boost :: asio正确处理fork()?

我在使用Boost Asio以多线程方式从多线程程序中正确处理创建子进程时遇到麻烦.

如果我理解正确,在Unix世界中启动一个子进程的方法调用fork(),然后调用exec *().另外,如果我正确理解,调用fork()将会重复所有的文件描述符等等,这些都需要在子进程中关闭,除非标记为FD_CLOEXEC(因此在调用exec *()时被原子关闭).

Boost Asio需要通过调用fork()才能正常通知调用notify_fork().但是,在多线程程序中,这会产生以下几个问题:

>如果我正确理解,默认情况下,插件是由子进程继承的.它们可以设置为SOCK_CLOEXEC – 但不能直接创建*,因此如果从另一个线程创建子进程,则导致定时窗口.
> notify_fork()要求没有其他线程调用任何其他io_service函数,也没有与io_service关联的任何其他I / O对象的任何函数.这似乎不是可行的 – 所有的程序是多线程的,因为一个原因.
>如果我理解正确,fork()和exec *()之间的任何函数调用都需要异步信号安全(见fork() documentation).没有关于notify_fork()调用异步信号安全的文档.实际上,如果我看看Boost Asio的源代码(至少在版本1.54中),可能会有pthread_mutex_lock的电话,这不是异步信号安全(参见Signal Concepts,还有其他的电话是不在白名单上).

问题#1我可以通过分离子进程和套接文件的创建来解决问题,以便我可以确保在创建的套接字和设置SOCK_CLOEXEC之间的窗口中不会创建子进程.问题#2是棘手的,我可能需要确保所有的asio处理程序线程都被阻止,做叉子,然后重新创建它们,这是最好的,最糟糕的是真的很糟糕(我的未决定时器呢? ).问题#3似乎完全不可能正确使用.

如何在一个多线程程序中与fork()exec *()正确使用Boost Asio?
…还是我“分叉”?

请让我知道,如果我误解了任何基本概念(我提出Windows编程,而不是* nix …).

编辑:
* – 实际上可以创建直接在Linux上设置的SOCK_CLOEXEC的套接字,从2.6.27开始可用(参见socket() documentation).在Windows上,相应的标志WSA_FLAG_NO_HANDLE_INHERIT可用于Windows 7 SP 1 / Windows Server 2008 R2 SP 1(参见WSASocket() documentation). OS X似乎并不支持.

解决方法

在多线程程序中,io_service::notify_fork()在孩子中调用不安全.但是,Boost.Asio希望它可以基于 fork() support进行调用,因为这是当孩子关闭父母以前的内部文件描述符并创建新的内容时.虽然Boost.Asio显式列出了调用io_service :: notify_fork()的前提条件,保证在fork()期间内部组件的状态,但是对 implementation的简要概述则表明std :: vector :: push_back()可能会从免费商店分配内存,并且分配不能保证是异步信号安全的.

就这样说,一个可能值得考虑的解决方案是fork(),当它仍然是单线程时.子进程将保持单线程,并在父进程通过进程间通信被告知执行fork()和exec()时执行.这种分离通过在执行fork()和exec()时消除管理多个线程的状态的需要来简化问题.

以下是一个完整的示例,演示此方法,多线程服务器将通过UDP接收文件名,子进程将执行fork()和exec()来运行文件名/usr/bin/touch.为了使这个例子变得更加可读,我已经选择使用stackful coroutines.

#include <unistd.h> // execl,fork
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>

/// @brief launcher receives a command from inter-process communication,///        and will then fork,allowing the child process to return to
///        the caller.
class launcher
{
public:
  launcher(boost::asio::io_service& io_service,boost::asio::local::datagram_protocol::socket& socket,std::string& command)
    : io_service_(io_service),socket_(socket),command_(command)
  {}

  void operator()(boost::asio::yield_context yield)
  {
    std::vector<char> buffer;
    while (command_.empty())
    {
      // Wait for server to write data.
      std::cout << "launcher is waiting for data" << std::endl;
      socket_.async_receive(boost::asio::null_buffers(),yield);

      // Resize buffer and read all data.
      buffer.resize(socket_.available());
      socket_.receive(boost::asio::buffer(buffer));

      io_service_.notify_fork(boost::asio::io_service::fork_prepare);
      if (fork() == 0) // child
      {
        io_service_.notify_fork(boost::asio::io_service::fork_child);
        command_.assign(buffer.begin(),buffer.end());
      }
      else // parent
      {
        io_service_.notify_fork(boost::asio::io_service::fork_parent);
      }
    }
  }

private:
  boost::asio::io_service& io_service_;
  boost::asio::local::datagram_protocol::socket& socket_;
  std::string& command_;
};

using boost::asio::ip::udp;

/// @brief server reads filenames from UDP and then uses
///        inter-process communication to delegate forking and exec
///        to the child launcher process.
class server
{
public:
  server(boost::asio::io_service& io_service,short port)
    : io_service_(io_service),launcher_socket_(socket),socket_(boost::make_shared<udp::socket>(
        boost::ref(io_service),udp::endpoint(udp::v4(),port)))
  {}

  void operator()(boost::asio::yield_context yield)
  {
    udp::endpoint sender_endpoint;
    std::vector<char> buffer;
    for (;;)
    {
      std::cout << "server is waiting for data" << std::endl;
      // Wait for data to become available.
      socket_->async_receive_from(boost::asio::null_buffers(),sender_endpoint,yield);

      // Resize buffer and read all data.
      buffer.resize(socket_->available());
      socket_->receive_from(boost::asio::buffer(buffer),sender_endpoint);
      std::cout << "server got data: ";
      std::cout.write(&buffer[0],buffer.size());
      std::cout << std::endl;

      // Write filename to launcher.
      launcher_socket_.async_send(boost::asio::buffer(buffer),yield);
    }
  }

private:
  boost::asio::io_service& io_service_;
  boost::asio::local::datagram_protocol::socket& launcher_socket_;

  // To be used as a coroutine,server must be copyable,so make socket_
  // copyable.
  boost::shared_ptr<udp::socket> socket_;
};

int main(int argc,char* argv[])
{
  std::string filename;

  // Try/catch provides exception handling,but also allows for the lifetime
  // of the io_service and its IO objects to be controlled.
  try
  {
    if (argc != 2)
    {
      std::cerr << "Usage: <port>\n";
      return 1;
    }

    boost::thread_group threads;
    boost::asio::io_service io_service;

    // Create two connected sockets for inter-process communication.
    boost::asio::local::datagram_protocol::socket parent_socket(io_service);
    boost::asio::local::datagram_protocol::socket child_socket(io_service);
    boost::asio::local::connect_pair(parent_socket,child_socket);

    io_service.notify_fork(boost::asio::io_service::fork_prepare);
    if (fork() == 0) // child
    {
      io_service.notify_fork(boost::asio::io_service::fork_child);
      parent_socket.close();
      boost::asio::spawn(io_service,launcher(io_service,child_socket,filename));
    }
    else // parent
    {
      io_service.notify_fork(boost::asio::io_service::fork_parent);
      child_socket.close();
      boost::asio::spawn(io_service,server(io_service,parent_socket,std::atoi(argv[1])));

      // Spawn additional threads.
      for (std::size_t i = 0; i < 3; ++i)
      {
        threads.create_thread(
          boost::bind(&boost::asio::io_service::run,&io_service));
      }
    }

    io_service.run();
    threads.join_all();
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  // Now that the io_service and IO objects have been destroyed,all internal
  // Boost.Asio file descriptors have been closed,so the execl should be
  // in a clean state.  If the filename has been set,then exec touch.
  if (!filename.empty())
  {
    std::cout << "creating file: " << filename << std::endl;
    execl("/usr/bin/touch","touch",filename.c_str(),static_cast<char*>(0));
  }
}

1号航站楼:

$ls
a.out  example.cpp
$./a.out 12345
server is waiting for data
launcher is waiting for data
server got data: a
server is waiting for data
launcher is waiting for data
creating file: a
server got data: b
server is waiting for data
launcher is waiting for data
creating file: b
server got data: c
server is waiting for data
launcher is waiting for data
creating file: c
ctrl + c
$ls
a  a.out  b  c  example.cpp

2号航站楼:

$nc -u 127.0.0.1 12345
actrl + dbctrl + dcctrl + d

相关文章

/** C+⬑ * 默认成员函数 原来C++类中,有6个默认成员函数: 构造函数 析构函数 拷贝...
#pragma once // 1. 设计一个不能被拷贝的类/* 解析:拷贝只会放生在两个场景中:拷贝构造函数以及赋值运...
C类型转换 C语言:显式和隐式类型转换 隐式类型转化:编译器在编译阶段自动进行,能转就转,不能转就编译...
//异常的概念/*抛出异常后必须要捕获,否则终止程序(到最外层后会交给main管理,main的行为就是终止) try...
#pragma once /*Smart pointer 智能指针;灵巧指针 智能指针三大件//1.RAII//2.像指针一样使用//3.拷贝问...
目录&lt;future&gt;future模板类成员函数:promise类promise的使用例程:packaged_task模板类例程...