C before recording on a closed pipe

Is there an easy way to check if a channel is closed before it is written to C? I have a child and parent process, and the parent has a pipe for recording to the child. However, if the child closes the channel and the parent tries to read, I get an interrupt error message.

So, how can I check to make sure that I can write in the pipe, so I can deal with it as an error if I can not? Thanks!

+4
source share
2 answers

A simple way to check is to make 0 bytes of recording (2) on the handset and check the return. If you catch SIGPIPE or check EPIPE, you get an error. But it’s the same as if you went ahead and made your real record, checking for an error return. Thus, simply record and process the error either in the signal handler (SIGPIPE), or if the signal is ignored, checking the error return from the recording.

+7
source

How to just try to write and deal with the error? Same as writing to a file or database. I do not see the meaning in the idiom:

check if *this* is going to work do *this* 

You just enter a smaller and harder to test window of opportunity:

 check if *this* is going to work child thinks "Ha, fooled you, I'm off now!" do *this*, which now fails! 
+3
source

Source: https://habr.com/ru/post/1308766/


All Articles