First off, when you present code for discussion on this site, it helps if the code is reasonably formatted. <code> tags around it are essential, along with indenting the code itself so that its structure becomes clearer to others trying to help you with it. It is always harder to read other people's programs than one's own, so anytime you need to get someone to help you it just makes sense to make it as easy for them as you can.

I've put some comments in the code. You didn't say which parts of it were giving you trouble, so don't get upset if I tell you something you already know. If you have more detailed questions about any of this, or want to correct my interpretation, please feel free. :)

For more help on Perl functions such as fork, wait, etc, do a 'perldoc perlfunc' on your system along with Super Search of this site.

#!/usr/local/bin/perl -w # I assume @dates contains a list of formatted date values # which correspond to a method of naming directories used in the # rest of the code. foreach $datdir (@dates) { # full or 'absolute' file paths based on @dates value $fullpath = (join ('',$ypath, $datdir)); $fulltmp = (join ('', $ytmp, $datdir)); # Ask the OS to make a child process and save its process id in $pi +d my $pid = fork(); if ($pid == 0) #-- start child process code { # Do 'ls -ld $fullpath' on remote system $yte and put its output + in $what $what = `rsh $yte ls -ld $fullpath`; # If that output contains any non-whitespace characters # (which AFAICS looks like it would always be true) if ($what =~/\S/) { # Hit the remote system $yte again and make $fulltmp $what2 = `rsh $yte mkdir -p $fulltmp`; # If the output of the mkdir does not contain any non-whitesp +ace # (the non-whitespace would be an error msg of some sort) if ($what2 !~/\S/) { # This is probably a 'success' exit 1; } } # This is probably 'fail' exit 0; } #-- end child process code # Store $fullpath in %paths hash along with the pid of the child pr +ocess $paths{$pid} = $fullpath; # NB The success (1) or failure (0) of the child process code # revolves around whether the *$fulltmp* directory can be created # though it is $fullpath which is stored # increment child job count $njobs++; # Child jobs "brake" - wait for a child job to complete if # there are more than $maxnjobs children currently running # before continuing the foreach loop while ($njobs >= $maxnjobs) { $waitpid = wait(); # Check return value of child process if ($?) { # Collect 'valid' $fullpath values in @vdates push(@vdates, $paths{$waitpid}); } $njobs--; } } # Collect the exit status and save any 'valid' $fullpath values from # the rest of the child jobs while ($njobs > 0) { $waitpid = wait(); if ($?) { push(@vdates, $paths{$waitpid}); } $njobs--; } # Sort 'valid $fullpaths' lexically my @sorteddates = sort(@vdates); # Return that sorted list return(@sorteddates);

In reply to Re: SOS by virtualsue
in thread Need help understanding legacy code with forking (was : SOS) by perl_virgin

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.