473,472 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Broken pipe

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
4 26784
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Frank de Bot | last post by:
Hi, occasionaly I find in my apache logs that fastcgi had a broken pipe error with php running as fastcgi. the logs are like this: -- > (32)Broken pipe: > FastCGI: comm with server...
1
by: Ruben | last post by:
I am reading a large text file a chunk at a time using the readlines(buffer_size) statement. I get an IOERROR ERRNO 32 Broken Pipe command when I "pipe" the output to MYSQL database using the...
2
by: Nigel King | last post by:
I have a problem with the logging module. It reports a Broken Pipe error after outputing to the log file occasionally (5%). This does not appear to happen on Mac OSX using current finked python...
1
by: funtoosh | last post by:
Hi Scenario: I have a shell script e.g. a.bash This script wraps a program called "generate" like this: generate > /tmp/y.txt 2>&1 # both stdout and stderr r redirected to y.txt
4
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
6
by: Tim Chase | last post by:
While working on a Jumble-esque program, I was trying to get a string into a character array. Unfortunately, it seems to choke on the following import random s = "abcefg" random.shuffle(s) ...
1
by: Christoph Krammer | last post by:
Hello everybody, I try to use an external OCR tool to convert some binary image data to text. The image is in one variable, the text should be converted to another. I use the following code: ...
11
by: 7stud | last post by:
Hi, Can someone explain what a broken pipe is? The following produces a broken pipe error: ---------- import subprocess as sub p = sub.Popen(, stdin=sub.PIPE, stdout=sub.PIPE)
2
by: Samuel | last post by:
Hi, When using telnetlib, the connection sometimes breaks with the following error: "error: (32, 'Broken pipe')" where the traceback points to self.sock.send(buffer)
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.