title: Perl - responsible forking
body: <p>I have newly discovered Perl forking and I am quite in love. But one thing concerns me -- if I am just splitting off processes left-and-right, surely this will cause some issue somewhere. Is there a reasonable kind of check that one should use to make that sure that my little application doesn't eat up all of the resources of my machine?</p>
  
  <p>Take this sample code:</p>
  
  <pre><code>foreach my $command (@commands) {
     my $pid = fork();
     if (!$defined $pid) {
       #Fork failed. Do something.
     } elsif ($pid == 0) { #This is the child.
        system($command);
        exit(0)
     }
  }
  
  while (wait() != -1) {} #wait() will be -1 when the last child exits.
  </code></pre>
  
  <p>So this will work fine, and spawn off a process to handle each command. It will all happen in parallel which is great if these commands are completely independent.</p>
  
  <p>What if I suddenly have 5,000+ commands to run through? It wouldn't be wise to thoughtlessly fork off that many processes. So what kind of check should be implemented, and how?</p>
  
  <p>)</p>