Update: waitpid added to serialize the kids in example code.

First, I suggest you enable strict and warnings. Multiprocess code has plenty of capacity to confuse about lexical scopes.

The order in which child processes execute is entirely up to the OS scheduler. It cannot even be said whether the parent or the child will run first. You are correct that print buffering is an issue in general, but your code is written so that the only printing is done in the parent.

Your program is actually exiting the parent and the loop is continuing in the child. Is that what you mean to do? That is one reason your prints are getting so out of order. Here's a version which does one print in the parent and the other in the child, with the original parent doing all the forking:

#!/usr/bin/perl -w use strict; my @LIST = 'A'..'E'; my $i=0; $|++; # Unbuffered print on STDOUT for (@LIST) { my $pid; if ($pid = fork) { # Parent, $pid is true, is certainly defined print "Mother says $_ is in position $i, with pid $pid\n"; # waitpid serializes the kids waitpid $pid, 0; } # $pid is 0 in the child elsif (defined $pid) { print "Child says That's what I said... $_ is in position ", "$i, with pid $$\n"; exit 0; } # $pid undefined -- error else { die "Error in forking at $i: $!"; } $i++; } =pod That's what I said... A is in position 0, with pid 19041 A is in position 0, with pid 19041 B is in position 1, with pid 19042 That's what I said... B is in position 1, with pid 19042 That's what I said... C is in position 2, with pid 19043 C is in position 2, with pid 19043 D is in position 3, with pid 19044 E is in position 4, with pid 19045 That's what I said... D is in position 3, with pid 19044 That's what I said... E is in position 4, with pid 19045 =cut

After Compline,
Zaxo


In reply to Re: deterministic fork by Zaxo
in thread deterministic fork by Anonymous Monk

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.