Say I need to run a UNIX command-line program on some big file. If I want to see a bit of the output, I can pipe the output to head
:
$ cat big_file.txt | head
For cat
and many similar programs, this command runs extremely fast. Since only the first few lines are needed, program execution stops as soon as the requirement on the other end of the pipe is satisfied. So we all know and love this kind of pipe command for quickly exploring what's in a file or what kind of output a program will write for us.
How does this work at the code level? Does some kind of signal get sent to cat
telling it that its output isn't needed anymore, and it can stop running now?