Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Uninitialized value in numeric eq (==) after 65 iteration of a loop using fork?!?

by doowah2004 (Monk)
on Sep 13, 2004 at 14:18 UTC ( [id://390562]=perlquestion: print w/replies, xml ) Need Help??

doowah2004 has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I have been farting and fooling around with fork, and I was trying to see the time difference in having child processes create more child processes to complete a task vs the parent creating all the children. so here is what I did:

#!/usr/bin/perl -w use strict; use Time::HiRes qw(usleep ualarm gettimeofday tv_interval); open (FILE, ">testfork.txt"); my $runs = 33; my $start = [gettimeofday]; while ($runs) { my $pid = fork; # Parent if ($pid) { $runs--; } # Child if ($pid == 0) { my $pid = fork; if ($pid == 0) { my $pid = fork; if ($pid == 0) { print FILE "$runs\n"; exit; } print FILE "$runs\n"; exit; } print FILE "$runs\n"; exit; } } my $elapse = tv_interval ($start); close (FILE); print "Elapsed time = $elapse\n"; exit;
This should create 99 lines in the output file and it does. I then change the code to:


#!/usr/bin/perl -w use strict; use Time::HiRes qw(usleep ualarm gettimeofday tv_interval); open (FILE, ">testfork.txt"); my $runs = 99; my $start = [gettimeofday]; while ($runs) { my $pid = fork; # Parent if ($pid) { $runs--; } # Child if ($pid == 0) { # my $pid = fork; # if ($pid == 0) { # my $pid = fork; # if ($pid == 0) { # print FILE "$runs\n"; # exit; # } # print FILE "$runs\n"; # exit; # } print FILE "$runs\n"; exit; } } my $elapse = tv_interval ($start); close (FILE); print "Elapsed time = $elapse\n"; exit;


This should also create 99 lines in the output file, but consistently after 65 lines I get the following:

Use of uninitialized value in numeric eq (==) at testfork1.pl line 16.

I can not see why this is happening. Does this happen to anyone else running the code? If can anyone enlighten me on why?

Thank you all in advance,

Cameron

Replies are listed 'Best First'.
Re: Uninitialized value in numeric eq (==) after 65 iteration of a loop using fork?!?
by ccn (Vicar) on Sep 13, 2004 at 14:24 UTC

    perldoc -f fork

    It returns the child pid to the parent process, 0 to the child process, or "undef" if the fork is unsuccessful.

    When you compare undefined $pid with any number you get warning

    You can make something like this: die "Can't fork: $!" unless defined $pid;

Re: Uninitialized value in numeric eq (==) after 65 iteration of a loop using fork?!?
by zejames (Hermit) on Sep 13, 2004 at 14:30 UTC
    Your code is working fine here (ActivePerl 5.8 and Windows XP). I have to agree with ccn : there may be a system limit preventing you from create 99 child processes at once. Could be give us more information about your system limit ?

    Kind regards

    --
    zejames
      Apparently that is the problem, I updated the code with die and I get:
      Can't fork: Resource temporarily unavailable at testfork1.pl line 14.

      There must be some sort of limit. My system is ActivePerl 5.8.3, Win XP SP2. I am unsure how to check or change the ability of my system to create child processes, but it looks like that might be the problem. It is interesting that I did not run into this with the code that had children creating children?

      zejames, out of curiousity, what times did you get when running the code?

      Thanks all for your quick and correct insight to my prob.

      Cameron
        In fact, your code did not work on my machine either : I ran it under Perl builder, which provided me no error except that it did not terminate it. Running it on the comment line was more interesting and gave the same result as your.

        Interesting to see a different behaviour when using Perl Builder or not.

        So I've got no figure for you ;)


        --
        zejames
Re: Uninitialized value in numeric eq (==) after 65 iteration of a loop using fork?!?
by BrowserUk (Patriarch) on Sep 13, 2004 at 20:40 UTC

    There is (or appears to be, though I haven't found it documented anywhere) a 64 (concurrent) child limit to the win32 fork emulation. This is probably due to the use of WaitForMultipleObjects(Ex) to implement wait and/or waitpid which has a maximum number of "objects" it can wait on WinNT.h:#define MAXIMUM_WAIT_OBJECTS 64     // Maximum number of wait objects

    This limit could be lifted, as it has been with threads (win32 forks are threads underneath).


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: Uninitialized value in numeric eq (==) after 65 iteration of a loop using fork?!?
by TheEnigma (Pilgrim) on Sep 13, 2004 at 16:25 UTC
    I've never used fork before, so I tried your code to see if I could learn something. I got the same errors as the rest of you, failing after 65 lines (printing 99 to 35).

    But if I add either of the following before the child if statement

    if(!defined $pid){ die "$runs: $!"; } OR if(!defined $pid){ print FILE "undefined at $runs\n"; $runs--; next; }

    it only prints 64 times (99 to 36). Actually, that's what I would expect.

    Why do we get an error on the if ($pid == 0) { line, but then it still goes and prints one more time?

    I also find it interesting that when I use my second if statement above, it prints down to 37, then all the undefined lines; and then it prints 36. Is there something significant about that?

    TheEnigma

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://390562]
Approved by ccn
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-26 01:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found