+1 To Greg Hewgill for leading my thought process in the correct direction to find the answer.
The real reason for SIGPIPE
in both sockets and pipes is the filter idiom / pattern which applies to typical I/O in Unix systems.
Starting with pipes. Filter programs like grep typically write to STDOUT
and read from STDIN
, which may be redirected by the shell to a pipe. For example:
cat someVeryBigFile | grep foo | doSomeThingErrorProne
The shell when it forks and then exec's these programs probably uses the dup2
system call to redirect STDIN
, STDOUT
and STDERR
to the appropriate pipes.
Since the filter program grep
doesn't know and has no way of knowing that it's output has been redirected then the only way to tell it to stop writing to a broken pipe if doSomeThingErrorProne
crashes is with a signal since return values of writes to STDOUT
are rarely if ever checked.
The analog with sockets would be the inetd
server taking the place of the shell.
As an example I assume you could turn grep
into a network service which operates over TCP
sockets. For example with inetd
if you want to have a grep
server on TCP
port 8000 then add this to /etc/services
:
grep 8000/tcp # grep server
Then add this to /etc/inetd.conf
:
grep stream tcp nowait root /usr/bin/grep grep foo
Send SIGHUP
to inetd
and connect to port 8000 with telnet. This should cause inetd
to fork, dup the socket onto STDIN
, STDOUT
and STDERR
and then exec grep
with foo as an argument. If you start typing lines into telnet grep
will echo those lines which contain foo.
Now replace telnet with a program named ticker
that for instance writes a stream of real time stock quotes to STDOUT
and gets commands on STDIN
. Someone telnets to port 8000 and types "start java" to get quotes for Sun Microsystems. Then they get up and go to lunch. telnet inexplicably crashes. If there was no SIGPIPE
to send then ticker
would keep sending quotes forever, never knowing that the process on the other end had crashed, and needlessly wasting system resources.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…