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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:42:42+00:00 2026-06-11T09:42:42+00:00

I have two sets of code both trying to execute something like ls|grep pip

  • 0

I have two sets of code both trying to execute something like ls|grep pip
One that works and one that does not.

The working code creates 2 child process and uses one child each to execlp the one command and the other simply tries to do this by creating one child. I.e executing ls in say the child and the grep in the parent. This does not seem to work. And I can’t seem to get any error either.

Can someone tell me what the problem is? And why it exists?

Not Working:

void runpipe()
{
    pid_t childpid;
    int fd[2];
    pipe(fd);
    int saved_stdout;
    int saved_stdin;
    saved_stdout=dup(STDOUT_FILENO);
    saved_stdin=dup(STDIN_FILENO);
    if((childpid=fork())==0)
    {
            dup2(fd[WRITE_END],STDOUT_FILENO);
            close(fd[WRITE_END]);
            execlp("/bin/ls","ls command","-l",NULL);

            dup2(STDOUT_FILENO,fd[1]);
            _exit(0);
    }
    else if(childpid>0)
    {

            dup2(saved_stdout,STDOUT_FILENO);
            dup2(fd[READ_END],STDIN_FILENO);
            close(fd[READ_END]);
            execlp("/bin/grep","grep","pip",NULL);
            wait();
            _exit(0);

    }
    else
    {
            printf("ERROR!\n");
    }
}

Here are the codes:

Working:

    int runpipe(){
    pid_t pid;
    int fd[2];

    pipe(fd);
    int i;
    pid=fork();

    if (pid==0) {

            printf("i'm the child used for ls \n");
            dup2(fd[WRITE_END], STDOUT_FILENO);
            close(fd[READ_END]);
            execlp("ls", "ls", "-al", NULL);
            _exit(0);
    } else {
            pid=fork();

            if (pid==0) {
                    printf("i'm in the second child, which will be used to grep\n");
                    dup2(fd[READ_END], STDIN_FILENO);
                    close(fd[WRITE_END]);
                    execlp("grep", "grep","pip",NULL);
            }
            else wait();
    }
    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-06-11T09:42:43+00:00Added an answer on June 11, 2026 at 9:42 am

    The parent needs to close the write side of the pipe before exec’ing grep. For some reason, your code with the two children closes that file descriptor, but does not in the code with only one child. You are leaving several descriptors open, but the write side on the pipe is the important one. The reader (the exec’d grep) will not finish until all copies of the write side of the pipe are closed. By failing to close it, the grep is the one holding it open so grep will never terminate, but just wait for more data.

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

Sidebar

Related Questions

I have two types of data sets. Both are in same size. One contains
I have two sets of data, (Ax, Ay; Bx, By). I'd like to plot
Assume that we have two sets: A=(a_1,a_2,...,a_m) and B=(b_1,b_2,...,a_n) (Not necessarily of same size).
I have two separate sets of tables in the same database that model the
I have a three pieces of code that are giving me trouble. This one
suppose you are given the following problem. You have two index sets that have
my current code is below. What I have is two sets of data phi
I have two sets of tabs, one nested inside the other. The nested tabs
I have two sets of arrays. I need to get the letters that are
I have two sets of elements with (sometimes) corresponding rel and id attributes: <a

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.