in reply to Need help understanding legacy code with forking (was : SOS)
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);
|
---|