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 7721403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:54:57+00:00 2026-06-01T03:54:57+00:00

I wrote this piece of code that is supposed to redirect something written on

  • 0

I wrote this piece of code that is supposed to redirect something written on the STDOUT by a function to the STDIN so that it can be read by another function. I cannot access these functions, so this is the only way I can use them.
mpz_fput(stdout, c) is one of these function. It just prints on the STDOUT something contained in the c data structure.

Now everything worked fine while debugging as before the following code I had a printf(); followed by a fflush(stdout); (needed to print debugging messages).
Now that I removed these two lines I noticed (using gdb) that this code stays idle on the read() function (last line of this piece of code)

char buffer[BUFSIZ] = "";
int out_pipe[2];
int in_pipe[2];
int saved_stdout;
int saved_stdin;
int errno;

// REDIRECT STDIN
saved_stdin = dup(STDIN_FILENO);    /* save stdin for later */
if(errno= pipe(in_pipe) != 0 ) {          /* make a pipe */
  printf("\n%s",strerror(errno));
  exit(1);
}
close(STDIN_FILENO);
dup2(in_pipe[0], STDIN_FILENO);     /* redirect pipe to stdin */

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

mpz_fput(stdout,c);         // put c on stdout
read(out_pipe[0], buffer, BUFSIZ);  // read c from stdout pipe into buffer

any idea why is that?

  • 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-01T03:54:58+00:00Added an answer on June 1, 2026 at 3:54 am

    Seems you used the blocking type. In this case, out_pipe[0] is a blocking handle. So read() blocked, and waiting for anything out from out_pipe[0].

    Besides, I think there’s something to do with the fflush():

    For output streams, fflush() forces a write of all user-space buffered data
    for the given output or update stream via the stream’s underlying write
    function. For input streams, fflush() discards any buffered data that has
    been fetched from the underlying file, but has not been by the application.
    The open status of the stream is unaffected.

    In your case, you redirected pipe to STDOUT, then called fflush() to make everything in STDOUT flushed and move them to read() buffer. Then you called read() to read them out. If you didn’t call fflush(), and read() buffer would be empty. Since it’s a blocking handle used by read(), you can’t read anything from the buffer, so it will be blocked.

    This is the brief theory, I suggest you to read Linux manpage for more details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this function that's supposed to do StringPadRight("Hello", 10, "0") -> "Hello00000" .
I just wrote this piece of code to represent this error that is killing
I found this piece of Java code at Wikipedia that is supposed to shuffle
I've wrote this simple piece of code. And I have a slight problem with
I just wrote this piece of code but I'm not quite happy about it.
I'm sorry, but a while ago I wrote a piece of code that was
I hope in a good manner :-) I wrote this piece of code. What
When I run a piece of code that I wrote I keep getting the
I wrote this simple test code, by adapting a piece from a book, to
I have the following piece of code that I wrote in C. Its fairly

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.