perl Write filehandle blocks.

Discussion in 'Perl Misc' started by kiranmn@my-deja.com, Apr 29, 2007.

  1. Guest

    hi,

    First parent do a fork, parent waits for child to finish , child
    runs a command "head" and exits. After waiting parent sleep fror 1
    hours and continue doing same thing. when i write a file to t "open2",
    write filehandle, it just hangs there, what is the reason.

    Following is my code. It stops at " now writing to write fd
    ". Please help me.

    Thanking you,
    regards,
    kiran
    ========================================
    #!/usr/bin/perl -I../lib


    #use strict;
    use Carp qw(verbose);
    use IPC::Open2;
    use Symbol;
    my $WTR;
    my $RDR;
    while(1)
    {
    if($kid=fork)
    {
    waitpid($kid,0);
    }
    else
    {
    while(<STDIN>)
    {
    $ref->{"mail"}.=$_;
    }
    $WTR = gensym(); # get a reference to a typeglob
    $RDR = gensym(); # and another one
    $pid=open2($RDR, $WTR, "/usr/bin/head ");
    print "now writing to write fd\n";
    $oo=select $WTR;
    $|=1;
    select $oo;
    print $WTR "$ref->{\"mail\"}";
    print "closing writing fd\n";
    close($WTR);
    print "going to reader\n";
    while(<$RDR>)
    {
    print "$_";
    }
    print "comming out of while reader\n";
    close($RDR);
    exit;
    }
    sleep (3600);
    }
     
    , Apr 29, 2007
    #1
    1. Advertising

  2. <> wrote:


    > #!/usr/bin/perl -I../lib
    >
    >
    > #use strict;



    You lose all of the benefits of strict when you comment it out like that.


    > print "$_";



    perldoc -q vars

    What’s wrong with always quoting "$vars"?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 29, 2007
    #2
    1. Advertising

  3. Guest

    if i run the code like "cat passwd|perl head.pl", if size of passwd
    file is less it works fine, if it is more than 4k it hangs.


    On Apr 29, 1:11 pm, wrote:
    > hi,
    >
    > First parent do a fork, parent waits for child to finish , child
    > runs a command "head" and exits. After waiting parent sleep fror 1
    > hours and continue doing same thing. when i write a file to t "open2",
    > write filehandle, it just hangs there, what is the reason.
    >
    > Following is my code. It stops at " now writing to write fd
    > ". Please help me.
    >
    > Thanking you,
    > regards,
    > kiran
    > ========================================
    > #!/usr/bin/perl -I../lib
    >
    > #use strict;
    > use Carp qw(verbose);
    > use IPC::Open2;
    > use Symbol;
    > my $WTR;
    > my $RDR;
    > while(1)
    > {
    > if($kid=fork)
    > {
    > waitpid($kid,0);
    > }
    > else
    > {
    > while(<STDIN>)
    > {
    > $ref->{"mail"}.=$_;
    > }
    > $WTR = gensym(); # get a reference to a typeglob
    > $RDR = gensym(); # and another one
    > $pid=open2($RDR, $WTR, "/usr/bin/head ");
    > print "now writing to write fd\n";
    > $oo=select $WTR;
    > $|=1;
    > select $oo;
    > print $WTR "$ref->{\"mail\"}";
    > print "closing writing fd\n";
    > close($WTR);
    > print "going to reader\n";
    > while(<$RDR>)
    > {
    > print "$_";
    > }
    > print "comming out of while reader\n";
    > close($RDR);
    > exit;
    > }
    > sleep (3600);
    > }
     
    , Apr 29, 2007
    #3
  4. Joe Smith Guest

    wrote:
    > First parent do a fork, parent waits for child to finish , child
    > runs a command "head" and exits. After waiting parent sleep fror 1
    > hours and continue doing same thing. when i write a file to t "open2",
    > write filehandle, it just hangs there...


    > use IPC::Open2;
    > $pid=open2($RDR, $WTR, "/usr/bin/head ");




    perldoc IPC::Open2

    This whole affair is quite dangerous, as you may block forever. It
    assumes it's going to talk to something like bc, both writing to it and
    reading from it. This is presumably safe because you "know" that com-
    mands like bc will read a line at a time and output a line at a time.
    Programs like sort that read their entire input stream first, however,
    are quite apt to cause deadlock.

    It's cases like this where the parent should fork two children:
    one that can block while writing to $WTR yet not preventing the other
    fork from reading from $RDR at the same time.

    -Joe
     
    Joe Smith, May 2, 2007
    #4
  5. Uri Guttman Guest

    >>>>> "JS" == Joe Smith <> writes:

    JS> perldoc IPC::Open2


    JS> It's cases like this where the parent should fork two children:
    JS> one that can block while writing to $WTR yet not preventing the other
    JS> fork from reading from $RDR at the same time.

    way too much work when an event loop in the parent can handle the open2
    and more all by itself. your idea can still deadlock if the parent
    doesn't manage correctly the i/o of the pair of children.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, May 2, 2007
    #5
  6. Guest

    Uri Guttman <> wrote:
    > >>>>> "JS" == Joe Smith <> writes:

    >
    > JS> perldoc IPC::Open2
    >
    > JS> It's cases like this where the parent should fork two children:
    > JS> one that can block while writing to $WTR yet not preventing the
    > other JS> fork from reading from $RDR at the same time.
    >
    > way too much work when an event loop in the parent can handle the open2
    > and more all by itself.


    I have a hard time seeing how coding a simple fork is more work than
    writing an event loop.

    > your idea can still deadlock if the parent
    > doesn't manage correctly the i/o of the pair of children.


    Sure, and it is possible to code an event loop incorrectly.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , May 2, 2007
    #6
  7. Uri Guttman Guest

    >>>>> "x" == xhoster <> writes:

    x> Uri Guttman <> wrote:
    >> >>>>> "JS" == Joe Smith <> writes:

    >>

    JS> perldoc IPC::Open2
    >>

    JS> It's cases like this where the parent should fork two children:
    JS> one that can block while writing to $WTR yet not preventing the
    >> other JS> fork from reading from $RDR at the same time.
    >>
    >> way too much work when an event loop in the parent can handle the open2
    >> and more all by itself.


    x> I have a hard time seeing how coding a simple fork is more work than
    x> writing an event loop.

    the previous post mentioned TWO forks and managing that is much more
    difficult than one fork with only input or output (or even open2 if
    blocking can't happen). using an event loop eliminated the deadlock
    issue and allows for scaling to more things such as sockets, guis, etc.

    >> your idea can still deadlock if the parent
    >> doesn't manage correctly the i/o of the pair of children.


    x> Sure, and it is possible to code an event loop incorrectly.

    who write event loops anymore? there are several modules that do all the
    work and in general they are fairly easy to use. the biggest issue is
    altering your mindset to a callback style of coding.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, May 2, 2007
    #7
    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. Arjen
    Replies:
    3
    Views:
    447
    Scott Allen
    Feb 27, 2005
  2. matt
    Replies:
    1
    Views:
    273
    George Ogata
    Aug 6, 2004
  3. dnrg
    Replies:
    5
    Views:
    120
    Matija Papec
    Jun 26, 2003
  4. Andrew Hamm

    Archive::Tar and write to an open filehandle

    Andrew Hamm, Oct 9, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    196
    Jens M. Felderhoff
    Oct 9, 2003
  5. Replies:
    6
    Views:
    127
    Lars Haugseth
    Nov 22, 2006
Loading...

Share This Page