summaryrefslogtreecommitdiff
path: root/examples/Threads/process_manager.cpp
blob: b8f74a63345970e94fa5a445ec16f34f645d2aba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// $Id$

// Test out the mechanisms provided by the ACE_Process_Manager.

#include "ace/Service_Config.h"
#include "ace/Thread_Manager.h"
#include "ace/Process_Manager.h"
#include "ace/Get_Opt.h"

ACE_RCSID(Threads, process_manager, "$Id$")

#if !defined (ACE_WIN32) && defined (ACE_HAS_THREADS)

// Global process manager.
static ACE_Process_Manager proc_mgr;

void *
sig_handler (void *)
{
  ACE_Sig_Set sigset;

  // Register a dummy signal handler so that our disposition isn't
  // SIG_IGN (which is the default).
  ACE_Sig_Action sa ((ACE_SignalHandler) sig_handler, SIGCHLD);
  ACE_UNUSED_ARG (sa);

  // Register signal handlers.
  sigset.sig_add (SIGINT);
  sigset.sig_add (SIGCHLD);

  for (;;)
    {
      int signum = ACE_OS::sigwait (sigset);

      ACE_DEBUG ((LM_DEBUG, 
		  "(%P|%t) received signal %S\n",
		  signum));

      switch (signum)
	{
	case SIGINT:
	  // Shutdown using <ACE_OS::exit>.
	  ACE_DEBUG ((LM_DEBUG, 
		      "(%P|%t) shutting down %n%a\n", 1));
	  /* NOTREACHED */
	case SIGCHLD:
	  
	  for (;;)
	    {
	      pid_t pid = proc_mgr.reap ();
	      
	      if (pid == -1) // No more children to reap.
		break;

	      ACE_DEBUG ((LM_DEBUG, "(%P|%t) reaped pid %d\n", pid));
	    }
	  
	  /* NOTREACHED */
	case -1:
	  ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "sigwait"), 0);
	  /* NOTREACHED */
	default:
	  ACE_ERROR_RETURN ((LM_ERROR, 
			     "(%P|%t) signal %S unexpected\n", signum), 0);
	  /* NOTREACHED */
	}
    }
}

static void
worker (size_t iterations)
{
  for (size_t i = 0; i < iterations; i++)
    if ((i % 100) == 0)
      {
	ACE_DEBUG ((LM_DEBUG, "(%P|%t) sleeping\n"));
	ACE_OS::sleep (5);
      }
}

extern "C" void
exithook (void)
{
  ACE_DEBUG ((LM_DEBUG, "(%P|%t) later...\n"));
}

static int n_iterations = 100000;
static int child = 0;

// Parse the command-line arguments and set options.
static void
parse_args (int argc, char *argv[])
{
  ACE_Get_Opt get_opt (argc, argv, "i:cu");

  int c; 

  while ((c = get_opt ()) != -1)
    switch (c)
    {
    case 'i':
      n_iterations = ACE_OS::atoi (get_opt.optarg);
      break;
    case 'c':
      child = 1;
      break;
    case 'u':
    default:
      ACE_DEBUG ((LM_DEBUG, "usage:\n"
		  "-p <processes>\n"
		  "-i <iterations>\n"));
      break;
  }
}

int
main (int argc, char *argv[])
{
  ACE_Service_Config daemon;

  daemon.open (argv[0]);

  atexit (exithook);

  parse_args (argc, argv);

  ACE_Sig_Set sigset (1); // Block all signals.

  if (ACE_OS::thr_sigsetmask (SIG_BLOCK, sigset, 0) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "thr_sigsetmask"), 1);
  else if (ACE_Thread_Manager::instance ()->spawn ((ACE_THR_FUNC) sig_handler, 0, THR_DETACHED) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "spawn"), 1);

  if (child)
    {
      worker (n_iterations);
      return 0;
    }

  ACE_Process_Options options;
  options.command_line ("%s -c", argv[0]);
  pid_t pid = proc_mgr.spawn (options);

  if (pid == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) %p\n", "start_n"), 1);
  else
    {
      // Give the child a chance to start running.
      ACE_OS::sleep (1);

      // Shutdown the child.

      if (proc_mgr.terminate (pid) == -1)
	ACE_ERROR_RETURN ((LM_DEBUG, "(%P|%t) %p\n", "terminate"), 1);

      // Perform a barrier wait until all the processes and threads
      // have shut down.
      proc_mgr.wait ();
	  ACE_Thread_Manager::instance ()->wait ();
    }

  return 0;
}
#else
int 
main (int, char *[])
{
  ACE_ERROR_RETURN ((LM_ERROR, "process manager not supported on this platform\n"), -1);
}
#endif /* !ACE_WIN32 && ACE_HAS_THREADS */