C pipe()在一定数量的调用后返回错误

edd*_* ed 1 c linux stdin stdout pipe

我写了这个函数来与外部程序进行通信.这样的程序从stdin获取输入并在stdout上输出它的输出.为了使我的代码与该程序通信,我使用管道将stdin和stdout重定向到缓冲区.

int query_oracle(mpz * c,int *t, mpz * m) {
  int out_pipe[2];
  int in_pipe[2];
  int saved_stdout;
  int saved_stdin;

  // REDIRECT STDIN
  saved_stdin = dup(STDIN_FILENO);      /* save stdin for later */
  pipe(in_pipe);            /* make a pipe */
  close(STDIN_FILENO);
  dup2(in_pipe[0], STDIN_FILENO);   /* redirect pipe to stdin */
  //write(in_pipe[1], in_buf, strlen(in_buf));

  // REDIRECT STDOUT
  saved_stdout = dup(STDOUT_FILENO);  /* save stdout for display later */
  if( pipe(out_pipe) != 0 ) {          /* make a pipe */
  exit(1);
  }
  dup2(out_pipe[1], STDOUT_FILENO);   /* redirect stdout to the pipe */
  close(out_pipe[1]);

  /* Some reads and writes on the pipes occur here 
   * so that the program can communicate with an 
   * external program*/

  dup2(saved_stdout, STDOUT_FILENO);    /* reconnect stdout */
  dup2(saved_stdin, STDIN_FILENO);  /* reconnect stdin */

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

问题是我第204次调用这个函数,pipe()给我一个错误(-1)!知道为什么会这样,或者我该如何避免呢?非常感谢

进一步的细节:这是在Linux上.uname -a的结果是:

 Linux snowy.*****.ac.uk 2.6.32-71.el6.x86_64 #1 SMP Fri May 20 03:51:51 BST 2011 x86_64 x86_64 x86_64 GNU/Linux
Run Code Online (Sandbox Code Playgroud)

Ste*_*ker 6

你可能正在运行我们的文件描述符.在您分叉远程程序之后,或者就此而言,您可能没有关闭程序中的in_pipe [1]和out_pipe [0].