GeekFactory
GeekFactory

Reputation: 399

How does pipelining work?

Can somebody explain what actually happens internally(the system calls called) for the command ls | grep 'xxx' ?

Upvotes: 2

Views: 1201

Answers (2)

dmp
dmp

Reputation: 439

The standard output of the first command is fed as standard input to the second command in the pipeline. There are a couple of system calls that you may be interested to understand what is happening in more detail, in particular, fork(2), execve(2), pipe(2), dup2(2), read(2) and write(2).

In effect the shell arranges STDIN_FILENO and STDOUT_FILENO to be the read end and the write end of the pipe respectively. When the first process in the pipeline performs a write(2) the standard output of that process is duplicated to be the write end of the pipe, similarly when the second process does a read(2) on the standard input it ends up reading from the read end of the pipe.

There are of course more details to be considered, please check out a book such as Advanced programming in the UNIX environment by Richard Stevens.

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799450

First, pipe(2,3p) is called in order to create the pipe with read and write ends. fork(2,3p) is then called twice, once for each command. Then dup2(2,3p) is used to replace the appropriate file descriptor in each forked child with each end of the pipe. Finally exec(3) is called in each child to actually run the commands.

Upvotes: 3

Related Questions