Thank you very much. I want to rename the name of $filename after all child process finished writing. Each child process will write to $filename cumulatively. The purpose of the script is to test network connection for every host. In the "for" loop, itis to create child processes to test the connection concurrencily, and meanwhile write to $filename. The alarm is for network connection timeout, if after 60s, the connection is still unable to setup, then will go to $SIG{ALRM} to print error message.
use strict; use Fcntl qw(:DEFAULT :flock); use FindBin qw($Bin); use lib "$Bin"; use JSON; use POSIX; my $JSON_FILE = $ARGV[0]; open my $file, '<', $JSON_FILE or die("$JSON_FILE Could not open file: + $!\n"); my $data; eval { $data = decode_json(<$file>); print $data; }; if ( $@ ){ print "Json file parsing failed.\n"; } my $targets = $data; my $filename = "/var/log/a.tmp"; my $newfilename = "/var/log/a.log"; open (my $fh, ">", $filename) or die "Could not open file '$filename' +$!\n"; my $time = strftime('%d-%m-%Y %H:%M:%S',localtime); for (my $index=0; $index <= $#$targets; $index++){ my $array = $targets->[$index]{label}; my $hostaddress = $array->{ip}; $time = strftime('%d-%m-%Y %H:%M:%S',localtime); defined(my $pid = fork) or die "fork failed: $!"; unless( defined($pid) ) { flock $fh, LOCK_EX; print $fh "Can't execute check: can't fork(): $!\n"; flock $fh, LOCK_UN; } unless($pid) { # child print "child: $$\n"; my $var; eval { $var = #setup a network connection...; $SIG{ALRM} = sub { flock $fh, LOCK_EX; print $fh "\n"; flock $fh, LOCK_UN;}; alarm(60); }; if( $@ ) { flock $fh, LOCK_EX; print $fh "Connection failed $@\n"; flock $fh, LOCK_UN; } unless( $var ) { flock $fh, LOCK_EX; print $fh "Connection failed"; flock $fh, LOCK_UN; } else { flock $fh, LOCK_EX; print $fh "Connection failed\n"; flock $fh, LOCK_UN; } close($fh); exit; # exit child process } waitpid($pid,0); } print "Parent Process"; close($fh); # Close file handler $fh rename ($filename, $newfilename) or die "Error in renaming $!"; alarm(0);

In reply to Re^6: Multiprocess - child process cannot be finished successfully by wonderG
in thread Multiprocess - child process cannot be finished successfully by wonderG

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.