LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-21-2005, 04:41 AM   #1
alanwolfen
LQ Newbie
 
Registered: Oct 2004
Distribution: Redhat Linux 9
Posts: 11

Rep: Reputation: 0
Arrow How to detect broken pipe in C (Linux)?


I am using pipes for IPC and have a main program opening pipes for 2 other processes. Each process is connected the main program by 2 pipes (read and write)

main <=a== process 1
main ==b=> process 1

The main program will wait at the read end of pipe a. When a process connects, the main program will open the write end of pipe b which process 1 had already opened.

Let's say process 1 terminates unexpectedly, then the main program crashes due to broken pipe b. Now the question is having 2 process connecting to main with 2 pairs of read-write pipes, how do main detect which pipe is broken and closes it without crashin?
 
Old 02-21-2005, 08:46 AM   #2
Raghavendra_M
LQ Newbie
 
Registered: Feb 2005
Location: Bangalore
Posts: 2

Rep: Reputation: 0
using signal

You can try catching the signal "SIGPIPE". You can register to get this signal using the system call "signal".

Hope this solves your problem.
 
Old 02-21-2005, 07:36 PM   #3
alanwolfen
LQ Newbie
 
Registered: Oct 2004
Distribution: Redhat Linux 9
Posts: 11

Original Poster
Rep: Reputation: 0
I have thought of signals, but it doesn't allow me to identify which pipe causes the error and to close it. I dun wan to close all pipes and reopen all of them.

Anyone to identify the pipe's FD if we are to use the signal handler's approach?
 
Old 02-22-2005, 07:57 AM   #4
Raghavendra_M
LQ Newbie
 
Registered: Feb 2005
Location: Bangalore
Posts: 2

Rep: Reputation: 0
As you might be knowing the signal SIGPIPE is sent to the process which is trying to write into a pipe which has no readers.

In your case, to identify the exact pipe which is closed probably you can do the following:

- ignore the SIGPIPE signal i.e.

Code:
signal(SIGPIPE,SIG_IGN); //ignoring the broken pipe signal
- now whenever a reader process exits, since writer process has opted to ignore the signal whenever writer tries to write into the pipe the "write" system call will fail & returns error (-1). The exact reason for the failure of "write" can be got if required ("errno" will be set to "EPIPE").

Hope this will solve your problem.
 
Old 02-23-2005, 01:22 AM   #5
alanwolfen
LQ Newbie
 
Registered: Oct 2004
Distribution: Redhat Linux 9
Posts: 11

Original Poster
Rep: Reputation: 0
Yes it solves my problem! Thanks.
 
  


Reply


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? Nubian Linux - Newbie 2 08-16-2005 08:56 AM
broken pipe in mozilla? cubfan425 Linux - Newbie 1 03-15-2004 09:06 AM
Broken pipe ? Red evil Linux - Software 1 01-16-2004 05:01 AM
Broken Pipe Cynthia Blue Linux - Newbie 0 12-30-2002 02:31 PM
ls - Broken pipe vcheah Linux - Software 2 10-30-2002 08:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:24 AM.

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