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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:46:14+00:00 2026-05-16T16:46:14+00:00

After incorporating Ben Voigt’s answer into the code, it appears to work Original question:

  • 0

After incorporating Ben Voigt’s answer into the code, it appears to work

Original question:

I’m trying to use dup2 to:

  1. pass the output of “ls -al” as input to “grep foo”,
  2. whose output becomes input for “grep bar”,
  3. which finally outputs to stdout.

The final output is (blank), the file “in” is (blank) & the file “out” has the output of “ls -al”.

Any ideas what could be happening?

int main()
{
    pid_t pid;
    int i;
    int inFileDes,outFileDes;   
    inFileDes=open("in",O_RDWR | O_CREAT,S_IRUSR | S_IWUSR); 
    outFileDes=open("out",O_RDWR | O_CREAT,S_IRUSR | S_IWUSR);  
    for(i=0;i<3;i++)
    {   
        if((pid=fork())==0)
        {
            switch(i)
            {
                case 0:
                dup2(outFileDes,1);
                execl("/bin/ls","ls","-al",0);
                break;
                case 1:
                                                  // originally:
                dup2(outFileDes,0);   // dup2(outFileDes,1);  
                dup2(inFileDes,1);    // dup2(inFileDes,0);

                execl("/bin/grep","grep","foo",0);   //lines having foo
                break;
                case 2:
                dup2(inFileDes,0);
                execl("/bin/grep","grep","bar",0);  //lines having foo & bar
                break;
            }
            exit(-1);  //in error   
        }
        waitpid(pid,NULL,0);
    }
    close(inFileDes);
    close(outFileDes);
    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-16T16:46:15+00:00Added an answer on May 16, 2026 at 4:46 pm

    Your open call creates an empty file “in” and none of the programs write to it, so that’s expected. Since both instances of grep read from an empty file, their output is also empty.

    What you really want is to use the pipe function to get a pair of handles, which is written to be one program and read from by the next. You’ll need to call it twice because you have two sets of connections between child processes.

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

Sidebar

Related Questions

After discovering about Javascript namespaces, I tried to implement them but I run into
After some time researching and trying different things I still cannot get my @ExceptionHandler
After I click update in the grid view, the code works successfully. But when
After doing an AJAX call, am redirecting- using the following code. But, after AJAX
After looking at this question , I think I want to wrap ThreadLocal to
After looking over the of the NHibernate.Envers code I realized I was implementing the
After using Instruments, I have found the a spot in my code that is
After using the following code no errors are shown, but my database is not
After my previous question I have managed to Add and Ad banner to my
After nearly 10 hours of searching and trying different things I couldn't 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.