3

Why does the following code execute the command:

"cat /etc/passwd | wc -l"

and not

"wc -l | cat /etc/passwd"?

Even though the debugging statements are in the order

b

a

 int main() {
    pid_t pid;
    int fd[2];

    int stdOut = dup(1);

    pid = fork();
    if (pid == 0) {
        pipe(fd);
        pid = fork();
        if (pid == 0) {
            close(fd[0]);
            dup2(fd[1], STDOUT_FILENO);
            close(fd[1]);

            write(stdOut, "a\n", 2);

            execlp("cat", "cat", "/etc/passwd", NULL);
        }
        close(fd[1]);
        dup2(fd[0], STDIN_FILENO);
        close(fd[0]);

        write(stdOut, "b\n", 2);

        execlp("wc", "wc", "-l", NULL);
    }
    wait(NULL);
    return 0;
}
GoodCoffee
  • 43
  • 5
  • 1
    Good job on closing file descriptors etc. The only minor quibble is that you should write an error and exit if either `execlp()` fails. – Jonathan Leffler Jul 10 '19 at 13:51

2 Answers2

1

The use of pipe, dup2 and close determines how the two processes are connected, not the order of execution. The process which runs first may block in a read or write access to the pipe until the other one sends or receives data respectively.

The execution order cannot be wrong because it is not specified. Any of parent or child may have to wait for something, and the scheduler does not guarantee fair distribution of resources. Maybe the creation of a child process takes some time, so the parent may reach

write(stdOut, "b\n", 2);

before the child reaches

write(stdOut, "a\n", 2);
Bodo
  • 9,287
  • 1
  • 13
  • 29
1

The order in which the markers a and b are displayed is not correlated with what happens through the pipe.
At the moment they are produced, there is no synchronisation between the two processes.
The cat command writes to its standard output which is redirected to the write side of the pipe.
The wc command reads from its standard input which is redirected from the read side of the pipe.
Thus, in any case, data will go from cat to wc through the pipe.

prog-fh
  • 13,492
  • 1
  • 15
  • 30