tags:

views:

40

answers:

1

I have successfully piped the output of one command into the input of another and then show the output of the second command to the screen.

I want to do this with three successive commands. (actually eventually I want to do it with N commands passed into the program at run time.

This is my attempt at pipelining three commands together.

UPDATED: i updated my question to reflect my latest try.

    #include <string.h>
    #include <fstream>
    #include <iostream>
    #include <unistd.h>
    #include <stdio.h>
    #include <sys/wait.h>
    #include <sys/types.h>
    using namespace std;

    int main(int argc, char * argv[])
    {
             pid_t pid;
        int pfd[2];
        char* prgname = NULL;
        if(pipe(pfd) == -1)
        {
                perror("error on pipe call");
                return(1);
        }
        for(int j = 0;j<numberOfCommands;j++)
        {
                cout<<commands[j]<<"_"<<endl;
        }
        pid = fork();
        if(pid == 0){//child process
                close(pfd[0]); //close read end of pipe
                dup2(pfd[1],1);//connect the pipes
                close(pfd[1]);//close extra file descriptors
                prgname = (char*)"dmesg"; //commands[0];//first command
                execlp(prgname, prgname, 0);//Load the program
        }
        else
        {
                int pfd2[2];
                if(pipe(pfd2) == -1)
                {
                        perror("error on pipe call 2");
                        return(1);
                }
                pid = fork();
                if(pid == 0)//child
                {
                        close(pfd[1]);
                        dup2(pfd[0],0);
                        close(pfd[0]);
                        close(pfd2[0]);
                        dup2(pfd2[1],1);
                        close(pfd2[1]);
                        prgname = (char*)"sort";
                        execlp(prgname,prgname,0);
                }
                else
                {
                close(pfd2[1]); //close the write end of the pipe
                dup2(pfd2[0],0);//connect the pipes
                close(pfd2[0]); //close extra file descriptor
                prgname = (char*)"more"; //commands[1];//now run the second command
                execlp(prgname, prgname, 0);//Load the program
                }
        }
        return 0;
}

i have hard coded all values for simplicity. The program displays what should be the output of "dmesg|more" but does not do the sort part and then freezes. I see the begging of dmesg and more in the bottom left but i can not view any more.

Any ideas?

+2  A: 

pipe(2) only provides 2 file descriptors for 1 pipe. The 3rd file descriptor (pfd[2]) is junk and is never getting initialized. If you want to create a pipeline with 3 commands in it, you need to call pipe() twice to get two pipes: one for connecting the first and second processes, and one for connecting the second and third processes.

Adam Rosenfield
do you have an example of how this should be done? Thanks.
kralco626
i updated my answer to reflect my latest attempt using two pipes.
kralco626