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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
// $Id$
// ============================================================================
//
// = LIBRARY
// examples/Threads/
//
// = FILENAME
// process_manager.cpp
//
// = DESCRIPTION
// Test out the mechanisms provided by the ACE_Process_Manager.
// Using the global ACE_Process_Manager::instance(), we first spawn
// some processes (re-invoke this program, and plain-old-fork on
// systems that support it), and try the wait() functions.
//
// Then, we register the Process_Manager with
// ACE_Reactor::instance() and spawn more processes, counting on the
// autoreap to clean up.
//
// Specific-pid and generic exit-handler functions are also tested.
//
// = AUTHOR
// Douglas C. Schmidt <schmidt@cs.wustl.edu> and
// Dave Madden <dhm@mersenne.com>
//
// ============================================================================
#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$")
class ExitHandler : public ACE_Event_Handler
{
public:
ExitHandler (const char *name);
virtual ~ExitHandler (void);
virtual int handle_exit (ACE_Process *proc);
virtual int handle_timeout (const ACE_Time_Value &tv,
const void *arg = 0);
virtual int handle_close (ACE_HANDLE handle,
ACE_Reactor_Mask close_mask);
// Called when object is removed from the <ACE_Reactor>.
private:
const char *name_;
};
ExitHandler::ExitHandler (const char *name)
: ACE_Event_Handler (),
name_ (name)
{
}
ExitHandler::~ExitHandler (void)
{
ACE_DEBUG ((LM_DEBUG,
"(%P|%t@%T) ExitHandler \"%s\" destroyed\n",
name_));
}
int
ExitHandler::handle_exit (ACE_Process *proc)
{
ACE_DEBUG ((LM_DEBUG,
"(%P|%t@%T) ExitHandler \"%s\" handle_exit for pid %d status %d\n",
name_,
proc->getpid (),
proc->exit_code ()));
return 0;
}
int
ExitHandler::handle_timeout(const ACE_Time_Value &,
const void *)
{
static int tick_tock = 0;
ACE_DEBUG ((LM_DEBUG,
"(%P|%t@%T) \"%s\" %s\n",
name_,
ACE_ODD (tick_tock) ? "Tock" : "Tick"));
tick_tock++;
return 0;
}
int
ExitHandler::handle_close (ACE_HANDLE,
ACE_Reactor_Mask)
{
ACE_DEBUG ((LM_DEBUG,
"(%P|%t@%T) ExitHandler \"%s\" handle_close\n",
name_));
delete this;
return 0;
}
// Spin furiously <iterations> times, pausing every 100 cycles to
// print a message and sleep for a few seconds.
static void
worker (size_t iterations)
{
for (size_t i = 0;
i <= iterations;
i++)
if (i && (i % 100) == 0)
{
ACE_DEBUG ((LM_DEBUG,
"(%P|%t@%T) worker spinning furiously... (%u)\n",
i));
ACE_OS::sleep (1);
}
ACE_DEBUG ((LM_DEBUG,
"(%P|%t@%T) worker finished\n"));
}
static int n_iterations = 500;
static int child = 0;
static int exit_code = 0;
// Parse the command-line arguments and set options.
static void
parse_args (int argc, ACE_TCHAR *argv[])
{
ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("i:e:cu"));
int c;
while ((c = get_opt ()) != -1)
switch (c)
{
case 'i':
n_iterations = ACE_OS::atoi (get_opt.opt_arg ());
break;
case 'e':
exit_code = ACE_OS::atoi (get_opt.opt_arg ());
break;
case 'c':
child = 1;
break;
case 'u':
default:
ACE_DEBUG ((LM_DEBUG, "usage:\n"
"-p <processes>\n"
"-i <iterations>\n"));
break;
}
}
// Use ACE_Process_Manager::instance() to spawn another copy of this
// process.
static pid_t
respawn_self (const ACE_TCHAR *myname,
int iter,
int exit_code)
{
ACE_Process_Options options;
options.command_line ("%s -c -i %d -e %d",
myname,
iter,
exit_code);
return ACE_Process_Manager::instance ()->spawn (options);
}
int
main (int argc, ACE_TCHAR *argv[])
{
ACE_Service_Config daemon;
daemon.open (argv[0]);
parse_args (argc, argv);
if (child)
{
worker (n_iterations);
ACE_OS::exit (exit_code);
}
ACE_DEBUG ((LM_DEBUG,
"(%P|%t@%T) Process_Manager test. Expect output from"
"2 or 3 processes...\n"));
ACE_Process_Manager::instance ()->register_handler
(new ExitHandler ("default"));
pid_t pid1 = respawn_self (argv[0],
n_iterations,
111);
pid_t pid2 = respawn_self (argv[0],
n_iterations + 500,
222);
#if !defined (ACE_WIN32)
pid_t pid3 = ACE_OS::fork ();
if (!pid3)
{
worker (n_iterations);
return 999;
}
#endif /* ACE_WIN32 */
ACE_Process_Manager::instance ()->register_handler (new ExitHandler ("specific"),
pid2);
if (pid1 == ACE_INVALID_PID || pid2 == ACE_INVALID_PID)
ACE_ERROR_RETURN ((LM_ERROR,
"(%P|%t) %p\n",
"start_n"),
1);
ACE_DEBUG ((LM_DEBUG,
"(%P|%t@%T) Test parent waiting (synchronously, "
"up to 6 seconds) for children...\n"));
int result =
ACE_Process_Manager::instance ()->wait (ACE_Time_Value (6));
ACE_DEBUG ((LM_DEBUG,
"(%P|%t@%T) Test parent: %d processes left\n",
result));
if (result > 0)
{
ACE_DEBUG ((LM_DEBUG,
"(%P|%t@%T) Test parent waiting (synchronously, "
"indefinitely) for remaining children...\n"));
result =
ACE_Process_Manager::instance ()->wait ();
ACE_DEBUG ((LM_DEBUG,
"(%P|%t@%T) Test parent finished waiting: %d\n",
result));
}
ACE_DEBUG ((LM_DEBUG,
"(%P|%t@%T) Test parent: try auto-reap functions\n"));
ACE_Process_Manager::instance ()->open (ACE_Process_Manager::DEFAULT_SIZE,
ACE_Reactor::instance ());
pid1 = respawn_self (argv[0],
n_iterations + 200,
333 );
pid2 = respawn_self (argv[0],
n_iterations + 500,
444);
#if !defined (ACE_WIN32)
pid3 = ACE_OS::fork ();
if (!pid3)
{
worker (n_iterations);
return 888;
}
#endif /* ACE_WIN32 */
ExitHandler *main_thread_work = 0;
ACE_NEW_RETURN (main_thread_work,
ExitHandler ("main thread worker"),
1);
ACE_Reactor::instance ()->schedule_timer (main_thread_work,
0,
ACE_Time_Value (2),
ACE_Time_Value (1, 500000));
ACE_DEBUG ((LM_DEBUG,
"(%P|%t@%T) Test parent: expect several Processes "
"to be auto-detected over the next 30 seconds.\n"
"The main thread will do some other work, too.\n" ));
ACE_Time_Value briefly (30);
result = ACE_Reactor::run_event_loop (briefly);
ACE_DEBUG ((LM_DEBUG,
"(%P|%t@%T) Test parent: finished (%d) %d.%d. Close"
"Process_Manager...\n",
result,
briefly.sec (),
briefly.usec ()));
ACE_Process_Manager::instance ()->close ();
return 0;
}
|