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:
"\nthis is my message\n\n"
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.
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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|