473,387 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 26764
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.