2 //=============================================================================
6 * Tests the construction of multiple pipes in a process.
8 * @author Irfan Pyarali <irfan@cs.wustl.edu>
10 //=============================================================================
13 #include "test_config.h"
15 #include "ace/Process.h"
16 #include "ace/Get_Opt.h"
18 #include "ace/OS_NS_stdio.h"
19 #include "ace/OS_NS_stdlib.h"
20 #include "ace/OS_NS_unistd.h"
22 // Indicates whether we should close the pipe or not.
23 static int close_pipe
= 1;
25 // Indicates whether we're running as the child or the parent.
26 static int child_process
= 0;
28 // Number of iterations to run the test.
29 static int iterations
= ACE_MAX_ITERATIONS
;
31 // Explain usage and exit.
33 print_usage_and_die ()
36 ACE_TEXT ("usage: %n [-d (don't close pipes)] ")
37 ACE_TEXT ("[-c (child process)] [-i (iterations)]\n")));
41 // Parse the command-line arguments and set options.
44 parse_args (int argc
, ACE_TCHAR
*argv
[])
46 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT("dci:"));
50 while ((c
= get_opt ()) != -1)
60 iterations
= ACE_OS::atoi (get_opt
.opt_arg ());
63 print_usage_and_die ();
68 // Consolidate the ACE_Pipe initializations.
70 #ifdef ACE_HAS_PROCESS_SPAWN
72 open_pipe (ACE_Pipe
&pipe
,
75 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("opening %C\n"), name
));
76 int result
= pipe
.open ();
78 ACE_TEST_ASSERT (result
!= -1);
79 result
= pipe
.read_handle () != ACE_INVALID_HANDLE
80 && pipe
.write_handle () != ACE_INVALID_HANDLE
;
81 ACE_TEST_ASSERT (result
== 1);
89 run_main (int argc
, ACE_TCHAR
*argv
[])
91 parse_args (argc
, argv
);
93 #ifndef ACE_HAS_PROCESS_SPAWN
94 ACE_START_TEST (ACE_TEXT ("Pipe_Test"));
100 ACE_APPEND_LOG (ACE_TEXT("Pipe_Test-children"));
101 ACE_Pipe a
, b
, c
, d
, e
;
113 ACE_START_TEST (ACE_TEXT("Pipe_Test"));
114 ACE_INIT_LOG (ACE_TEXT("Pipe_Test-children"));
116 ACE_Process_Options options
;
117 # ifndef ACE_LACKS_VA_FUNCTIONS
118 options
.command_line (ACE_TEXT ("%") ACE_TEXT_PRIs
119 ACE_TEXT (" -c%") ACE_TEXT_PRIs
,
120 argc
> 0 ? argv
[0] : ACE_TEXT ("Pipe_Test"),
121 close_pipe
== 0 ? ACE_TEXT (" -d") : ACE_TEXT (""));
123 ACE_UNUSED_ARG (cmdline_fmt
);
126 ACE_exitcode status
= 0;
128 for (int i
= 0; i
< ::iterations
; i
++)
132 if (server
.spawn (options
) == -1)
134 ACE_ERROR_RETURN ((LM_ERROR
,
136 ACE_TEXT ("spawn failed")),
141 ACE_DEBUG ((LM_DEBUG
,
142 ACE_TEXT ("Server forked with pid = %d.\n"),
146 // Wait for the process we just created to exit.
147 server
.wait (&status
);
149 // Check if child exited without error.
150 if (WIFEXITED (status
) != 0
151 && WEXITSTATUS (status
) != 0)
153 ACE_ERROR ((LM_ERROR
,
154 ACE_TEXT ("Child of server %d finished with error ")
155 ACE_TEXT ("exit status %d\n"),
157 WEXITSTATUS (status
)));
161 ACE_OS::exit (WEXITSTATUS (status
));
164 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Server %d finished\n"),
169 #endif // ACE_HAS_PROCESS_SPAWN