Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > perl Write filehandle blocks.

Reply
Thread Tools

perl Write filehandle blocks.

 
 
kiranmn@my-deja.com
Guest
Posts: n/a
 
      04-29-2007
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);
}

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      04-29-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> 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
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
kiranmn@my-deja.com
Guest
Posts: n/a
 
      04-29-2007
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, (E-Mail Removed) 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);
> }



 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      05-02-2007
(E-Mail Removed) 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
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      05-02-2007
>>>>> "JS" == Joe Smith <(E-Mail Removed)> 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 ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      05-02-2007
Uri Guttman <(E-Mail Removed)> wrote:
> >>>>> "JS" == Joe Smith <(E-Mail Removed)> 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
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      05-02-2007
>>>>> "x" == xhoster <(E-Mail Removed)> writes:

x> Uri Guttman <(E-Mail Removed)> wrote:
>> >>>>> "JS" == Joe Smith <(E-Mail Removed)> 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 ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl Strings vs FileHandle shadabh Perl Misc 9 09-30-2008 03:59 AM
use print to write binary data to a filehandle seems not correct quakewang@mail.whut.edu.cn Perl Misc 6 11-22-2006 06:30 PM
$v = shift OR$_[0] on a filehandle, whats teh difereence Eduard W. Lohmann Perl 1 12-02-2003 06:30 PM
Archive::Tar and write to an open filehandle Andrew Hamm Perl Misc 1 10-09-2003 09:24 AM
Write to a filehandle and to STDOUT without double print statements? dnrg Perl Misc 5 06-26-2003 04:39 PM



Advertisments