All pastes #635116 Raw Edit

Miscellany

public text v1 · immutable
#635116 ·published 2007-07-26 21:14 UTC
rendered paste body
using namespace std;

class ChildWatch : public sigc::trackable
{
public:
  ChildWatch(const Glib::RefPtr<Glib::MainLoop>& mainLoop)
  : m_mainLoop(mainLoop)
  {}

  void on_child_exited(GPid pid,int status);
  void run(); // fork a child and call signal_child_watch

private:
  Glib::RefPtr<Glib::MainLoop> m_mainLoop;
};

void ChildWatch::run()
{
  GPid pid = fork();
  
  if(pid==0)
  {
    sleep(5);
    exit(0);
  }
  
  std:: cout << "Child " << pid << " created" << std::endl;
  
  Glib::signal_child_watch().connect(sigc::mem_fun(*this, &ChildWatch::on_child_exited), pid);
}

void ChildWatch::on_child_exited(GPid pid, int status)
{
  std::cout << "Child " << pid << " exited with status " << status << std::endl;
  m_mainLoop->quit();
}

int main()
{
  Glib::RefPtr<Glib::MainLoop> mainLoop = Glib::MainLoop::create();
  
  ChildWatch cwatch(mainLoop);
  cwatch.run();
  mainLoop->run();

  return 0;
}