Thread: Question in process redirection

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    1

    Question in process redirection

    I am trying to write a program to run ls ../ | grep sg | sort
    with execvp and pipe in linux
    The program does what I want it to do but the sequence is wrong.
    The shell always prompt before the result.
    How can I print the result before the shell prompt?
    Here is my code:
    Code:
    #include <stdio.h>
    #include <unistd.h>
    int main(){
            char cmd[255];
            int fd[2];
            int fd2[2];
            int i;
            pid_t id;
            pipe(fd);
            pipe(fd2);
            for(i = 1; i < 4; i++){
                    id = fork();
                    if(id==0){
                            switch (i){
                                    case(1): close(fd[0]);
                                        close(fd2[0]);
                                        close(fd2[1]);
                                        dup2(fd[1],STDOUT_FILENO);
                                        char * args1[] = { "ls", "../", NULL};
                                        execvp(args1[0],args1);
                                        break;
                                    case(2): close(fd2[0]);
                                        dup2(fd2[1],STDOUT_FILENO);
                                        close(fd[1]);
                                        dup2(fd[0],STDIN_FILENO);
                                        char *args2[] = {"grep","sg",NULL};
                                        execvp(args2[0],args2);
                                        break;
                                    case(3): close(fd[0]);
                                        close(fd[1]);
                                        close(fd2[1]);
                                        dup2(fd2[0],STDIN_FILENO);
                                        char *args3[] = {"sort",NULL};
                                        execvp(args3[0],args3);
                                    }
                    }else{
                            waitpid(id);
                    }
            }
            return 0;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You are doing a VERY wierd thing here, teleologically speaking.

    Anyway, there is only one thing wrong: in case(2) you need to move the stdout duplication after the execvp call. Otherwise, the pipe is waiting with nothing.

    Code:
                                    case(2): close(fd2[0]);
                                        close(fd[1]);
                                        dup2(fd[0],STDIN_FILENO);
                                        char *args2[] = {"grep","sg",NULL};
                                        execvp(args2[0],args2);
                                        dup2(fd2[1],STDOUT_FILENO);
                                        break;
    Your program remains very unorthodox but it should work perfectly now.
    ps. you do know that there is no point calling sort at the end of the command?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MK27 View Post
    Anyway, there is only one thing wrong: in case(2) you need to move the stdout duplication after the execvp call. Otherwise, the pipe is waiting with nothing.
    Actually that's not true: the reason it works is because it now skips the last step (sort) and the output would be identical anyway.

    This is really not a good use for c. I was warned.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stopping Processes Question
    By brett in forum Linux Programming
    Replies: 3
    Last Post: 06-24-2007, 10:15 PM
  2. Round Robin Scheduling using c.
    By eclipt in forum C Programming
    Replies: 8
    Last Post: 12-28-2005, 04:58 PM
  3. Replies: 12
    Last Post: 05-17-2003, 05:58 AM
  4. Hi all!, Another question, About NT Process
    By Pandora in forum Windows Programming
    Replies: 4
    Last Post: 03-20-2003, 12:24 AM
  5. redirection program help needed??
    By Unregistered in forum Linux Programming
    Replies: 0
    Last Post: 04-17-2002, 05:50 AM