10

Its been a while since I've had to do this and in the past I've used "spawn" to create processes.

Now I want to launch processes from my application asynchronously so my application continues to execute in the background and does not get held up by launching the process.

I also want to be able to communicate with the launched processes. When I launch the process I will send it the launchers process id so that the launched process can communicate with the launcher using it's pid.

What is the best method to use that is not specific to any platform / operating system, I'm looking for a solution that is multi-platform?

I'm writing this in C++, I don't want a solution that ties me to any third party licensed product.

I don't want to use threads, the solution must be for creating new processes.

SPlatten
  • 5,334
  • 11
  • 57
  • 128
  • is communication with pid necessary ? – iyasar Apr 03 '19 at 08:21
  • @iyasar, what else would you suggest, I was thinking that since the parent knows its own pid and can pass this onto the newly launched process that this would be a simple way for both to communicate. – SPlatten Apr 03 '19 at 08:23
  • why dont you think std::thread with conncurrent data structures ? – iyasar Apr 03 '19 at 08:23
  • 3
    @iyasar, because as I said in the post I want seperate processes NOT threads. – SPlatten Apr 03 '19 at 08:24
  • 1
    There's no standard way of creating process among operating system. You have to use spawn/exec/fork (or any wrapper around them) on unix and I don't know what on windows. – Gojita Apr 03 '19 at 08:24
  • 1
    Boost.Process if you want cross platform support. – Shawn Apr 03 '19 at 08:25
  • @Gojita, I know there are multiple ways of doing the same thing, what I'm after is a recommendation of the best way. – SPlatten Apr 03 '19 at 08:26
  • 1
    Requests for tools/libraries are off topic here. – xaxxon Apr 03 '19 at 08:27
  • @xaxxon, how is it off topic? The point of this forum is to get support on an software coding issue, which this is. – SPlatten Apr 03 '19 at 08:29
  • 1
    **The C++11 standard does not know about processes.** Check by reading [n3337](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf). Some framework libraries ([Boost](http://boost.org/), [POCO](http://pocoproject.org/), [Qt](http://qt.io/) ...) know them. – Basile Starynkevitch Apr 03 '19 at 08:31
  • 1
    It's literlly in the list of off topic reasons. – xaxxon Apr 03 '19 at 08:34
  • Have you looked at openMPI ? – Clonk Apr 03 '19 at 08:40
  • I downloaded boost form the web-site, but sorry to say that the download does not contain anything that is ready to use, it makes reference to headers that do not exist in the download. – SPlatten Apr 03 '19 at 18:48

3 Answers3

5

Try Boost.Process.

Boost.Process provides a flexible framework for the C++ programming language to manage running programs, also known as processes. It empowers C++ developers to do what Java developers can do with java.lang.Runtime/java.lang.Process and .NET developers can do with System.Diagnostics.Process. Among other functionality, this includes the ability to manage the execution context of the currently running process, the ability to spawn new child processes, and a way to communicate with them them using standard C++ streams and asynchronous I/O.

The library is designed in a way to transparently abstract all process management details to the user, allowing for painless development of cross-platform applications. However, as such abstractions often restrict what the developer can do, the framework allows direct access to operating system specific functionality - obviously losing the portability features of the library.

Example code to run and wait to finish for child process from the site:

bp::child c(bp::search_path("g++"), "main.cpp");

while (c.running())
    do_some_stuff();

c.wait(); //wait for the process to exit   
int result = c.exit_code();
Community
  • 1
  • 1
Oliort UA
  • 1,568
  • 1
  • 14
  • 31
  • 1
    I just downloaded the latest from the web-site, its not a great start when the headers themselves are full of invalid path references, these are simple enough to fix...also the namespace ::boost does not appear t be defined. There are so many errors I'm not sure what the intention was. – SPlatten Apr 03 '19 at 18:38
  • @SPlatten where (what site?) and what did you install (built from sources / downloaded binaries / etc) and on which platform do you have problems? – Oliort UA Apr 03 '19 at 19:29
  • I clicked on the download from the boost site posted in your link. I’m working on a Mac. – SPlatten Apr 03 '19 at 19:31
  • @Oliort that page seems to be a 10 year old version of the code, long since outdated. Why not link to the updated version now it's officially included in Boost? – Jonathan Wakely Apr 03 '19 at 19:32
  • 1
    @SPlatten that's not "the boost site" though. https://www.boost.org/doc/libs/release/doc/html/process.html is the latest version and it's part of the normal Boost releases. – Jonathan Wakely Apr 03 '19 at 19:32
  • Sorry guys,just googled quickly and pasted, shouldn't hurry. Edited the link from @JonathanWakely comment. – Oliort UA Apr 03 '19 at 19:43
  • So where is the download link now?...Found it https://dl.bintray.com/boostorg/release/1.69.0/source/ – SPlatten Apr 03 '19 at 20:06
3

Warning: calling std::system() is generally a bad idea.

A portable to launch a new process is std::system.

#include <cstdlib>

int main() {
   std::system("./myapp");
   return 0;
}

if you use linux and you want to share handles/memory between processes, fork is what you are looking for

Timmmm
  • 88,195
  • 71
  • 364
  • 509
cprogrammer
  • 5,503
  • 3
  • 36
  • 56
  • 3
    That fails the _"launch processes from my application asynchronously so my application continues to execute in the background and does not get held up by launching the process"_ requirement. – Jonathan Wakely Apr 03 '19 at 08:42
  • std::system usually returns the value that the invoked program returns, so it waits for program to finish. – Oliort UA Apr 03 '19 at 08:44
1

I'll plug my own little (single header) library:

PStreams allows you to run another program from your C++ application and to transfer data between the two programs similar to shell pipelines.

In the simplest case, a PStreams class is like a C++ wrapper for the POSIX.2 functions popen(3) and pclose(3), using C++ iostreams instead of C's stdio library.

The library provides class templates in the style of the standard iostreams that can be used with any ISO C++ compiler on a POSIX platform. The classes use a streambuf class that uses fork(2) and the exec(2) family of functions to create a new process and creates up to three pipes to write/read data to/from the process.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • how does "on a posix platform" fit "not specific to any platform"? – xaxxon Apr 03 '19 at 08:37
  • @xaxxon I missed that part, but since when is "posix" a specific platform? :-P There's [a fork of pstreams for Windows](https://sourceforge.net/p/pstreams/code/ci/win32-branch/tree/pstream.h) (but it's unsupported and unmaintained). – Jonathan Wakely Apr 03 '19 at 08:40