Fellow monks, I hope you can help me. The code below spawns off child processes and things are done at the child level. My first attempt was to have the child pass back variables to the parent, but that didn't go so well. I think this is (have the child do everything) the best way to handle it. But just for learning purposes how do I retain variables after the child exit? Thanks in advance.
#!/usr/bin/perl -W use scr_rsh; my @properties = ("run","conf"); my $cmd_run = "ps -ef|grep newmatch | grep \" 1 \"|wc -l"; my $cmd_conf = "wc -l /usr/local/multiplex.conf"; my ($pid,@pid); my $passwd = ask; print "\n"; # hash of hashes our $domatch = {}; # Hash of Hashes # set default values of HoH for(my $key1 = 0; $key1 <= $#ARGV; $key1++) { foreach my $key2 (@properties) { my $domatch = { $ARGV[$key1 ] => {$key2=>'0'} }; } } # Fork processes for($key1=0, $i = 1; ($i <= 2) && ($key1 <= $#ARGV); $i++,$key1++) { $pid=fork(); push @pids, $pid; if($pid == 0) { #Child Process $domatch->{$ARGV[$key1]}->{"conf"} = &get_proc_run($p +asswd,$ARGV[$key1]); $domatch->{$ARGV[$key1]}->{"run"} = &get_proc_conf($p +asswd,$ARGV[$key1]); &prt_result($ARGV[$key1]); exit(0); } if("$i" == "2") { $i=0; } } foreach my $childpid (@pids) { waitpid($childpid,0); } #-------------------------------------------- # Beging Sub-Routines #-------------------------------------------- sub prt_result { my $host = shift; if( $domatch->{$host}->{run} eq $domatch->{$host}->{conf} ) { print "$host green: $domatch->{ $host }->{conf}/$domat +ch->{ $host }->{run}\n"; } else { print "$host red: $domatch->{ $host }->{conf}/$domatch +->{ $host }->{run}\n"; } } sub get_proc_conf { my $passwd = shift; my $host = shift; # Get number of newmatch configured to be run on each server my $line = scr_remote("$passwd", "$cmd_conf", "$host"); chomp($line); my($junk,$wc) = split(/\s+/,$line); $wc =~ s/\s//g; # string spaces in a string #$domatch->{$host}->{"conf"} = "$wc"; return $wc; }sub get_proc_run { my $passwd = shift; my $host = shift; # Get PPID of newmatch only my $line = scr_remote("$passwd", "$cmd_run", "$host"); chomp($line); $line =~ s/\s//g; # string spaces in a string #$domatch->{$host}->{"run"} = "$line"; return $line; }

Edit kudra, 2001-11-23 Added additional desc to title


In reply to forking: retaining variables after the child exits by Anonymous Monk

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.