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


In reply to Understanding how to fork properly. by c

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.