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! |
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! |
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
|
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 |
|
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 08:18 PM. |