[SOLVED] How to handle a broken pipe exception (SIGPIPE) in FIFO pipe?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!
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
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.
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!!!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.