in reply to while/fork problem

Replacing for(<IN>) with while(<IN>) works fine. The for() reads the entire file and builds a list to iterate over, while the while() just reads one line at a time. Inside the loop the $_ is just the same. With modification in two places
#here while (<IN>) { #for (<IN>) { chomp; $rcount++; $tcount++; print "Progress: $tcount\n"; # changed \r to \n $pid = fork; if ($pid==0) { # child close IN; print "child pid $$\n"; # new debug line # start call sleep sub

your script produces

Progress: 1 child pid 21079 Progress: 2 child pid 21080 Progress: 3 child pid 21081 Progress: 4 child pid 21082 ...

so I guess it works as expected. It is definitely a better to use while() if you don't need the entire bunch of lines (e.g. to sort them first), but one line at a time.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: while/fork problem
by hammopau (Novice) on Jun 13, 2007 at 07:40 UTC
    shmem,

    Thanks for the reply. The problem is only encountered when the waitpid function is called when the max number of runs is hit. The sleep is currently used to make sure this happens. Are you still calling the sleep. If you are calling waitpid and the 'while' works ok, it may be a problem with our version of perl.

    Can you pls confirm & thanks again...

    Paul