【发布时间】:2020-07-22 12:27:00
【问题描述】:
我目前正在尝试实现一个可以与父进程通信的进程池。在父进程告诉它们(可能使用信号)之前,任何子进程都不应该退出。现在我的脑海中出现了几个问题,我很高兴使用我的 MWE 获得一些意见:
#include <iostream>
#include <boost/thread.hpp>
#include <boost/process/async_pipe.hpp>
#include <boost/asio.hpp>
#include <boost/array.hpp>
static const std::size_t process_count = 3;
static void start_reading(boost::process::async_pipe& ap)
{
static boost::array<char, 256> buf;
ap.async_read_some(boost::asio::buffer(buf.data(), buf.size()), [](const boost::system::error_code& error, std::size_t bytes_transferred)
{
if(!error)
{
std::cout << "received " << bytes_transferred << " from pid " << getpid() << " " << buf[0] << "...." << std::endl;
// perform some heavy computations here..
}
});
}
static void start_writing(boost::process::async_pipe& ap)
{
boost::array<char, 256> buf;
buf.fill('A');
ap.async_write_some(boost::asio::buffer(buf.data(), buf.size()), [&ap](const boost::system::error_code& error, std::size_t bytes_transferred)
{
if(!error)
{
std::cout << "parent " << getpid() << " sent " << bytes_transferred << " to [" << ap.native_source() << "," << ap.native_sink() << "]" << std::endl;
}
});
}
int main()
{
try
{
boost::asio::io_service io_context;
// prevent the associated executor from stopping
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> guard = boost::asio::make_work_guard(io_context);
pid_t main_process = getpid();
std::cout << "before forks " << main_process << std::endl;
std::vector<boost::process::async_pipe> pipes;
pipes.reserve(process_count);
for(std::size_t i = 0; i < process_count; i++)
{
pipes.emplace_back(io_context);
io_context.notify_fork(boost::asio::io_service::fork_prepare);
pid_t pid = fork();
if(pid == 0)
{
io_context.notify_fork(boost::asio::io_service::fork_child);
// perform some costly initialization here...
boost::process::async_pipe& ap = pipes[i];
std::cout << "child " << getpid() << " listening to [" << ap.native_source() << "," << ap.native_sink() << "]" << std::endl;
start_reading(ap);
io_context.run();
}
else if(pid > 0)
{
io_context.notify_fork(boost::asio::io_service::fork_parent);
}
else
{
std::cerr << "fork() failed" << std::endl;
}
}
// only parent gets there
start_writing(pipes[0]);
start_writing(pipes[0]);
start_writing(pipes[1]);
start_writing(pipes[2]);
io_context.run();
}
catch(const std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 1;
}
程序输出
before forks 15603
child 15611 listening to [8,9]
child 15612 listening to [10,11]
parent 15603 sent 256 to [8,9]
parent 15603 sent 256 to [8,9]
parent 15603 sent 256 to [10,11]
parent 15603 sent 256 to [21,22]
received 256 from pid 15612 A....
received 256 from pid 15611 A....
child 15613 listening to [21,22]
received 256 from pid 15613 A....
我当时主要关心的是如何在工作进程(子进程)中无限读取数据,只要进程不忙。一旦工作人员从 async_read_some 进入处理程序,它就会按照注释中的说明执行一些计算(可能需要几秒钟)。在执行此操作时,该过程应该并且将会阻塞,之后我想通知我的父母再次准备好并接受管道上的新读取。到目前为止,我对如何做到这一点没有任何深刻的想法。从子进程通知父进程本身并不是必须的,但是父进程需要一直跟踪所有空闲的子进程,因此它可以通过相应的管道发送新的输入。
除此之外,我还没有得到一件事:
请注意,boost::array<char, 256> buf; 在start_reading 中是static。如果我删除了静态修饰符,我永远不会进入async_read_some 的完成处理程序,这是为什么呢?
编辑:
在完成例程中再次调用start_reading,将继续读取。然而,没有父进程“知道”它。
编辑2: 到目前为止,我想出了一种可行的方法(猜想有几种)。我还没有完成实现,但共享互斥体按预期工作。这是一些伪代码:
process_pool
{
worker get_next_worker()
ScopedLock(memory_mapped_mutex);
free_worker = *available.rbegin()
available.pop_back()
return free_worker;
memory_mapped_vec<worker> available;
};
server::completion_handler_async_connect()
get_next_worker().socket().write(request)
worker::completion_handler_async_read()
// do something very long before locking
ScopedLock(memory_mapped_mutex);
process_pool::available.push_back(self);
【问题讨论】:
标签: c++ asynchronous process fork boost-asio