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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:21:20+00:00 2026-05-28T02:21:20+00:00

I’m trying to implement the following pipe using anonymous pipes : cat filename |

  • 0

I’m trying to implement the following pipe using anonymous pipes :

cat filename | grep "a" | wc -c

The program seems to be hanging on the grep command and i don’t see why. I’ve tried with some other commands rather than grep, and worked perfectly.

Here’s the code so far…

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>

    #define READ  0
    #define WRITE 1

    #define handle_errno(msg)   do{perror(msg); exit(EXIT_FAILURE);}while(0);

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

int fd1[2], fd2[2];

if(argc != 2 || !strcmp(argv[1], "--help")){
    fprintf(stderr, "Usage : %s <filepath>\n", argv[0]);
    exit(EXIT_FAILURE);
}

if(access(argv[1], F_OK) == -1)
    handle_errno("access error");

if(pipe(fd1) == -1)
    handle_errno("pipe 1 error");

if(pipe(fd2) == -1)
    handle_errno("pipe 2 error");

switch(fork()){
    case -1:    handle_errno("fork 1 error");

    case  0:        
            /* first child: writer on pipe1 */
            if(close(fd1[READ]) == -1)
                handle_errno("child 1, close error");

            if(fd1[WRITE] != STDOUT_FILENO){
                if(dup2(fd1[WRITE], STDOUT_FILENO) == -1)
                    handle_errno("child 1, dup 2 error");

                if(close(fd1[WRITE]) == -1)
                    handle_errno("child 1, close error");
            }

            execlp("cat", "cat", argv[1], (char*)NULL);
            handle_errno("child 1, execlp cat error");
    default:
            break;
}

switch(fork()){
    case -1:    handle_errno("fork 2 error");

    case  0:        
            /* second child: reader on pipe1, writer on pipe2 */
            if(close(fd1[WRITE]) == -1)
                handle_errno("child 2, close error");

            if(close(fd2[READ]) == -1)
                handle_errno("child 2, close error");

            if(fd1[READ] != STDIN_FILENO){
                if(dup2(fd1[READ], STDIN_FILENO) == -1)
                    handle_errno("child 2, dup 2 error");

                if(close(fd1[READ]) == -1)
                    handle_errno("child 2, close error");
            }

            if(fd2[WRITE] != STDOUT_FILENO){
                if(dup2(fd2[WRITE], STDOUT_FILENO) == -1)
                    handle_errno("child 2, dup 2 error");

                if(close(fd2[WRITE]) == -1)
                    handle_errno("child 2, close error");
            }

            execlp("grep", "grep", "\"a\"", (char*)NULL);
            handle_errno("child 2, execlp grep error");

    default:
            break;
}

switch(fork()){
    case -1:    handle_errno("fork 3 error");

    case  0:        
            /* third child: reader on pipe2 */
            if(close(fd2[WRITE]) == -1)
                handle_errno("child 3, close error");

            if(fd2[READ] != STDIN_FILENO){
                if(dup2(fd2[READ], STDIN_FILENO) == -1)
                    handle_errno("child 3, dup 2 error");

                if(close(fd2[READ]) == -1)
                    handle_errno("child 3, close error");
            }

            execlp("wc", "wc", "-c", (char*)NULL);
            handle_errno("child 3, execlp wc error");
        default:
            break;
    }

    if(close(fd1[READ]) == -1)
        handle_errno("father, close error");

    if(close(fd1[WRITE]) == -1)
        handle_errno("father, close error");

    if(close(fd2[READ]) == -1)
        handle_errno("father, close error");    

    if(close(fd2[WRITE]) == -1)
        handle_errno("father, close error");

    if(wait(NULL) == -1)
        handle_errno("father, wait error");

    if(wait(NULL) == -1)
        handle_errno("father, wait error");

    if(wait(NULL) == -1)
        handle_errno("father, wait error");

    exit(EXIT_SUCCESS);
}
  • 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-28T02:21:21+00:00Added an answer on May 28, 2026 at 2:21 am

    The problem is you left the extra pipe handles for pipe #1 open in process 3 (wc). Close all unnecessary handles before calling exec.

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

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
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 am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.