23,110
社区成员
发帖
与我相关
我的任务
分享
/**
* @file factorial.cpp
* @brief
*/
#include "ace/OS.h"
#include "ace/ACE.h"
#include "ace/Process.h"
int main(int argc, char *argv[])
{
ACE_Process_Options options;
FILE *fp = NULL;
char *n_env = NULL;
int n;
if (argc == 1) {
// Top-level process.
n_env = ACE_OS::getenv ("FACTORIAL");
n = n_env == 0 ? 0 : atoi (n_env);
options.command_line ("%s %d", argv[0], n == 0 ? 10 : n);
const char *working_dir = ACE_OS::getenv ("WORKING_DIR");
if (working_dir) options.working_directory (working_dir);
fp = fopen ("factorial.log", "a");
options.setenv ("PROGRAM=%s", ACE::basename (argv[0]));
} else {
fp = fopen ("factorial.log", "a");
if (atoi (argv[1]) == 1) {
// Base case
fprintf (fp, "[%s|%d]: base case\n",
ACE_OS::getenv ("PROGRAM"), ACE_OS::getpid ());
fclose (fp);
return 1;
} else {
n = atoi (argv[1]);
options.command_line ("%s %d", argv[0], n - 1);
}
}
ACE_Process child;
// Make ''recursive'' call.
child.spawn (options);
child.wait ();
int factorial = n * child.exit_code (); // Compute n factorial
fprintf (fp, "[%s | %d]: %d! == %d\n",
ACE_OS::getenv ("PROGRAM"), ACE_OS::getpid (), n, factorial);
fclose (fp);
return 0;
}
int ACE_Process_Options::setenv (const ACE_TCHAR *format, ...),
int ACE_Process_Options::setenv (const ACE_TCHAR *variable_name,
const ACE_TCHAR *format, ...)
[/quote]char buf[128] = {0};
snprintf(buf, 128, "PROGRAM=%s", ACE::basename(argv[0]));
options.setenv(buf);
int ACE_Process_Options::setenv (const ACE_TCHAR *format, ...),
int ACE_Process_Options::setenv (const ACE_TCHAR *variable_name,
const ACE_TCHAR *format, ...)