the first terminal:

$ sort | sort

pid of the first one is 1, pid of the second one is 2.

The second terminal:

$ strace -p 1

strace: Process 1 attached

The third terminal:

$ strace -p 2
strace: Process 2 attached

The fourth terminal:

$ ps l

0  1000  1  6891  20   0  27464   912 wait_w S+   pts/2      0:00 sort
0  1000  2  6891  20   0  27464   924 pipe_w S+   pts/2      0:00 sort

what points that both sort processes are foreground. However, when I put any character (A) to the first terminal strace points that only the first one sort (with pid = 1) got an input:

read(0, "A\n", 130048)                  = 2

However, when I send SIGINT by pressing Ctrl-C both sort processes got signal. Why?

--- SIGINT {si_signo=SIGINT, si_code=SI_TKILL, si_pid=1, si_uid=1000} ---
--- SIGINT {si_signo=SIGINT, si_code=SI_TKILL, si_pid=2, si_uid=1000} ---

1 Answers

Shawn On

sort has to read all its input before it outputs anything (Since otherwise how can it sort everything?).

Since your first invocation is reading standard input from a terminal, you need to tell the terminal you're done providing input with a ctrl-d. Then it can write all the sorted lines to its standard output, which is connected to the second invocation's standard input, and exit, which will cause the second sort to in turn see EOF when it's done reading all of the first one's output.