Merge pull request #2318 from jwillemsen/jwi-add_forward_profiles
[ACE_TAO.git] / ACE / examples / APG / Processes / Process_Manager_Death.cpp
blob7f3a9bc9bac83265b1801dcd652f243ca05a3c3b
1 #include "ace/Log_Msg.h"
2 #include "ace/Process_Manager.h"
3 #include "ace/Reactor.h"
5 static const int NCHILDREN = 2;
7 // Listing 1 code/ch10
8 class DeathHandler: public ACE_Event_Handler
10 public:
11 DeathHandler () : count_(0)
13 ACE_TRACE ("DeathHandler::DeathHandler");
16 virtual int handle_exit (ACE_Process * process)
18 ACE_TRACE ("DeathHandler::handle_exit");
20 ACE_DEBUG
21 ((LM_DEBUG,
22 ACE_TEXT ("Process %d exited with exit code %d\n"),
23 process->getpid (), process->return_value ()));
25 if (++count_ == NCHILDREN)
26 ACE_Reactor::instance ()->end_reactor_event_loop ();
28 return 0;
31 private:
32 int count_;
34 // Listing 1
35 // Listing 0 code/ch10
36 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
38 if (argc > 1) // Running as a child.
39 return 0;
41 // Instantiate a process manager with space for
42 // 10 processes.
43 ACE_Process_Manager pm (10, ACE_Reactor::instance ());
45 // Create a process termination handler.
46 DeathHandler handler;
48 // Specify the options for the new processes to be spawned.
49 ACE_Process_Options options;
50 options.command_line (ACE_TEXT ("%s a"), argv[0]);
52 // Spawn two child processes.
53 pid_t pids[NCHILDREN];
54 pm.spawn_n (NCHILDREN, options, pids);
56 // Register handler to be called when these processes exit.
57 for (int i = 0; i < NCHILDREN; i++)
58 pm.register_handler (&handler, pids[i]);
60 // Run the reactor event loop waiting for events to occur.
61 ACE_Reactor::instance ()->run_reactor_event_loop ();
63 return 0;
65 // Listing 0