title: Running a process using boost process in async mode with timeout
body: <p>In the following code, I am trying to implement a program that runs a shell command and get the <code>stdio</code>, <code>stderr</code> and return code. I am doing it using <code>boost process</code> in the <code>async</code> mode as advised <a href="https://stackoverflow.com/questions/52496487/get-stdout-of-a-shell-command-using-boost-process">here</a>. </p>
  
  <pre><code>namespace bp = boost::process;
  class Process {
  
  public:
      Process(std::string &amp; cmd, const int timeout);
      void run();
  
  private:
      void timeout_handler();
  
      const std::string command;
      const int timeout;
  
      bool killed;
      bool stopped;
  
      std::string stdOut;
      std::string stdErr;
      int returnStatus;
  
      boost::asio::io_service ios;
      boost::process::group group;
      boost::asio::deadline_timer deadline_timer;
  };
  
  Process::Process(std::string &amp; cmd, const int timeout):
      command(cmd),
      timeout(timeout),
      deadline_timer(ios)
  {}
  
  void Process::timeout_handler()
  {
      if (stopped)
      return;
  
      if (deadline_timer.expires_at() &lt;= boost::asio::deadline_timer::traits_type::now())
      {
          std::cout &lt;&lt; "Time Up!" &lt;&lt; std::endl;
          group.terminate();
          std::cout &lt;&lt; "Killed the process and all its decendents" &lt;&lt; std::endl;
          killed = true;
          stopped = true;
          deadline_timer.expires_at(boost::posix_time::pos_infin);
      }
      deadline_timer.async_wait(std::bind(&amp;Process::timeout_handler, this));
  }
  
  void Process::run()
  {
  
      std::future&lt;std::string&gt; dataOut;
      std::future&lt;std::string&gt; dataErr;
  
      bp::child c(command, bp::std_in.close(), bp::std_out &gt; dataOut, bp::std_err &gt; dataErr, ios, group);
      deadline_timer.expires_from_now(boost::posix_time::seconds(timeout));
      deadline_timer.async_wait(std::bind(&amp;Process::timeout_handler, this));
  
      ios.run();
      c.wait();
  
      stdOut = dataOut.get();
      stdErr = dataErr.get();
      returnStatus = c.exit_code();
  }
  
  int main(int argc, char** argv)
  {
      if(argc &lt; 2)
      {
      std::cout &lt;&lt; "Usage: \na.out &lt;command&gt;" &lt;&lt; std::endl;
      exit(1);
      }
      std::vector&lt;std::string&gt; arguments(argv + 1, argv + argc);
  
      std::string command;
      for( const auto &amp; tok : arguments)
      {
          command += tok + " ";
      }
  
      std::cout &lt;&lt; command &lt;&lt; std::endl;
      Process p(command, 10);
      p.run();
      return 0;
  }
  </code></pre>
  
  <p>Now, the above code returns only after <code>deadline_timer</code> expires. What I want is that the child process should exit if it finishes before the timer expires or it (along with all the child processes it forks), should be terminated. Please point out the mistake in my code. </p>