-2

There is a python program which is not installed in system's default path. How do I call a program in C++ similar to ProcessBuilder's functionality in Java.

What is the equivalent of the below in C++

ProcessBuilder pb = new ProcessBuilder("python3","software","param");
pb.inheritIO();
Process ps = pb.start();

if(ps.exitValue()==0) {
    System.out.println("Process executed successfully");
}
Tarun Maganti
  • 3,076
  • 2
  • 35
  • 64
  • What does process builder do? – JVApen Aug 08 '19 at 11:27
  • There is no portable equivalent of the Java's `inheritIO()` functionality in C++. `system()` will ignore program output entirely. The POSIX `popen()` can be used to pump stdin and stdout "manually" (in two threads or using async read/write with poll/select), but `popen` doesn't support stderr. [`boost::process`](https://www.boost.org/doc/libs/1_70_0/doc/html/boost/process/child.html) does support stderr, so you can try that. – rustyx Aug 08 '19 at 11:48

1 Answers1

1

https://en.cppreference.com/w/cpp/utility/program/system

int wstatus = std::system("python3 software param");

The return value is an implementation-defined value. On Posix systems it contains a lot of information that can be extracted using the following macros:

WIFEXITED(wstatus)
          returns true if the child terminated normally, that is,  by  calling  exit(3)  or
          _exit(2), or by returning from main().

WEXITSTATUS(wstatus)
          returns  the  exit status of the child.  This consists of the least significant 8
          bits of the status argument that the child specified in  a  call  to  exit(3)  or
          _exit(2)  or as the argument for a return statement in main().  This macro should
          be employed only if WIFEXITED returned true.

WIFSIGNALED(wstatus)
          returns true if the child process was terminated by a signal.

WTERMSIG(wstatus)
          returns the number of the signal that caused  the  child  process  to  terminate.
          This macro should be employed only if WIFSIGNALED returned true.

WCOREDUMP(wstatus)
          returns  true  if  the child produced a core dump.  This macro should be employed
          only if WIFSIGNALED returned true.

          This macro is not specified in POSIX.1-2001 and is not  available  on  some  UNIX
          implementations  (e.g.,  AIX,  SunOS).   Therefore, enclose its use inside #ifdef
          WCOREDUMP ... #endif.

WIFSTOPPED(wstatus)
          returns true if the child process was stopped by delivery of a  signal;  this  is
          possible  only  if  the  call was done using WUNTRACED or when the child is being
          traced (see ptrace(2)).

WSTOPSIG(wstatus)
          returns the number of the signal which caused the  child  to  stop.   This  macro
          should be employed only if WIFSTOPPED returned true.

WIFCONTINUED(wstatus)
          (since Linux 2.6.10) returns true if the child process was resumed by delivery of
          SIGCONT.
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108