Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Please Explain the Parallel::ForkManager Idiom my $pid = $pm->start and next;

by Jim (Curate)
on Feb 04, 2014 at 16:56 UTC ( #1073442=note: print w/replies, xml ) Need Help??


in reply to Re: Please Explain the Parallel::ForkManager Idiom my $pid = $pm->start and next;
in thread Please Explain the Parallel::ForkManager Idiom my $pid = $pm->start and next;

Gosh, I remember reading bart's excellent tutorial Mr. Peabody Explains fork() years ago when fork() was relatively new to Perl on Windows. Obviously, I wasn't able to wrap my head around it then, and I continue to struggle with it today. But I'm more determined now than I was then to understand how it works.

At least one light bulb has gone on above my head after reading this and seeing its accompanying graphic in blue and green text:

What fork() does is extrordinary! It takes the existing process and clones it. If you're familiar with Star Trek, this is like a bad transporter accident. An exact copy is made of each process, and each process is almost unaware of the other.

The code is executed instruction-by-instruction until the fork() call is reached. At that point, the process is cloned and now there are two identical processes running the instruction right after the fork().

In fact, they're so identical that only one bit of information distinguishes between them: the return value of fork(). In the child process fork() appears to have returned 0. In the parent process, fork() appears to have returned a non-zero integer. This is how parent and child tell themselves apart.

The important difference here is the explanation that "the code is executed instruction-by-instruction until the fork() is reached." This is the first time I've seen it plainly stated that fork() alters the ordinary sequential execution of statements in a Perl program. This helps me begin to understand what's going on in the mystifying example program I'm studying.

I likened fork() in Perl to job control in the Unix shell, but I'm now realizing they're not the same thing at all. A shell loop that launches commands in the background with & is not the same as a fork() in Perl.

Jim

Replies are listed 'Best First'.
Re^3: Please Explain the Parallel::ForkManager Idiom my $pid = $pm->start and next;
by Anonymous Monk on Feb 05, 2014 at 00:20 UTC

    Here is a forking program expressed as a tree (this is parent)

    • code before fork
    • fork call
    • code after fork

    The child process execution is

    • fork call
    • code after fork

    When the fork call is reached, the parent process creates a child process (clone), and returns a pid for the new child process

    The child process doesn't clone/create a new child process (only parent does that), and fork returns zero

    After that the two processes are identical, and each of them continues execution from the point of fork ... they both execute code after fork

    The child doesn't start from beginning to execute code before fork (its not a new process, its a clone of the parent)

     

    When you run the program here is the execution order as a tree

    • before fork
    • fork() ....
        • parent clones child, and gets not-zero (pid of child)
        • code after fork is executed
        • child gets zero (only parent forks/clones itself)
        • code after fork is executed

    Or the same in table tree form :) the parent process is started and it runs
    code before fork
    fork call (I parent clone child here) I child gets 0 and I run in parallel
    code after fork code after fork

Re^3: Please Explain the Parallel::ForkManager Idiom my $pid = $pm->start and next;
by soonix (Monsignor) on Feb 06, 2014 at 10:45 UTC
    plainly stated that fork() alters the ordinary sequential execution of statements

    No. It doesn't state that. It emphasizes that both before and after the fork() there is no difference except for fork's return value.

    The only alteration is: after the fork(), there are two identical copies of those instructions. If there were is no if around the fork() (or you save the return value to a variable and evaluate it afterwards), you would not be able to distinguish between them. The "ordinary sequential execution" is not altered.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1073442]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2018-07-23 09:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (461 votes). Check out past polls.

    Notices?