📄 process.cpp
字号:
// process.cpp,v 4.21 2003/11/13 18:45:50 dhinton Exp
// ============================================================================
//
// = LIBRARY
// examples
//
// = FILENAME
// process.cpp
//
// = DESCRIPTION
// This example tests the <ACE_Process>. For more info, check the
// README file in this directory.
//
// = AUTHOR
// Tim Harrison <harrison@cs.wustl.edu>.
//
// ============================================================================
#include "ace/OS_NS_errno.h"
#include "ace/Get_Opt.h"
#include "ace/Process.h"
#include "ace/Log_Msg.h"
#include "ace/OS_NS_stdio.h"
ACE_RCSID(Process, process, "process.cpp,v 4.21 2003/11/13 18:45:50 dhinton Exp")
#if defined (ACE_WIN32)
#define EXEC_NAME "MORE.COM"
const char *DATE_PATH = "date.exe";
const char *LS_PATH = "ls.exe";
const char *SLEEP_PATH = "sleep.exe";
#else
#define EXEC_NAME "less"
const char *DATE_PATH = "date";
const char *LS_PATH = "ls";
const char *SLEEP_PATH = "sleep";
#endif /* ACE_WIN32 */
static const char *executable = EXEC_NAME;
static char *print_file = 0;
static char *environment_string = 0;
static int get_env = 0;
static int run_date = 0;
static int run_ls = 0;
static int run_all = 0;
static int run_setenv = 0;
static int run_tokenizer = 0;
static int run_wait = 0;
// Parse the command-line arguments and set options.
static int
parse_args (int argc, char **argv)
{
ACE_Get_Opt get_opt (argc, argv, "dlx:p:e:gastuw");
int c;
while ((c = get_opt ()) != -1)
{
switch (c)
{
case 't':
run_tokenizer = 1;
break;
case 's':
run_setenv = 1;
break;
case 'a':
run_all = 1;
break;
case 'd':
run_date = 1;
break;
case 'l':
run_ls = 1;
break;
case 'x':
executable = get_opt.opt_arg ();
break;
case 'p':
print_file = get_opt.opt_arg ();
break;
case 'e':
environment_string = get_opt.opt_arg ();
break;
case 'g':
get_env = 1;
break;
case 'w':
run_wait = 1;
break;
case 'u':
default:
ACE_ERROR_RETURN ((LM_ERROR, "Usage:\n"
"-d print date\n"
"-l run ls\n"
"-x <executable=more.com>\n"
"-p print <file_name>\n"
"-e <env variable message>\n"
"-s setenv ACE_PROCESS_ENV and spawn -g\n"
"-g get_env ACE_PROCESS_ENV\n"
"-t test tokenizer\n"
"-w test wait functions\n"
"-a run all (d,l,e \"running\")\n"), -1);
break;
}
}
return 0;
}
// This shows how to set handles.
static void
test_more (void)
{
ACE_HANDLE infile = ACE_OS::open (print_file, O_RDONLY);
if (infile == ACE_INVALID_HANDLE)
{
ACE_ERROR ((LM_DEBUG, "%p\n", print_file));
return;
}
ACE_Process new_process;
ACE_Process_Options options;
options.command_line (executable);
options.set_handles (infile);
if (new_process.spawn (options) == -1)
{
int error = ACE_OS::last_error ();
ACE_ERROR ((LM_ERROR,
"%p errno = %d.\n",
"test_more",
error));
}
ACE_exitcode status;
new_process.wait (&status);
ACE_DEBUG ((LM_DEBUG,
"Process exit with status %d\n",
status));
ACE_OS::close (infile);
ACE_DEBUG ((LM_DEBUG,
"More succeeded.\n"));
}
// This is a simple usage of ACE_Process.
static void
test_date (void)
{
ACE_Process_Options options;
options.command_line (DATE_PATH);
// Try to create a new process running date.
ACE_Process new_process;
if (new_process.spawn (options) == -1)
{
int error = ACE_OS::last_error ();
ACE_ERROR ((LM_ERROR,
"%p errno = %d.\n",
"test_date",
error));
return;
}
ACE_exitcode status;
new_process.wait (&status);
ACE_DEBUG ((LM_DEBUG,
"Process exit with status %d\n",
status));
ACE_DEBUG ((LM_DEBUG,
"date succeeded.\n"));
}
static void
test_ls (void)
{
ACE_Process_Options options;
options.command_line ("%s -al", LS_PATH);
ACE_Process new_process;
if (new_process.spawn (options) == -1)
{
int error = ACE_OS::last_error ();
ACE_ERROR ((LM_ERROR,
"%p errno = %d.\n",
"test_ls",
error));
}
ACE_exitcode status;
new_process.wait (&status);
ACE_DEBUG ((LM_DEBUG,
"Process exit with status %d\n",
status));
}
static void
test_wait (void)
{
ACE_Process_Options options;
options.command_line ("%s 10", SLEEP_PATH);
ACE_Process process1;
if (process1.spawn (options) == -1)
{
int error = ACE_OS::last_error ();
ACE_ERROR ((LM_ERROR,
"%p errno = %d.\n",
"test_ls",
error));
}
int result;
ACE_exitcode status;
ACE_DEBUG ((LM_DEBUG,
"[%T] New process sleeping 10; try wait(2)\n"));
result = process1.wait (ACE_Time_Value (2),
&status);
ACE_DEBUG ((LM_DEBUG,
"[%T] wait(2) returns %d(%d)...now try regular wait\n",
result,
status));
result = process1.wait (&status);
ACE_DEBUG ((LM_DEBUG,
"[%T] wait() returns %d(%d)\n",
result,
status));
ACE_Process process2;
if (process2.spawn (options) == -1)
{
int error = ACE_OS::last_error ();
ACE_ERROR ((LM_ERROR,
"%p errno = %d.\n",
"test_ls",
error));
}
ACE_DEBUG ((LM_DEBUG,
"[%T] New process sleeping 10; try wait(12)\n",
status));
result = process2.wait (ACE_Time_Value (12),
&status);
ACE_DEBUG ((LM_DEBUG,
"[%T] wait(12) returns %d(%d)...now try regular wait\n",
result,
status));
result = process2.wait (&status);
ACE_DEBUG ((LM_DEBUG,
"[%T] wait returns %d(%d)\n",
result,
status));
}
#if defined (ACE_WIN32)
// This is just to test the direct usage of CreateProcess. I use this
// occasionally as a sanity check when ACE_Process breaks.
static void
win32_test_ls (void)
{
PROCESS_INFORMATION process_info;
ACE_TEXT_STARTUPINFO startup_info;
ACE_OS::memset ((void *) &startup_info,
0,
sizeof startup_info);
ACE_OS::memset ((void *) &process_info,
0,
sizeof process_info);
startup_info.cb = sizeof startup_info;
startup_info.dwFlags = STARTF_USESTDHANDLES;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -