1

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);
}
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
user996922
  • 161
  • 1
  • 12

2 Answers2

0

Are you looking for "a" or a? The double quotes might be left over from shell syntax.

Joshua
  • 40,822
  • 8
  • 72
  • 132
  • The shell will generate the same output for a and "a". what you mean is: `cat filename | grep \"a\"` – Nir Alfasi Jan 06 '12 at 18:30
  • Yeah but passing the double quotes to grep via execlp won't. – Joshua Jan 06 '12 at 18:56
  • @Joshua : the program hangs on grep whether i call execlp("grep", "grep", "\"a\"", (char*)NULL) or just execlp("grep", "grep", "a", (char*)NULL); – user996922 Jan 07 '12 at 18:04
  • Oops, that wasn't it. I'm leaving this answer up only as it might be a correct description of another problem in the code. – Joshua Jan 07 '12 at 22:31
0

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

Joshua
  • 40,822
  • 8
  • 72
  • 132