Thread: "ls | sort" with pipes and forks?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    15

    "ls | sort" with pipes and forks?

    I'm practicing with exec and pipes and forks but I can't seem to understand how to have "sort" sort the output of "ls". I'm actually unsure of how to do this. I have my code below.

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/wait.h>
    
    int main(int argc, char** argv) {
    
        int fd[2];
        int pid;
        char read_buf[READ_SIZE + 1];
        int chars_read;
    
        /*
         * Check for adequate args.
         */
        if (argc < 1) {
            fprintf(stderr, "usage: grab-stdout prog arg ...");
            exit(-1);
        }
    
        /*
         * Create a pipe that will have its output written to by the executed
         * program.
         */
        pipe(fd);
    
        /*
         * Fork off a process to exec the program.
         */
        if ((pid = fork()) < 0) {
            perror("fork");
      exit(-1);
        }
    
        /*
         * Forked child has its stdout be the write end of the pipe.
         */
        else if (pid == 0) {
    
            /*
             * Don't need read end of pipe in child.
             */
            close(fd[0]);
    
            /*
             * This use of dup2 makes the output end of the pipe be stdout.
             */
            dup2(fd[1], STDOUT_FILENO);
    
            /*
             * Don't need fd[1] after the dup2.
             */
            close(fd[1]);
    
            /*
             * Exec the program given in the command line, including any args.
             */
    
            execlp("ls", "ls", 0);
            
            perror("exec1");
            exit(-1);
        }
    
        /*
         * Parent takes its input from the read end of the pipe.
         */
        else {
            /*
             * Don't need write end of pipe in parent.
             */
            close(fd[1]);
    
            //I'm unsure of what to do here but this is all I could think of
            execlp("sort", "sort", "-r", fd[0] );
    
            perror("exec2");
            exit(-1);
    
        }
    
        exit(0);
    }
    Thanks for any input.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868

    * Parent takes its input from the read end of the pipe.
    . . .
    * Don't need write end of pipe in parent.
    OMG! Thanks for the laugh!

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > execlp("sort", "sort", "-r", fd[0] );
    Well you use the same dup2 (for stdin of course) and then
    execlp("sort", "sort", "-r" );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    15
    ok i got it, Thanks Salem!
    Last edited by alwut; 01-09-2008 at 03:08 AM. Reason: not up to date

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple forks, pipes?
    By alcaseltz in forum C Programming
    Replies: 2
    Last Post: 10-26-2007, 07:07 AM