Skip to content

Pipes and streams

8 min

Streaming, measured

A pipeline that reads the file four times is not the better answer.

A pipeline runs its stages at the same time. head does not wait for sort to finish; it takes what it needs and stops.

When it stops, it closes its end of the pipe. The next write by whatever was feeding it fails, and that process is killed by SIGPIPE. That is the entire mechanism by which an infinite producer terminates.

Try this

Open the processes pane after running one of these. One process exited 0; the other was killed by SIGPIPE.

Because the stages run together, where you put the work changes how much gets read. head -1 big.txt reads one line. sort big.txt | head -1 reads all of it, because sort cannot produce its first line until it has seen the last.

Both print the same thing. Only one of them is the answer, and the tasks in this track measure the difference rather than guessing at it.

Try this

$

Files

The files in the sandbox, with their modes, link counts and sizes
ModeLinksSizeName
-rw-r--r--142words.txt