Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7716791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:43:15+00:00 2026-06-01T02:43:15+00:00

I wrote this function to communicate with an external program. Such program takes input

  • 0

I wrote this function to communicate with an external program. Such program takes input from stdin and prints its output on stdout.
In order to make my code communicate with this program I redirect the stdin and stdout to buffers using pipes.

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;
}

the problem is that the 204th time I invoke this function, pipe() returns me an error (-1)!
Any idea why is that, or how can I avoid it? thanks a lot

further details: This is on Linux. The result of uname -a is:

 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:43:16+00:00Added an answer on June 1, 2026 at 2:43 am

    As many of you suggested, the problem was I neglected to close file descriptors before returning from the function, so I soon ran out of available ones.

    Here it follows the revised (working) version of the code

    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 */
    
      /* close all open file descriptors */
      close(in_pipe[1]);
      close(in_pipe[0]);
      close(out_pipe[1]);
      close(out_pipe[0]);
      close(saved_stdin);
      close(saved_stdout);
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python program which takes input from stdin. Now, I've to write
I wrote this little function to fill a drop down list with data from
this is somewhat of an odd question. I wrote a C function. Its 'like'
I wrote this function in C++ as part of a bigger program: Object Single_list<Object>::pop_front()
I wrote this function: jQuery(document).ready(function() { jQuery('input[type=text]').each( function(i) { thisval = jQuery(this).val(); jQuery(this).blur( function()
I wrote this function and now its always returning true... can anybody help me?
I wrote this function for filling closed loop, pixvali is declared globally to store
I wrote this function to get the unread count of google reader items. function
I wrote this function to get a pseudo random float between 0 .. 1
I wrote this code in my viewDidLoad function in my iOS project. lettersLeftLabel.text =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.