Broken pipe

Discussion in 'C Programming' started by Karthik Kumar, Oct 6, 2004.

  1. 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. '
    Karthik Kumar, Oct 6, 2004
    #1
    1. Advertising

  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 ----
    Karthik Kumar, Oct 6, 2004
    #2
    1. Advertising

  3. Karthik Kumar

    -berlin.de Guest

    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.


    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 ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Oct 6, 2004
    #3
  4. 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
    Pascal Ehlert, Oct 6, 2004
    #4
  5. Karthik Kumar

    CBFalconer Guest

    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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Oct 7, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. funtoosh

    how to detect broken pipe ?

    funtoosh, Dec 20, 2003, in forum: Perl
    Replies:
    1
    Views:
    911
    Ben Morrow
    Dec 21, 2003
  2. Clive
    Replies:
    0
    Views:
    3,005
    Clive
    Aug 12, 2003
  3. Tomas Penc
    Replies:
    3
    Views:
    35,939
  4. lee, wonsun
    Replies:
    1
    Views:
    485
    Jack Klein
    Nov 2, 2004
  5. Replies:
    1
    Views:
    222
    Ben Morrow
    Jun 2, 2004
Loading...

Share This Page