Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-02-2011, 11:24 PM   #1
LQ Newbie
Registered: Feb 2011
Location: China
Posts: 17

Rep: Reputation: 0
Unhappy How to handle a broken pipe exception (SIGPIPE) in FIFO pipe?

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!
Old 03-03-2011, 02:54 AM   #2
LQ Newbie
Registered: Jul 2008
Location: Auckland, New Zealand
Distribution: Free/Open BSD and Ubuntu
Posts: 9

Rep: Reputation: 1
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!
Old 03-03-2011, 05:27 AM   #3
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
Old 03-03-2011, 11:23 AM   #4
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
Old 03-03-2011, 12:08 PM   #5
Nominal Animal
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
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.
1 members found this post helpful.
Old 03-03-2011, 08:10 PM   #6
LQ Newbie
Registered: Feb 2011
Location: China
Posts: 17

Original Poster
Rep: Reputation: 0
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!!!



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Broken Pipe Exception mail2mphani Linux - Networking 2 03-27-2019 05:10 AM
[SOLVED] Problems with FIFO pipe using a child and parent process mashhype Programming 5 11-22-2010 02:43 AM
Program received signal SIGPIPE, Broken pipe. grupoapunte Programming 1 06-03-2005 05:49 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:11 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration