The code below outputs:
1- child about to sleep for 2s
2- child about to sleep for 4s
3- child finish sleeping for 2s
6- sleep 4s child finished
5- child finish sleeping for 4s
4- sleep 2s child finished
in a posix environment. I would expect the output to be in-order, looks like I get the wrong on_exit notification for the child processes I launch.
Q: is this a bug or am I doing something wrong ? What is the correct way to get notified of specific process termination ?
Regards,
Sandy.
code:
#include
#include
#include <thread>
#include <iostream>
int main( int argc, char **argv )
{
if ( argc > 2 && strcmp( argv[1], "sleep" ) == 0 )
{
auto s = atoi(argv[2]);
std::cout << (s/2) << "- child about to sleep for " << s << "s" << std::endl;
std::this_thread::sleep_for( std::chrono::seconds(s) );
std::cout << (s+1) << "- child finish sleeping for " << s << "s" << std::endl;
return 0;
}
namespace bp = boost::process;
boost::asio::io_service ios;
// launch a child that will sleep for 2s
auto c1 = bp::child( argv[0], "sleep", "2", ios,
bp::on_exit( [](int, const std::error_code&)
{ std::cout << "4- sleep 2s child finished" << std::endl; }) );
// wait a bit, make sure the child startup for my test
std::this_thread::sleep_for( std::chrono::milliseconds(10) );
// launch a child that will sleep for 4s
auto c2 = bp::child( argv[0], "sleep", "4", ios,
bp::on_exit( [](int, const std::error_code&)
{ std::cout << "6- sleep 4s child finished" << std::endl; }) );
// wait for the notifications
ios.run();
}