Thread: Pipe multiple programs using pipe() and c

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    7

    Pipe multiple programs using pipe() and c

    Hello,
    I am trying to pipe multiple programs together using C. I piped 2 programs fine:
    here is the code for that:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(void) {
    
        int isParent;
        int apipe[2];
        char *cmd[2][3];
        
        cmd[0][0] = "ps";   cmd[0][1] = "-axu";     cmd[0][2] = NULL;
        cmd[1][0] = "grep"; cmd[1][1] = "root"; cmd[1][2] = NULL;
        
        pipe (apipe);
        isParent = fork();
        
        if (!isParent) {
            close (apipe[1]);
            dup2 (apipe[0], 0);
            close (apipe[0]);
            execvp (cmd[1][0],cmd[1]);
            perror ("Child exec failed!\n");
            exit (1);
        } else {
            close (apipe[0]);
            dup2 (apipe[1], 1);
            close (apipe[1]);
            
            execvp (cmd[0][0], cmd[0]);
            perror ("Parent exec failed!\n");
            exit (1);
        }
        
        return 0;
    }
    The code above is equivalent to "ps -axu | grep root" command in terminal.

    However when I want to pipe three programs, my solution does not work, here is the code for that:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(void) {
    
        int isParent1;
        int isParent2;
        int apipe[2];
        char *cmd[3][3];
        
        cmd[0][0] = "ps";   cmd[0][1] = "-axu";     cmd[0][2] = NULL;
        cmd[1][0] = "grep"; cmd[1][1] = "<your username>"; cmd[1][2] = NULL;
        cmd[2][0] = "head"; cmd[2][1] = "-6";       cmd[2][2] = NULL;
        
        pipe (apipe);
        isParent1 = fork();
        
        if (!isParent1) {
            // HEAD
            close (apipe[1]);
            dup2 (apipe[0], 0);
            close (apipe[0]);
            
            execvp (cmd[2][0],cmd[2]);
            perror ("Child exec failed!\n");
            exit (1);
        } else {
            isParent2 = fork ();
            
            if (!isParent2) {
                // GREP
                dup2 (apipe[0], 0);
                dup2 (apipe[1], 1);
                close (apipe[0]);
                close (apipe[1]);
    
                
                execvp (cmd[1][0],cmd[1]);
                perror ("Child exec failed!\n");
                exit (1);
            } else {
                // PS
                close (apipe[0]);
                dup2 (apipe[1], 1);
                close (apipe[1]);
            
                execvp (cmd[0][0], cmd[0]);
                perror ("Parent exec failed!\n");
                exit (1);
            }
        }
        
        return 0;
    }
    The code above is equivalent to "ps -axu | grep <your username> | head -6" command in terminal. It does not work as expected.

    I think I am doing everything right. I substitute stdout of ps with the pipe, then stdin and stdout of grep with pipe, and then stdin of head with pipe.

    Any help is appreciated!! Thanks

  2. #2
    Registered User
    Join Date
    Dec 2010
    Posts
    7
    YAY !!! Figured it out!

Popular pages Recent additions subscribe to a feed

Tags for this Thread