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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:32:01+00:00 2026-05-28T05:32:01+00:00

I’m taking an operating systems course and I’m having a hard time how input

  • 0

I’m taking an operating systems course and I’m having a hard time how input is redirected with dup2 when you have forks. I wrote this small program to try and get a sense for it but I wasn’t successful in passing the output of a grand-child to a child. I am trying to mimick the unix command: ps -A | wc -l. I’m new to Unix, but I believe this should count the lines of the list of running processes I get. So my output should be a single number.

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <iostream>

using namespace std;

int main( int argc, char *argv[] )  {

    char *searchArg = argv[ 1 ];
    pid_t pid;

    if ( ( pid = fork() ) > 0 ) {
        wait( NULL );
        cout << "Inside parent" << endl;
    } 
    else if ( pid == 0 ) {
            int fd1[ 2 ];
            pipe( fd1 );
            cout << "Inside child" << endl;

            if ( pid = fork() > 0 ) {
                dup2( fd1[ 0 ], 0 );
                close( fd1[ 0 ] );
                execlp( "/bin/wc", "-l", NULL );
            }
            else if ( pid == 0 ) {
                cout << "Inside grand child" << endl;
                execlp( "/bin/ps", "-A", NULL );
            }
        }
    return 0;
}

I don’t have it in the code above, but here is my guess on how things should go down:

  • We need to redirect standard output of command ps -A (whatever is usually printed to the screen, correct?) so that the wc -l command can use it to count the lines.
  • This standard output can be redirected using dup2, like dup2( ?, 1 ) which means redirect standard output to ?. Then you close ?.

Question: Where do I redirect it to? I know it should be one of the file descriptors, but where should it be redirected so that wc can process it?

  • wc somehow receives the standard output.

Question: How does wc receive the output? Through an execlp parameter? Or does the operating system check one of the file descriptors?

  • Execute wc -l.

Which one of these is closed and left open for wc to receive and process ps’s output? I keep thinking this needs to be thought of backwards since ps needs to give its output to wc…but that doesn’t seem to make sense since both child and grand-child are being processed in parallel.

pipe dream

  • 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-28T05:32:01+00:00Added an answer on May 28, 2026 at 5:32 am

    First off, let’s fix your code so that we add a tiny bit more error-checking to it, and so that it works; replace the bottom bit with:

    else if ( pid == 0 ) {
            int fd1[ 2 ];
            pipe( fd1 );
            cout << "Inside child" << endl;
    
            if ( (pid = fork()) > 0 ) {
                if (dup2( fd1[ 0 ] , 0 ) < 0) {
                  cerr << "Err dup2 in child" << endl;
                }
                close( fd1[ 0 ] );
                close( fd1[ 1 ] ); // important; see below
                // Note: /usr/bin, not /bin
                execlp( "/usr/bin/wc", "wc", "-l", NULL );
                cerr << "Err execing in child" << endl;
            }
            else if ( pid == 0 ) {
                cout << "Inside grand child" << endl;
                if (dup2( fd1[ 1 ] , 1 ) < 0) {
                  cerr << "Err dup2 in gchild" << endl;
                }
                close( fd1[ 0 ] );
                close( fd1[ 1 ] );
                execlp( "/bin/ps", "ps", "-A", NULL );
                cerr << "Err execing in grandchild" << endl;
            }
    }
    

    Now, your questions:

    • Question: Where do I redirect it to? I know it should be one of the file descriptors, but where should it be redirected so that wc can process it?

      The filedescriptors 0, 1, and 2 are special in Unix in that they are the standard input, standard output, and standard error. wc reads from standard input, so whatever is duped to 0.

    • Question: How does wc receive the output? Through an execlp parameter? Or does the operating system check one of the file descriptors?

      In general, after a process has had its image swapped out with exec, it will have all the open file descriptors it had before exec. (Except for those descriptors with the CLOSE_ON_EXEC flag set, but ignore that for now) Therefore, if you dup2 something to 0, then wc will read it.

    • Which one of these is closed and left open for wc to receive and process ps’s output?

      As shown above, you can close both ends of the pipe in both child and grandchild, and that’ll be fine. In fact, standard practice would recommend that you do that. However, the only truly necessary close line in this specific example is the one I comment as "important" – that’s closing the write end of the pipe in the child.

      The idea is this: both child and grand-child have both ends of the pipe open when they start. Now, through dup we’ve connected wc to the read end of the pipe. wc is going to keep sucking on that pipe until all descriptors on the write end of the pipe are closed, at which point it’ll see that it came to the end of the file and stop. Now, in the grand-child, we can get away with not closing anything because ps -A isn’t going to do anything with any of the descriptors but write to descriptor 1, and after ps -A finishes spitting out stuff about some processes it’ll exit, closing everything it had. In the child, we don’t really need to close the read descriptor stored in fd[0] because wc isn’t going to try to read from anything but descriptor 0. However, we do need to close the write end of the pipe in the child because otherwise wc is just going to sit there with a pipe that’s never completely closed.

      As you can see, the reasoning for why we didn’t really need any of the close lines except the one marked "important" depend on the details of how wc and ps are going to behave, so the standard practice is to close the end of a pipe you aren’t using completely, and keep open the end you are using only with one descriptor. Since you’re using dup2 in both processes, that means four close statements as above.

    EDIT: Updated the arguments to execlp.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a French site that I want to parse, but am running into

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.