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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:57:56+00:00 2026-05-18T09:57:56+00:00

Hopefully a simple question. I’m trying to learn, simultaneously, fork(), pipe(), and waitpid() and

  • 0

Hopefully a simple question. I’m trying to learn, simultaneously, fork(), pipe(), and waitpid() and running into some problems.

if (pipe(myout)<0 || pipe(myin)<0 || pipe(myerr)<0) { perror("Couldn't make pipes"); return; }
int childpid=fork();
if (childpid==0) { //child
    fdopen(myout[1], "w");
    fdopen(myin[1], "r");
    fdopen(myerr[1], "w");
    dup2(myout[1],  1);
    dup2(myin[1], 0);
    dup2(myerr[1], 2);
    printf("This should be seen\n");
    fclose(stdout); fclose(stdin); fclose(stderr);
    sleep(10);
    _exit(0);
 } else { //parent, wait on child
    printf("parent, monitoring\n");
    sim_out=fdopen(myout[0], "r");
    sim_in=fdopen(myin[0], "w");
    sim_err=fdopen(myerr[0], "r");
    printf("have my fds\n");
    int status;
    do {
        int ch;
        if (read(myout[0], &ch, 1)>0)
            write(1, &ch, 1);
        else printf("no go\n");
            waitpid(childpid, &status, WNOHANG);
    } while (!WIFEXITED(status) && !WIFSIGNALED(status));
}

I’m getting:

parent, monitoring
have my fds
T

before the program exits – that is, the loop runs only once. I have a check below that, and it’s coming up WIFEXITED() so the process is supposed to have exited normally. But what bothers me is that there’s a sleep(10) before that happens, and this happens immediately – not to mention that the child processes are left running for the remaining wait time.

I’m fundamentally misunderstanding something, clearly. My expectation was that the parent loop would block until it sees a character from the child, read it, then check to see if it was still alive. I certainly didn’t expect waitpid() to set WIFEXITED when the child was still alive.

Where am I wrong?

  • 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-05-18T09:57:57+00:00Added an answer on May 18, 2026 at 9:57 am

    I think I can see several issues. I’ll try to mention them in order of appearance.

    • You should check fork for the return value -1 which indicates an error (no child will have been forked in this case).
    • All your calls to fdopen leak resources (depending on the implementation; the one on RHEL4 leaks). They return a FILE* which you could then use in fwrite etc. and then close by doing fclose on them. But you throw that value away. You don’t need to open the pipe for reading/writing. Pipes are suitable for that when created.
    • The child should close the ends of the pipe it does not use. Add close (myin [1]); myin [1] = -1; close (myout [0]); myout [0] = -1; close (myerr [0]); myerr [0] = -1;
    • The dup2 are technically fine on all Linux variants I know, but it is customary to use the first fd of a pipe for reading and the other for writing. Thus, your dup2s would be best changed to dup2 (myin [0], STDIN_FILENO); dup2 (myout [1], STDOUT_FILENO); dup2 (myerr [1], STDERR_FILENO);
    • The parent should close the ends of the pipe it does not use. Add close (myin [0]); myin [0] = -1; close (myout [1]); myout [1] = -1; close (myerr [1]); myerr [1] = -1;
    • Your main problem: You check the possibly uninitialized status for your child’s exit code. But waitpid has not yet been called. You should check waitpid‘s exit code and not evaluate status if it returns something other than childpid.

    Edit

    Since only the pipe ends that you actually need are open now, the operating system will detect a broken pipe for you. The pipe breaks when the child does fclose (stdout). The parent can still proceed to read all the data that may be in the pipe, but after that read will return zero, indicating a broken pipe.

    Thus, you can actually spare the call to waitpid. You could instead simply wait for read to return zero. This is not 100% equivalent, though, since your child closes its end of the pipe before it goes to sleep (causing the parent to proceed when all data have been read), whereas the waitpid version, of course, only proceeds when the child actually died.

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

Sidebar

Related Questions

Hopefully simple question: I'm trying to set the alternatingItemColors on a datagrid via some
I've got a (hopefully) simple question. My program has a ListView to display some
I have a hopefully pretty simple question here. I'm converting some Access SQL script
I have a quick (hopefully simple) question. I have some information stored in a
Very quick and hopefully simple question. I am trying to select a hidden input
I have a hopefully simple question... I am trying to change over from the
hopefully a simple question here. I'm running a Wordpress blog that has a bunch
I have a (hopefully) simple question. Is it possible to use SSHD tunneling to
I'm just getting started with Require.JS and I have a (hopefully) simple question. I'm
Hopefully a simple question although one I have found impossible to answer myself using

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.