Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Okay, this may be difficult to follow along to; but I will do my best.

When you fork, your children processes get an exact replica of the process. So, your first child gets spawned. Now, it has a copy of $i which has the value zero. So, it is spawned, it runs the phork() subroutine, and it returns to the for loop with a $i of 0. So, it beings to execute the loop; and your first child process forks its own child and that child (the first child of the first child of the parent) is spawned and it gets a copy of the memory block who has a $i of 1. This stuff happens some more with the second child of the parent and so on. What you want is something along these lines: (warning: untested)
#!/usr/bin/perl -w use strict; my $childLimit = shift @ARGV; #get total number of children my @pids = (); #array of pids of children for(my $i = 0; $i < $childLimit; $i++) { if( !( $pids[$i] = fork() ) ) { # in the child callChildSub(); exit(0); # exit from the child process IMPORTANT } } foreach my $pid (@pids) { waitpid $pid, 0; } sub callChildSub() { print "I am the child with pid $$.\n"; }
Of course, that needs some error-checking and such, but that should get you started.

Jeremy

In reply to Re: Re: Re: A Quick fork theory question by enoch
in thread A Quick fork theory question by E-Bitch

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-29 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found