Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of send(2) receiving a SIGPIPE? [duplicate]

Tags:

c

linux

sockets

Possible Duplicate:
Why is writing a closed TCP socket worse than reading one?

Why doesn't an erroneous return value suffice?
What can I do in a signal handler that I can't do by testing the return value for EPIPE?

like image 531
Idan K Avatar asked Jan 29 '26 13:01

Idan K


1 Answers

Back in the old days almost every signal caused a Unix program to terminate. Because inter-process communication by pipes is fundamental in Unix, SIGPIPE was intended to terminate programs which didn't handle write(2)/read(2) errors.

Suppose you have two processes communicating through a pipe. If one of them dies, one of the ends of the pipe isn't active anymore. SIGPIPE is intended to kill the other process as well.

As an example, consider:

cat myfile | grep find_something

If cat is killed in the middle of reading the file, grep simply doesn't have what to do anymore and is killed by a SIGPIPE signal. If no signal was sent and grep didn't check the return value of read, grep would misbehave in some way.

like image 67
Blagovest Buyukliev Avatar answered Jan 31 '26 02:01

Blagovest Buyukliev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!