C++; 我有两个C++程序:程序1和程序2。我想做的是让Program1运行它的算法来计算它需要的任何东西,然后将所有计算出的信息导入Program2,让它使用Program1的输出运行它的算法

C++; 我有两个C++程序:程序1和程序2。我想做的是让Program1运行它的算法来计算它需要的任何东西,然后将所有计算出的信息导入Program2,让它使用Program1的输出运行它的算法,c++,subprocess,pipe,C++,Subprocess,Pipe,如果我可以通过管道传输信息并关闭Program1,而不必等待Program2首先完成,那就太好了。这与python中的类似。您需要按照以下思路执行操作: #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]

如果我可以通过管道传输信息并关闭Program1,而不必等待Program2首先完成,那就太好了。这与python中的类似。

您需要按照以下思路执行操作:

#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;
}
#包括
int main(){
//为程序1做一些事情
int-pipefds[2];
if(管道(管道)
投掷1枚;
//使用“写入”将二进制数据发送到pipefds[0]
dup2(pipefds[1],0);
execl(/*将所需的program2参数放在此处。*/);
返回1;
}

有了这个,你所需要的就是让program2从stdin中读取所有必要的数据,你就完成了

要模拟
子流程。请检查调用(“Program1 | Program2”,shell=True)
Python调用,您可以使用:

报告错误和退出、关闭、重定向可定义为:

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>

#define Close(FD) do {                                          \
    const int Close_fd = (FD);                                  \
    if (close(Close_fd) == -1)                                  \
      fprintf(stderr, "%s:%d: close(" #FD ") %d: %s\n",         \
          __FILE__, __LINE__, Close_fd, strerror(errno));       \
  }while(0)

#define Report_error_and_exit(msg) do {                       \
    fprintf(stderr, "%s:%d: %s: %s\n", __FILE__, __LINE__,    \
        (msg), strerror(errno));                              \
    (is_child ? _exit : exit)(EXIT_FAILURE);                  \
  } while(0)
static int is_child = 0;

#define Redirect(FROM, TO) do {            \
    const int from = (FROM);               \
    const int to = (TO);                   \
    if (from != to) {                      \
      if (dup2(from, to) == -1)            \
        Report_error_and_exit("dup2");     \
      else                                 \
        Close(from);                       \
    }                                      \
  } while(0)
#包括
#包括
#包括
#包括
#包括
#定义关闭(FD)do{\
常数int Close_fd=(fd)\
如果(关闭(关闭\u fd)=-1)\
fprintf(标准,“%s:%d:close(#FD”)%d:%s\n”\
__文件,行,关闭fd,strerror(errno))\
}而(0)
#定义报告错误和退出(msg)do{\
fprintf(标准文件,“%s:%d:%s:%s\n”、\uuuuuu文件、\uuuuu行、\
(msg)、strerror(errno))\
(是否为子项?_退出:退出)(退出失败)\
}而(0)
静态int为_child=0;
#定义重定向(从、到)do{\
常量int from=(from)\
常数int to=(to)\
如果(从!=到){\
if(dup2(从,到)=-1)\
报告错误和退出(“dup2”)\
否则\
关闭(从)\
}                                      \
}而(0)

您可能会发现这很有用。虽然我没怎么说,但我从来没用过。如果解决方案不必是可移植的,那么好的旧
fork
(甚至是
系统
)也可以。您可能会发现有用的功能有:
fork
exec
dup2
write
read
。使用这些功能听起来会满足您的需要。它比pipe/fork/dup2/exec组合更易于使用。是否要模拟shell管道
Program1 | Program2
?这里
Program1
可以在写入结果后立即退出。整个管道等待
Program2
退出。您可以将管道放在后台,这样就可以在同一个shell中运行其他命令,而无需等待
Program2
完成。您需要它吗?
dup2(pipefds[1],0)
到底做什么。。。?
/** $ gcc simple-pipe.c && ./a.out */
#include <sys/types.h> /* pid_t */
#include <unistd.h>

int main(void) {
  int fd[2]; /* pipe ends */
  pid_t pid = -1;

  if (pipe(fd) == -1)
    Report_error_and_exit("pipe");

  if ((pid = fork()) == -1)
    Report_error_and_exit("fork");
  else if (pid == 0)  {
    /* child: run Program1, redirecting stdout to the pipe */
    is_child = 1;
    Close(fd[0]); /* close unused read end of the pipe */

    /* redirect stdout */
    Redirect(fd[1], STDOUT_FILENO);

    /* run Program1 with redirected stdout */
    execlp("Program1", "Program1", NULL);
    Report_error_and_exit("execlp");
  }

  /* parent: run Program2, redirecting stdin to the pipe */
  Close(fd[1]); /* close unused write end of the pipe */

  /* redirect stdin */
  Redirect(fd[0], STDIN_FILENO);
  /* run Program2 with redirected stdin */
  execlp("Program2", "Program2", NULL);
  Report_error_and_exit("execlp");
}
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>

#define Close(FD) do {                                          \
    const int Close_fd = (FD);                                  \
    if (close(Close_fd) == -1)                                  \
      fprintf(stderr, "%s:%d: close(" #FD ") %d: %s\n",         \
          __FILE__, __LINE__, Close_fd, strerror(errno));       \
  }while(0)

#define Report_error_and_exit(msg) do {                       \
    fprintf(stderr, "%s:%d: %s: %s\n", __FILE__, __LINE__,    \
        (msg), strerror(errno));                              \
    (is_child ? _exit : exit)(EXIT_FAILURE);                  \
  } while(0)
static int is_child = 0;

#define Redirect(FROM, TO) do {            \
    const int from = (FROM);               \
    const int to = (TO);                   \
    if (from != to) {                      \
      if (dup2(from, to) == -1)            \
        Report_error_and_exit("dup2");     \
      else                                 \
        Close(from);                       \
    }                                      \
  } while(0)