I really find it difficult to understand what you're asking, sorry for this.

When fork() is called, you end up having two processes: the parent and the child. They both execute the same code (your Perl script), but you can understand if you're in the child testing whether the call returned 0. Then, you're supposed to call exec() (if you really need it) passing the control to the process you want to execute, but this isn't necessary if the code is in the parent script!

The two processes created by fork() are independent, so they will be both running after the call, if this is what you're worried about. Unlike system(), which waits for the child process to complete, after fork() you can do whatever you want in the parent process, even calling fork() two more times to complete the creation of your three subprocesses.

Simple snippet:

#!/usr/bin/perl use strict; use warnings; sub child { print("Hey, I'm child $$\n"); sleep(2 + rand(5)); print("$$ exiting...\n"); exit(0); } foreach (1 .. 3) { my $pid = fork; die "fork(): $@, stopped" unless defined($pid); child() unless $pid; } # Only the parent reaches this point wait foreach (1 .. 3);
which yelds, for example
Hey, I'm child 1048 Hey, I'm child 1368 Hey, I'm child 424 1368 exiting... 424 exiting... 1048 exiting...

Flavio

Don't fool yourself.

In reply to Re^3: launching concurrent processes by polettix
in thread launching concurrent processes 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.