*** I know I've posted this twice, because the question was duplicated and I didn't know which one would get reaped. If this is against etiquette, please /msg me.

Here's some example code with the fork()'s in place:

#! /usr/bin/perl -w use strict; $SIG{CHLD} = \&REAPER; my @server_list = qw/one two three four/; my $children = 0; foreach my $server (@server_list) { print "$$ Forking for server: $server\n"; if (my $pid = fork) { # Parent process # Nothing to do except go to the next server in the list $children++; sleep 2; next; } else { # Child process # Note that you *must* use 'exit;' when processing is # finished or the child will continue going through the # loop print "$$ Processing for server: $server\n"; sleep 3; exit; } } # Wait for children to finish while ($children > 0) { sleep; } sub REAPER { my $pid = wait; print "Process $pid has done it's stuff!\n"; $children--; }
When a forked process dies, the parent process will get a SIGCHLD signal (I am assuming you are using UNIX). Unless the parent does something with this signal, the child process will becoma a 'zobmie', and eventually the process table will fill up (not good). If you don't need to keep track of the children as they finish, do $SIG{CHLD} = 'IGNORE'. I've put some delays in the example code so you can see this happening.

You should also be aware that as with any system call, a fork() can fail. Read page 715 of 'Programming Perl, 3rd edition' for more info.

JJ


In reply to Re: simoultaneous connections using Net::SSH::Perl by jj808
in thread simoultaneous connections using Net::SSH::Perl by vxp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.