LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How to handle a broken pipe exception (SIGPIPE) in FIFO pipe? (http://www.linuxquestions.org/questions/programming-9/how-to-handle-a-broken-pipe-exception-sigpipe-in-fifo-pipe-866132/)

zyroot998 03-02-2011 11:24 PM

How to handle a broken pipe exception (SIGPIPE) in FIFO pipe?
 
Hi,all:
I've written a simple server in linux used fork to create a FIFO pipe.The server create two FIFO pipe.One for server read data from client and write data to client.Then another pipe for client read data from server and write data to server.
When the server read data from a client used server-pipe and then write data to client.But ,if the client no read open the pipe,the server side write will be crashed because of a broken-pipe SIGPIPE.

How to check whether the read side is opened?Or,how to catch the SIGPIPE,and then my server will still execute on,not crashed!!

Thank you very much!I'm on line waiting for your answer!Thanks!

halon1301 03-03-2011 02:54 AM

Hi there. It's easy to catch signals, here's how to do it:

1. #include <signal.h>

2. Add a function like this:
void handler(int s) {
printf("Caught SIGPIPE\n");
}

3. In your main(), do this:
signal(SIGPIPE, handler);

Whether your code will still work after the signal has been caught is a different story... But at least I hope this helps you towards a solution for your problem!

archtoad6 03-03-2011 05:27 AM

Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.

ta0kira 03-03-2011 11:23 AM

You should check for errors after write or fflush; if errno isn't either EINTR or EAGAIN you should generally stop writing to the pipe for good. EPIPE is one of those errors, which you get if SIGPIPE doesn't terminate the process.
Kevin Barry

Nominal Animal 03-03-2011 12:08 PM

You can also use send instead of write to write to a socket. If you use the MSG_NOSIGNAL flag for send, the SIGPIPE signal will not be sent: the call will just return (ssize_t)-1 with errno set to EPIPE.

zyroot998 03-03-2011 08:10 PM

Hi,to all:
halon1301 's method is ok,can used to solve this problem.But I used signal(SIGPIPE,SIG_IGN) to ignore the SIGPIPE exception when occured write to a closed read pipe in the other side.
I use FIFO pipe in block style,so have this problem that when to write first to chek read is or not opend yet.Otherwise wite will SIGPIPE.But I think there is some good way to solved this issue.
I used (SIGPIPE,SIG_IGN),then is no problem.
Thank you very muchu to all helped me!Thank you!!!


All times are GMT -5. The time now is 09:47 AM.