Question on SIGTERM

Discussion in 'Perl Misc' started by wing328hk@gmail.com, Jan 16, 2007.

  1. Guest

    Hi,

    I need to pipe a data stream to STDIN and I'm only interesting in the
    first line so I do

    my $header = <STDIN>;

    To read the rest of the data stream, I do

    do {
    my $buf = "";
    1 while (read(STDIN, $buf, 1024));
    };

    Would I get SIGPIPE if I don't use the do loop to get the rest of the
    message?

    Basically, how can I discard the rest of the message from STDIN? The
    data stream can be huge (30MB...) and that's why I don't want to waste
    CPU time in reading something I don't need.

    Would it make a different to above questions if the above code is part
    of a child process and there will be several child processes running
    simultaneously reading from different data stream from pipe??

    Thanks,
    wing328hk
     
    , Jan 16, 2007
    #1
    1. Advertising

  2. Guest

    wrote:
    > Hi,
    >
    > I need to pipe a data stream to STDIN and I'm only interesting in the
    > first line so I do
    >
    > my $header = <STDIN>;
    >
    > To read the rest of the data stream, I do
    >
    > do {
    > my $buf = "";
    > 1 while (read(STDIN, $buf, 1024));
    > };
    >
    > Would I get SIGPIPE if I don't use the do loop to get the rest of the
    > message?


    "You" (i.e. the process (not) doing the reading) wouldn't get a SIGPIPE.
    The process writing to "you" might get a SIGPIPE.

    > Basically, how can I discard the rest of the message from STDIN?


    Don't read it. Either close the filehandle without reading the rest, or
    just exit the program without reading the rest.

    > The
    > data stream can be huge (30MB...) and that's why I don't want to waste
    > CPU time in reading something I don't need.


    Why waste time *writing* something you don't need? If you have control
    over the writing process (which it seems you do, as you call the
    reading process a child), fix it.

    > Would it make a different to above questions if the above code is part
    > of a child process and there will be several child processes running
    > simultaneously reading from different data stream from pipe??


    Where do they get these different data streams from? Do you care whether
    the writing process gets a SIGPIPE? If so, why?

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jan 16, 2007
    #2
    1. Advertising

  3. Guest

    Hi,

    Thanks for the prompt reply. Sorry that I should have provided more
    information about the scenario.

    The data stream I was talking about is a mail pipe, so my question
    becomes would the mail program gets the SIGPIPE? I think yes based on
    the reply below.

    My next question is would it be possible to close the data stream from
    the client side (basicaly my script below that aims to read the first
    line or the mail header only) without reading all the data and without
    sending an SIGPIPE back to the mail program?

    Thanks,
    wing328hk


    wrote:
    > wrote:
    > > Hi,
    > >
    > > I need to pipe a data stream to STDIN and I'm only interesting in the
    > > first line so I do
    > >
    > > my $header = <STDIN>;
    > >
    > > To read the rest of the data stream, I do
    > >
    > > do {
    > > my $buf = "";
    > > 1 while (read(STDIN, $buf, 1024));
    > > };
    > >
    > > Would I get SIGPIPE if I don't use the do loop to get the rest of the
    > > message?

    >
    > "You" (i.e. the process (not) doing the reading) wouldn't get a SIGPIPE.
    > The process writing to "you" might get a SIGPIPE.
    >
    > > Basically, how can I discard the rest of the message from STDIN?

    >
    > Don't read it. Either close the filehandle without reading the rest, or
    > just exit the program without reading the rest.
    >
    > > The
    > > data stream can be huge (30MB...) and that's why I don't want to waste
    > > CPU time in reading something I don't need.

    >
    > Why waste time *writing* something you don't need? If you have control
    > over the writing process (which it seems you do, as you call the
    > reading process a child), fix it.
    >
    > > Would it make a different to above questions if the above code is part
    > > of a child process and there will be several child processes running
    > > simultaneously reading from different data stream from pipe??

    >
    > Where do they get these different data streams from? Do you care whether
    > the writing process gets a SIGPIPE? If so, why?
    >
    > Xho
    >
    > --
    > -------------------- http://NewsReader.Com/ --------------------
    > Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jan 17, 2007
    #3
  4. wrote:
    > wrote:
    > > wrote:
    > > > Hi,
    > > >
    > > > I need to pipe a data stream to STDIN and I'm only interesting in the
    > > > first line so I do
    > > >
    > > > my $header = <STDIN>;
    > > >
    > > > To read the rest of the data stream, I do
    > > >
    > > > do {
    > > > my $buf = "";
    > > > 1 while (read(STDIN, $buf, 1024));
    > > > };
    > > >
    > > > Would I get SIGPIPE if I don't use the do loop to get the rest of the
    > > > message?

    > >
    > > "You" (i.e. the process (not) doing the reading) wouldn't get a SIGPIPE.
    > > The process writing to "you" might get a SIGPIPE.
    > >
    > > > Basically, how can I discard the rest of the message from STDIN?

    > >
    > > Don't read it. Either close the filehandle without reading the rest, or
    > > just exit the program without reading the rest.
    > >
    > > > The
    > > > data stream can be huge (30MB...) and that's why I don't want to waste
    > > > CPU time in reading something I don't need.

    > >
    > > Why waste time *writing* something you don't need? If you have control
    > > over the writing process (which it seems you do, as you call the
    > > reading process a child), fix it.
    > >
    > > > Would it make a different to above questions if the above code is part
    > > > of a child process and there will be several child processes running
    > > > simultaneously reading from different data stream from pipe??

    > >
    > > Where do they get these different data streams from? Do you care whether
    > > the writing process gets a SIGPIPE? If so, why?
    > >


    >
    > Thanks for the prompt reply. Sorry that I should have provided more
    > information about the scenario.
    >
    > The data stream I was talking about is a mail pipe, so my question
    > becomes would the mail program gets the SIGPIPE? I think yes based on
    > the reply below.
    >
    > My next question is would it be possible to close the data stream from
    > the client side (basicaly my script below that aims to read the first
    > line or the mail header only) without reading all the data and without
    > sending an SIGPIPE back to the mail program?
    >


    You could cause the rest of the stream to be discarded:

    my( $header ) = <STDIN>; # instead of: my $header = <STDIN>

    this'll avoid the SIGPIPE.

    --
    Charles DeRykus
     
    comp.llang.perl.moderated, Jan 17, 2007
    #4
  5. Guest

    comp.llang.perl.moderated wrote:
    > wrote:
    > > wrote:
    > > > wrote:
    > > > > Hi,
    > > > >
    > > > > I need to pipe a data stream to STDIN and I'm only interesting in the
    > > > > first line so I do
    > > > >
    > > > > my $header = <STDIN>;
    > > > >
    > > > > To read the rest of the data stream, I do
    > > > >
    > > > > do {
    > > > > my $buf = "";
    > > > > 1 while (read(STDIN, $buf, 1024));
    > > > > };
    > > > >
    > > > > Would I get SIGPIPE if I don't use the do loop to get the rest of the
    > > > > message?
    > > >
    > > > "You" (i.e. the process (not) doing the reading) wouldn't get a SIGPIPE.
    > > > The process writing to "you" might get a SIGPIPE.
    > > >
    > > > > Basically, how can I discard the rest of the message from STDIN?
    > > >
    > > > Don't read it. Either close the filehandle without reading the rest, or
    > > > just exit the program without reading the rest.
    > > >
    > > > > The
    > > > > data stream can be huge (30MB...) and that's why I don't want to waste
    > > > > CPU time in reading something I don't need.
    > > >
    > > > Why waste time *writing* something you don't need? If you have control
    > > > over the writing process (which it seems you do, as you call the
    > > > reading process a child), fix it.
    > > >
    > > > > Would it make a different to above questions if the above code is part
    > > > > of a child process and there will be several child processes running
    > > > > simultaneously reading from different data stream from pipe??
    > > >
    > > > Where do they get these different data streams from? Do you care whether
    > > > the writing process gets a SIGPIPE? If so, why?
    > > >

    >
    > >
    > > Thanks for the prompt reply. Sorry that I should have provided more
    > > information about the scenario.
    > >
    > > The data stream I was talking about is a mail pipe, so my question
    > > becomes would the mail program gets the SIGPIPE? I think yes based on
    > > the reply below.
    > >
    > > My next question is would it be possible to close the data stream from
    > > the client side (basicaly my script below that aims to read the first
    > > line or the mail header only) without reading all the data and without
    > > sending an SIGPIPE back to the mail program?
    > >

    >
    > You could cause the rest of the stream to be discarded:
    >
    > my( $header ) = <STDIN>; # instead of: my $header = <STDIN>
    >
    > this'll avoid the SIGPIPE.
    >
    > --
    > Charles DeRykus


    But using
    my( $header ) = <STDIN>;
    would still read all the data from the pipe, and then assign first line
    to $header, right? or it has the intelligence to read only the first
    line into $header and discard the rest.

    I want to avoid reading all the data from the pipe, which can be huge
    (~3MB). I'm only interested in email headers (ie the first few lines of
    data).

    Thanks,
    wing328hk
     
    , Jan 17, 2007
    #5
  6. wrote:
    > comp.llang.perl.moderated wrote:
    >>> ....
    > > > My next question is would it be possible to close the data stream from
    > > > the client side (basicaly my script below that aims to read the first
    > > > line or the mail header only) without reading all the data and without
    > > > sending an SIGPIPE back to the mail program?
    > > >

    > >
    > > You could cause the rest of the stream to be discarded:
    > >
    > > my( $header ) = <STDIN>; # instead of: my $header = <STDIN>
    > >
    > > this'll avoid the SIGPIPE.


    >
    > But using
    > my( $header ) = <STDIN>;
    > would still read all the data from the pipe, and then assign first line
    > to $header, right? or it has the intelligence to read only the first
    > line into $header and discard the rest.
    >
    > I want to avoid reading all the data from the pipe, which can be huge
    > (~3MB). I'm only interested in email headers (ie the first few lines of
    > data).
    >


    Yes, then we've reached an impasse. You can just just continue
    to read the first line of the stream and quit of course ... but then
    the SIGPIPE occurs when the next line gets written to the pipe
    upstream. Does your upstream process there terminate with
    "Broken pipe"... What problem are you trying to solve...?

    As Xho mentioned earlier, you'd have to handle this upstream
    on the write end of the pipe if that's problematic. If you're able to
    catch or ignore the signal from that end or, better yet, filter the
    stream and write only the first line, then the problem is solvable.
    It may be as easy as including a 'local $SIG{PIPE} = 'IGNORE'...
    but it may be more complicated. More details about your program
    are needed.

    --
    Charles DeRykus
     
    comp.llang.perl.moderated, Jan 17, 2007
    #6
    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. Teresa
    Replies:
    2
    Views:
    651
    Rob Thorpe
    Apr 6, 2004
  2. Jane Austine
    Replies:
    2
    Views:
    1,123
    Donn Cave
    Aug 16, 2003
  3. Zoran Bosnjak

    popen3,4 and SIGTERM

    Zoran Bosnjak, Oct 15, 2003, in forum: Python
    Replies:
    0
    Views:
    407
    Zoran Bosnjak
    Oct 15, 2003
  4. Andrew Athan
    Replies:
    0
    Views:
    412
    Andrew Athan
    Jan 6, 2004
  5. Andrew Athan
    Replies:
    0
    Views:
    463
    Andrew Athan
    Jan 6, 2004
Loading...

Share This Page