Boost logo

Boost Users :

Subject: [Boost-users] ⁨[process] problem with boost::process::on_exit
From: Sandy Martel (sandy_at_[hidden])
Date: 2017-07-14 03:23:42


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 <boost/process.hpp>
#include <boost/asio.hpp>
#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();
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net