6
votes

I need to create two child processes. One child needs to run the command "ls -al" and redirect its output to the input of the next child process, which in turn will run the command "sort -r -n -k 5" on its input data. Finally, the parent process needs to read that (data already sorted) and display it in the terminal. The final result in the terminal (when executing the program) should be the same as if I entered the following command directly in the shell: "ls -al | sort -r -n -k 5". For this I need to use the following methods: pipe(), fork(), execlp().

My program compiles, but I don't get the desired output to the terminal. I don't know what is wrong. Here is the code:

#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main()
{
    int fd[2];
    pid_t ls_pid, sort_pid;
    char buff[1000];

    /* create the pipe */
    if (pipe(fd) == -1) {
        fprintf(stderr, "Pipe failed");
        return 1;
    }

    /* create child 2 first */
    sort_pid = fork();
    if (sort_pid < 0) {                         // error creating Child 2 process
        fprintf(stderr, "\nChild 2 Fork failed");
        return 1;
    }
    else if(sort_pid > 0) {                     // parent process

        wait(NULL);                             // wait for children termination

        /* create child 1 */
        ls_pid = fork();
        if (ls_pid < 0) {                       // error creating Child 1 process
            fprintf(stderr, "\nChild 1 Fork failed");
            return 1;
        }
        else if (ls_pid == 0) {                 // child 1 process
            close(1);                           // close stdout
            dup2(fd[1], 1);                     // make stdout same as fd[1]
            close(fd[0]);                       // we don't need this end of pipe
            execlp("bin/ls", "ls", "-al", NULL);// executes ls command
        }

        wait(NULL);
        read(fd[0], buff, 1000);                // parent reads data 
        printf(buff);                           // parent prints data to terminal   
    }
    else if (sort_pid == 0) {                   // child 2 process
        close(0);                               // close stdin
        dup2(fd[0], 0);                         // make stdin same as fd[0]
        close(fd[1]);                           // we don't need this end of pipe
        execlp("bin/sort", "sort", "-r", "-n", "-k", "5", NULL); // executes sort operation
    }

    return 0;
}
1
Unless if you are executing from /, try replacing "bin/ls" by "/bin/ls" (similar for "/bin/sort" )wildplasser
It appears you are doing no error checking. You should add some, that will help you diagnose the issue.Oliver Charlesworth
I have printf() statements inside the children blocks. Without the "/", it shows me both children statements but when I put the "/" it doesn't show those statements; it is like it doesn't reach the children blocks.user2443819
BTW: execlp() and execvp() perform a PATH search if there is no '/' in the pathname. This can all be read in The Fine Manual. Thay also can return (on error) check the return value and errno IF they return.wildplasser
Where did that disease start that prints newlines at the start instead of the end of strings, as in fprintf(stderr, "\nChild 2 Fork failed");?Jens

1 Answers

3
votes

Your parent process waits for the sort process to finish before creating the ls process.

The sort process needs to read its input before it can finish. And its input is coming from the ls that won't be started until after the wait. Deadlock.

You need to create both processes, then wait for both of them.

Also, your file descriptor manipulations aren't quite right. In this pair of calls:

close(0);
dup2(fd[0], 0);

the close is redundant, since dup2 will automatically close the existing fd 0 if there is one. You should do a close(fd[0]) after ther dup2, so you only have one file descriptor tied to that end of the pipe. And if you want to be really robust, you should test wither fd[0]==0 already, and in that case skip the dup2 and close.

Apply all of that to the other dup2 also.

Then there's the issue of the parent process holding the pipe open. I'd say you should close both ends of the pipe in the parent after you've passed them on to the children, but you have that weird read from fd[0] after the last wait... I'm not sure why that's there. If the ls|sort pipeline has run correctly, the pipe will be empty afterward, so there will be nothing to read. In any case, you definitely need to close fd[1] in the parent, otherwise the sort process won't finish because the pipe won't indicate EOF until all writers are closed.

After the weird read is a printf that will probably crash, since the read buffer won't be '\0'-terminated.

And the point of using execlp is that it does the $PATH lookup for you so you don't have to specify /bin/. My first test run failed because my sort is in /usr/bin/. Why hardcode paths when you don't have to?