Skip to content

Commit 3ff43f5

Browse files
committed
Adding process example
1 parent 979b71b commit 3ff43f5

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

components/process/examples/CMakeLists.txt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019 The STE||AR-Group
1+
# Copyright (c) 2024 The STE||AR-Group
22
#
33
# SPDX-License-Identifier: BSL-1.0
44
# Distributed under the Boost Software License, Version 1.0. (See accompanying
@@ -13,4 +13,30 @@ if(HPX_WITH_EXAMPLES)
1313
tests.examples.components tests.examples.components.process
1414
)
1515
endif()
16+
else()
17+
return()
1618
endif()
19+
20+
set(example_programs launch_command)
21+
set(launch_command_FLAGS DEPENDENCIES process_component)
22+
23+
foreach(example_program ${example_programs})
24+
set(sources ${example_program}.cpp)
25+
26+
source_group("Source Files" FILES ${sources})
27+
28+
# add example executable
29+
add_hpx_executable(
30+
${example_program} INTERNAL_FLAGS
31+
SOURCES ${sources} ${${example_program}_FLAGS}
32+
FOLDER "Examples/Components/Process"
33+
)
34+
35+
add_hpx_example_target_dependencies("components.process" ${example_program})
36+
37+
if(HPX_WITH_TESTS AND HPX_WITH_TESTS_EXAMPLES)
38+
add_hpx_example_test(
39+
"components.process" ${example_program} ${${example_program}_PARAMETERS}
40+
)
41+
endif()
42+
endforeach()
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright (c) 2024 Hartmut Kaiser
2+
//
3+
// SPDX-License-Identifier: BSL-1.0
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
7+
// This example demonstrates how the Process component can be used to launch
8+
// arbitrary commands on any of the participating localities.
9+
10+
#include <hpx/hpx_main.hpp>
11+
#include <hpx/include/components.hpp>
12+
#include <hpx/include/process.hpp>
13+
#include <hpx/include/runtime.hpp>
14+
15+
#include <iostream>
16+
#include <vector>
17+
18+
inline int get_arraylen(char** arr)
19+
{
20+
int count = 0;
21+
if (nullptr != arr)
22+
{
23+
while (nullptr != arr[count])
24+
++count; // simply count the strings
25+
}
26+
return count;
27+
}
28+
29+
std::vector<std::string> get_environment()
30+
{
31+
std::vector<std::string> env;
32+
#if defined(HPX_WINDOWS)
33+
int len = get_arraylen(_environ);
34+
std::copy(&_environ[0], &_environ[len], std::back_inserter(env));
35+
#elif defined(linux) || defined(__linux) || defined(__linux__) || \
36+
defined(__AIX__) || defined(__APPLE__) || defined(__FreeBSD__)
37+
int len = get_arraylen(environ);
38+
std::copy(&environ[0], &environ[len], std::back_inserter(env));
39+
#else
40+
#error "Don't know, how to access the execution environment on this platform"
41+
#endif
42+
return env;
43+
}
44+
45+
int main()
46+
{
47+
namespace process = hpx::components::process;
48+
49+
// use hpx::find_all_localities(); if you want to include the current
50+
// locality as well
51+
std::vector<hpx::id_type> localities = hpx::find_remote_localities();
52+
std::vector<process::child> children;
53+
children.reserve(localities.size());
54+
55+
for (auto const& locality : localities)
56+
{
57+
#if defined(HPX_WINDOWS)
58+
std::string exe = "cmd.exe";
59+
#else
60+
std::string exe = "ls";
61+
#endif
62+
63+
// set up command line for launched executable
64+
std::vector<std::string> args;
65+
args.push_back(exe);
66+
#if defined(HPX_WINDOWS)
67+
args.push_back("/c");
68+
args.push_back("dir");
69+
#endif
70+
71+
// set up environment for launched executable (propagate current env)
72+
std::vector<std::string> env = get_environment();
73+
74+
// launch test executable
75+
process::child c = process::execute(locality, process::run_exe(exe),
76+
process::set_args(args), process::set_env(env),
77+
process::start_in_dir("."), process::throw_on_error());
78+
79+
children.push_back(c);
80+
}
81+
82+
// wait for the processes to start executing
83+
hpx::wait_all(children);
84+
85+
// wait for the processes to terminate
86+
for (auto& c : children)
87+
{
88+
int retval = c.wait_for_exit(hpx::launch::sync);
89+
std::cout << retval << '\n';
90+
}
91+
92+
return 0;
93+
}

0 commit comments

Comments
 (0)