C ++中的子流程命令

理查德

我有两个C ++程序:Program1和Program2。我要做的是让Program1运行其算法以计算所需的内容,然后将所有计算出的信息通过管道传输到Program2中,以使其使用Program1的输出来运行其算法。

如果我可以通过管道传输信息并关闭Program1,而不必等待Program2首先完成,那就太好了。就像python中subprocess.call()一样

随机用户名

您将需要执行以下操作:

#include <unistd.h>

int main () {
  // Do stuff for program1
  int pipefds[2];
  if (pipe (pipefds))
    throw 1;
  // Use ``write'' to send binary data to pipefds[0]
  dup2 (pipefds[1], 0);
  execl (/* Put the program2 arguments you want here. */);
  return 1;
}

这样,您所需要做的就是让program2从stdin中读取所有必要的数据,然后就完成了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章