c has asked for the wisdom of the Perl Monks concerning the following question:
i'm having some issues with subroutines being ran on a forked
process. the subroutines have print statements which i would like to have
printed to stdout in a format similar to:
however, the print format is not as controlled as it is when i do not fork. sometimes my command line is returned before the final print statement in the loop. i'll see my first three prints, and then my command line prompt, with the fourth print statement appearing after the prompt."\nthis is my message\n\n"
do i need a wait() statement? and if so, where? i'm not too familiar with the function. thanks -c
my code:
#!/usr/bin/perl -w use strict; ## declare a pid for forking my $pid; ## loop through devices for my $node(@devices) { ## if child process exists, move to next device if ($pid = fork) { next; ## begin actions within the child process } elsif(defined($pid)) { ## print a message &log_action($node); ## explicit exit for child process exit; } else { die "Cant fork for some reason! : $!\n"; ## endif } ## end of for loop } sub log_action { my $i = shift; print "\nThis message if for node $i.\n\n"; }
Edit by tye to remove tons of trailing spaces from lines
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Understanding how to fork properly.
by jepri (Parson) on Jan 31, 2002 at 02:37 UTC | |
Re: Understanding how to fork properly.
by particle (Vicar) on Jan 31, 2002 at 02:39 UTC | |
by dvergin (Monsignor) on Jan 31, 2002 at 04:07 UTC | |
by crazyinsomniac (Prior) on Jan 31, 2002 at 04:34 UTC | |
by chromatic (Archbishop) on Jan 31, 2002 at 05:55 UTC | |
by particle (Vicar) on Jan 31, 2002 at 14:06 UTC |
Back to
Seekers of Perl Wisdom