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 (void)
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 # if defined (ACE_WIN32) || !defined (ACE_USES_WCHAR)
117 const ACE_TCHAR
*cmdline_fmt
= ACE_TEXT ("%s -c%s");
119 const ACE_TCHAR
*cmdline_fmt
= ACE_TEXT ("%ls -c%ls");
120 # endif /* ACE_WIN32 || !ACE_USES_WCHAR */
121 ACE_Process_Options options
;
122 # ifndef ACE_LACKS_VA_FUNCTIONS
123 options
.command_line (cmdline_fmt
,
124 argc
> 0 ? argv
[0] : ACE_TEXT ("Pipe_Test"),
125 close_pipe
== 0 ? ACE_TEXT (" -d") : ACE_TEXT (""));
127 ACE_UNUSED_ARG (cmdline_fmt
);
130 ACE_exitcode status
= 0;
132 for (int i
= 0; i
< ::iterations
; i
++)
136 if (server
.spawn (options
) == -1)
138 ACE_ERROR_RETURN ((LM_ERROR
,
140 ACE_TEXT ("spawn failed")),
145 ACE_DEBUG ((LM_DEBUG
,
146 ACE_TEXT ("Server forked with pid = %d.\n"),
150 // Wait for the process we just created to exit.
151 server
.wait (&status
);
153 // Check if child exited without error.
154 if (WIFEXITED (status
) != 0
155 && WEXITSTATUS (status
) != 0)
157 ACE_ERROR ((LM_ERROR
,
158 ACE_TEXT ("Child of server %d finished with error ")
159 ACE_TEXT ("exit status %d\n"),
161 WEXITSTATUS (status
)));
165 ACE_OS::exit (WEXITSTATUS (status
));
168 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Server %d finished\n"),
173 #endif // ACE_HAS_PROCESS_SPAWN