By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,636 Members | 1,915 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,636 IT Pros & Developers. It's quick & easy.

Broken pipe

P: n/a
I don't know if this is the right newsgroup because the question is maybe a bit linux specific so if not slap me ;-)

I'm trying to send the output of a perl-script to a socket.
So I'm opening a pipe to the program (FILE fp = popen("foobar.pl", "r")) and read and write it directly to the socket.. This is my code:

------
while(!feof(fp))
{
bytes = fread(b,1,1024,fp);
fwrite(b,1,bytes,sock);
}
------

b was defined like this: b = malloc(1025); and is freed later.
This works correctly if the output of './foobar.pl' is very small.. But unfortunately, if it gets bigger I get the error: 'Broken pipe'.
The debuggers output is:

------
Program received signal SIGPIPE, Broken pipe.
0x400c3868 in write () from /lib/libc.so.6
(gdb) backtrace
#0 0x400c3868 in write () from /lib/libc.so.6
#1 0x40126308 in ?? () from /lib/libc.so.6
#2 0x4007cb0d in _IO_file_write () from /lib/libc.so.6
#3 0x4007b688 in _IO_do_write () from /lib/libc.so.6
#4 0x4007cca8 in _IO_file_xsputn () from /lib/libc.so.6
#5 0x40073959 in fwrite () from /lib/libc.so.6
#6 0x08049317 in send_sock (fp=0x804c668, sock=0x804c050, mode=0x804997d "cgi") at shttpd.c:29
#7 0x080491e1 in serve (fp=0x804c050) at serve.c:108
#8 0x0804960d in main (argc=1, argv=0xbffff6d4) at shttpd.c:108
------

Regards
Pascal Ehlert
Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Pascal Ehlert wrote:
I don't know if this is the right newsgroup because the question is maybe a bit linux specific so if not slap me ;-)

I'm trying to send the output of a perl-script to a socket.
So I'm opening a pipe to the program (FILE fp = popen("foobar.pl", "r")) and read and write it directly to the socket.. This is my code:

------
while(!feof(fp))
{
bytes = fread(b,1,1024,fp);
fwrite(b,1,bytes,sock);
}
------

b was defined like this: b = malloc(1025); and is freed later.
This works correctly if the output of './foobar.pl' is very small.. But unfortunately, if it gets bigger I get the error: 'Broken pipe'.
The debuggers output is:


<OT>

From what I could see,
SIGPIPE is generally caused because of a process synchronization
problem.
Process A might be trying to read / write from the
output of another Process B , that would have terminated
by then, and Process A might not be aware of that.
</OT>

--
Karthik. http://akktech.blogspot.com .
' Remove _nospamplz from my email to mail me. '
Nov 14 '05 #2

P: n/a
Karthik Kumar wrote:
Pascal Ehlert wrote:
I don't know if this is the right newsgroup because the question is
maybe a bit linux specific so if not slap me ;-)


Oops !!

news:comp.unix.programmer might be more appropriate.
If you suspect it is a C code problem, post a
small compilable code fragment to analyze the same.

--
Karthik. http://akktech.blogspot.com .
' Remove _nospamplz from my email to mail me. '
------------ And now a word from our sponsor ------------------
For a quality usenet news server, try DNEWS, easy to install,
fast, efficient and reliable. For home servers or carrier class
installations with millions of users it will allow you to grow!
---- See http://netwinsite.com/sponsor/sponsor_dnews.htm ----
Nov 14 '05 #3

P: n/a
Pascal Ehlert <da****@dadark.de> wrote:
I don't know if this is the right newsgroup because the question is
maybe a bit linux specific so if not slap me ;-) I'm trying to send the output of a perl-script to a socket.
So I'm opening a pipe to the program (FILE fp = popen("foobar.pl", "r"))
and read and write it directly to the socket.. This is my code: ------
while(!feof(fp))
{
bytes = fread(b,1,1024,fp);
fwrite(b,1,bytes,sock);
}
------ b was defined like this: b = malloc(1025); and is freed later.
malloc()ing 1024 would have been enough if you don't try to read more
- you're not dealing with strings here but "raw" data.
This works correctly if the output of './foobar.pl' is very small..
But unfortunately, if it gets bigger I get the error: 'Broken pipe'.


popen(), pipes and sockets are extensions to C, so they aren't dealt
with here, e.g. comp.unix.programmer would be a better place to ask
in your case.

But also from a standard C point of view there's a problem with your
code. feof() can't determine in advance if you're going to be able
to read from a file - you can only use it afterwards to find out
if a read failed because you reached EOF. So you better check the
return value of fread() to find out if the read was successful.

<OT>
You get a broken pipe error when you try to write to a pipe or
socket that has been closed by the reading side. So it looks as
if the other side has closed the socket before you're finished
writing to it.
</OT>
Regards, Jens

PS: Please keep your line length down to not more than about 72 chars.
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4

P: n/a
Pascal Ehlert wrote:

I don't know if this is the right newsgroup because the question
is maybe a bit linux specific so if not slap me ;-)

I'm trying to send the output of a perl-script to a socket. So I'm
opening a pipe to the program (FILE fp = popen("foobar.pl", "r"))
and read and write it directly to the socket.. This is my code:

------
while(!feof(fp))
{
bytes = fread(b,1,1024,fp);
fwrite(b,1,bytes,sock);
}
------

b was defined like this: b = malloc(1025); and is freed later.


pipes are OT here. However that is not your problem. You are
misusing feof, which only announces that a read encountered EOF,
not that the file is at EOF. So you should use something like:

#define BYTESTOREAD 1024
size_t bytes;

while ((bytes = fread(b, 1, BYTESTOREAD, fp) == BYTESTOREAD) {
fwrite(b, 1, bytes, sock);
}
/* possible error check goes here */
if (bytes) fwrite(b, 1, bytes, sock);

since a bytes value less than BYTESTOREAD signifies either error or
EOF. At the error check point above you can check feof to
distinguish between them. At that point your buffer b holds the
data (possibly 0 bytes) last read.

C is much different in this respect from Pascal, which has a read
ahead buffer (f^) and thus can automatically know when EOF has been
reached. C only reports it after a failed read.

You should restrict your line lengths to no more than 72 chars, 65
is preferable, in newsgroups.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.