Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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 ( [id://1073495]=note: print w/replies, xml ) Need Help??


in reply to Re^2: 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;

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

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1073495]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-19 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found