Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding process example #6515

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion components/process/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019 The STE||AR-Group
# Copyright (c) 2024 The STE||AR-Group
#
# SPDX-License-Identifier: BSL-1.0
# Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand All @@ -13,4 +13,30 @@ if(HPX_WITH_EXAMPLES)
tests.examples.components tests.examples.components.process
)
endif()
else()
return()
endif()

set(example_programs launch_command)
set(launch_command_FLAGS DEPENDENCIES process_component)

foreach(example_program ${example_programs})
set(sources ${example_program}.cpp)

source_group("Source Files" FILES ${sources})

# add example executable
add_hpx_executable(
${example_program} INTERNAL_FLAGS
SOURCES ${sources} ${${example_program}_FLAGS}
FOLDER "Examples/Components/Process"
)

add_hpx_example_target_dependencies("components.process" ${example_program})

if(HPX_WITH_TESTS AND HPX_WITH_TESTS_EXAMPLES)
add_hpx_example_test(
"components.process" ${example_program} ${${example_program}_PARAMETERS}
)
endif()
endforeach()
93 changes: 93 additions & 0 deletions components/process/examples/launch_command.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright (c) 2024 Hartmut Kaiser
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// This example demonstrates how the Process component can be used to launch
// arbitrary commands on any of the participating localities.

#include <hpx/hpx_main.hpp>
#include <hpx/include/components.hpp>
#include <hpx/include/process.hpp>
#include <hpx/include/runtime.hpp>

#include <iostream>
#include <vector>

inline int get_arraylen(char** arr)
{
int count = 0;
if (nullptr != arr)
{
while (nullptr != arr[count])
++count; // simply count the strings
}
return count;
}

std::vector<std::string> get_environment()
{
std::vector<std::string> env;
#if defined(HPX_WINDOWS)
int len = get_arraylen(_environ);
std::copy(&_environ[0], &_environ[len], std::back_inserter(env));
#elif defined(linux) || defined(__linux) || defined(__linux__) || \
defined(__AIX__) || defined(__APPLE__) || defined(__FreeBSD__)
int len = get_arraylen(environ);
std::copy(&environ[0], &environ[len], std::back_inserter(env));
#else
#error "Don't know, how to access the execution environment on this platform"
#endif
return env;
}

int main()
{
namespace process = hpx::components::process;

// use hpx::find_all_localities(); if you want to include the current
// locality as well
std::vector<hpx::id_type> localities = hpx::find_remote_localities();
std::vector<process::child> children;
children.reserve(localities.size());

for (auto const& locality : localities)
{
#if defined(HPX_WINDOWS)
std::string exe = "cmd.exe";
#else
std::string exe = "ls";
#endif

// set up command line for launched executable
std::vector<std::string> args;
args.push_back(exe);
#if defined(HPX_WINDOWS)
args.push_back("/c");
args.push_back("dir");
#endif

// set up environment for launched executable (propagate current env)
std::vector<std::string> env = get_environment();

// launch test executable
process::child c = process::execute(locality, process::run_exe(exe),
process::set_args(args), process::set_env(env),
process::start_in_dir("."), process::throw_on_error());

children.push_back(c);
}

// wait for the processes to start executing
hpx::wait_all(children);

// wait for the processes to terminate
for (auto& c : children)
{
int retval = c.wait_for_exit(hpx::launch::sync);
std::cout << retval << '\n';
}

return 0;
}
Loading