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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:59:41+00:00 2026-05-30T12:59:41+00:00

I’m having trouble using dup2 to make a c program execute a command such

  • 0

I’m having trouble using dup2 to make a c program execute a command such as ls /bin | grep grep | grep b. When I comment out the third command and associated pipe it executes ls /bin | grep grep fine, but with the last command it just returns right away. Also when I enter ‘ps’ the processes are still running. I think it’s due to how I close the pipes. My code is below:

int main()
{
    int pipeA[2];
    int pipeB[2];

    pipe(pipeA);
    pipe(pipeB);

    int pidA,pidB,pidC;

    if(pidA = fork())
    {
            close(pipeA[0]);
            dup2(pipeA[1],1);
            close(pipeA[1]);
            execlp("ls","ls","/bin",NULL);
            printf("error\n");
    }

    if(pidB = fork())
    {
            close(pipeA[1]);
            dup2(pipeA[0],0);
            close(pipeA[0]);

            close(pipeB[0]);
            dup2(pipeB[1],1);
            close(pipeB[1]);
            execlp("grep","grep","grep",NULL);
            printf("error\n");
    }

    if(pidC = fork())
    {
            close(pipeB[1]);
            dup2(pipeB[0],0);
            close(pipeB[0]);
            execlp("grep","grep","b",NULL);
            printf("error");
    }


    while(pidA != wait(0)){}

    return 0;
}
  • 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-30T12:59:42+00:00Added an answer on May 30, 2026 at 12:59 pm

    You aren’t closing enough file descriptors.

    /* Semi-working code */
    int main()
    {
        int pipeA[2];
        int pipeB[2];
    
        pipe(pipeA);
        pipe(pipeB);
    
        int pidA,pidB,pidC;
    
        if (pidA = fork())
        {
                close(pipeB[0]);  // "ls" is not going to use the second pipe
                close(pipeB[1]);  // Ditto
                close(pipeA[0]);
                dup2(pipeA[1], 1);
                close(pipeA[1]);
                execlp("ls", "ls", "/bin", (char *)NULL);
                fprintf(stderr, "error executing 'ls'\n");
                exit(1);
        }
    
        if (pidB = fork())
        {
                close(pipeA[1]);
                dup2(pipeA[0],0);
                close(pipeA[0]);
                close(pipeB[0]);
                dup2(pipeB[1],1);
                close(pipeB[1]);
                execlp("grep", "grep", "grep", (char *)NULL);
                fprintf(stderr, "error execing 'grep grep'\n");
                exit(1);
        }
    
        if (pidC = fork())
        {
                close(pipeA[0]);  // The second grep is not going to use the first pipe
                close(pipeA[1]);  // Ditto
                close(pipeB[1]);
                dup2(pipeB[0],0);
                close(pipeB[0]);
                execlp("grep", "grep", "b", (char *)NULL);
                fprintf(stderr, "error execing 'grep b'\n");
                exit(1);
        }
    
        close(pipeA[0]);  // The parent process is not using the pipes at all
        close(pipeA[1]);
        close(pipeB[0]);
        close(pipeB[1]);
    
        while (pidA != wait(0))
            ;
    
        return 0; 
    }
    

    Because you didn’t close pipeA in the second grep, you end up with the first grep waiting for input from the pipe the second grep still has open, even though the process will not write to it. Because of that, the first grep does not finish, so the second doesn’t finish either – even though the ls does complete. These comments would apply even if the parent process closed its copies of the pipes – as the corrected code does.

    Notice how you end up closing all 4 descriptors returned by the two calls to pipe() in each of the four processes – three children and the parent process.

    This leaves one residual problem – the process hierarchy is upside down because of your aconventional use of if (pidA = fork()). You have a child process waiting for its parents. You need to use:

    if ((pidA = fork()) == 0)
    {
        /* Be childish */
    }
    

    Similarly for each of the other two processes. You should also check the pipe() calls and the fork() calls for failure, just to be sure.

    #include <stdio.h>
    #include <unistd.h>
    #include <stdarg.h>
    #include <sys/wait.h>
    #include <stdlib.h>
    
    static void err_exit(const char *format, ...);
    
    /* Working code */
    int main(void)
    {
        int pipeA[2];
        int pipeB[2];
    
        if (pipe(pipeA) != 0 || pipe(pipeB) != 0)
            err_exit("Failed to create a pipe\n");
    
        int pidA,pidB,pidC;
    
        if ((pidA = fork()) < 0)
            err_exit("Failed to fork (A)\n");
        else if (pidA == 0)
        {
                close(pipeB[0]);  // "ls" is not going to use the second pipe
                close(pipeB[1]);  // Ditto
                close(pipeA[0]);
                dup2(pipeA[1], 1);
                close(pipeA[1]);
                execlp("ls", "ls", "/bin", (char *)NULL);
                err_exit("error executing 'ls'\n");
        }
    
        if ((pidB = fork()) < 0)
            err_exit("failed to fork (B)\n");
        else if (pidB == 0)
        {
                close(pipeA[1]);
                dup2(pipeA[0],0);
                close(pipeA[0]);
                close(pipeB[0]);
                dup2(pipeB[1],1);
                close(pipeB[1]);
                execlp("grep", "grep", "grep", (char *)NULL);
                err_exit("error execing 'grep grep'\n");
        }
    
        if ((pidC = fork()) < 0)
            err_exit("failed to fork (C)\n");
        else if (pidC == 0)
        {
                close(pipeA[0]);  // The second grep is not going to use the first pipe
                close(pipeA[1]);  // Ditto
                close(pipeB[1]);
                dup2(pipeB[0],0);
                close(pipeB[0]);
                execlp("grep", "grep", "b", (char *)NULL);
                err_exit("error execing 'grep b'\n");
        }
    
        close(pipeA[0]);  // The parent process is not using the pipes at all
        close(pipeA[1]);
        close(pipeB[0]);
        close(pipeB[1]);
    
        while (wait(0) != -1)
            ;
    
        printf("Continuing here...\n");
        sleep(3);
        printf("That's enough of that!\n");
    
        return 0; 
    }
    
    static void err_exit(const char *format, ...)
    {
        va_list args;
        va_start(args, format);
        vfprintf(stderr, format, args);
        va_end(args);
        exit(1);
    }
    

    When doctored to use /usr/bin instead of /bin, this program works OK on Mac OS X 10.7.3. It lists three files and then generates the message about ‘Continuing here’:

    bzegrep
    bzfgrep
    bzgrep
    Continuing here...
    That's enough of that!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.