|
| 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