【问题标题】:What is ProcessBuilder of Java's counterpart in C++?C++ 中 Java 对应的 ProcessBuilder 是什么?
【发布时间】:2019-12-16 02:02:42
【问题描述】:

有一个python程序没有安装在系统的默认路径中。如何在 C++ 中调用类似于 Java 中 ProcessBuilder 的功能的程序。

C++中下面的等价物是什么

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

if(ps.exitValue()==0) {
    System.out.println("Process executed successfully");
}

【问题讨论】:

  • 流程构建器是做什么的?
  • C++ 中没有与 Java 的 inheritIO() 功能等效的可移植版本。 system() 将完全忽略程序输出。 POSIX popen() 可用于“手动”抽取标准输入和标准输出(在两个线程中或通过轮询/选择使用异步读/写),但 popen 不支持标准错误。 boost::process 确实支持stderr,所以你可以试试。

标签: java c++ command pipe processbuilder


【解决方案1】:

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

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

返回值是实现定义的值。在 Posix 系统上,它包含许多可以使用以下宏提取的信息:

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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 2013-07-28
    相关资源
    最近更新 更多