Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Dear Monks,

I'm trying to write a program which will calls a subroutine with a given sourceid.
There can be around 200 sourceids which are nothing but directory names.
The script should initially spawn 4 childs and call the subroutine with a
given sourceid (the sourceid cannot be repeated). Script should create new childs
as and when a child finishes the job and continue until all the 200 sourceids are processed.

Below is the code I've tried. My only question is
Is there anyway of doing the same thing in a simple manner and the most important thing
how do I pass the sourceids(unique) to the subroutine.
#!/usr/bin/perl -w my $NumChilds = 4; @sourceids=("aex", "athena", "athens","bidapa","bbic","bony","capctha" +,"cooldo","italy","paris","xmltech"); my $sourcecount = scalar(@sourceids)."\n"; # This section spawns the initial 4 children. for ($counter = 1; $counter <= $NumChilds; $counter++) { #foreach my $source (@sourceids){ $pid = fork(); if ($pid) # parent { $child{$pid} = $counter; print "forked process $counter.\n"; } else # child { callScan(); #should be callScan($source); exit $counter; } # } } # spawns additional children . $diecount = 0; while ($counter <= $sourcecount) { $doneproc = wait(); $doneval = $? >> 8; $pid = fork(); if ($pid) # parent { $child{$pid} = $counter; print "child $doneval ($doneproc) exited, forking process $cou +nter.\n"; $counter++; $diecount++; } else # child { callScan(); #should be callScan($source); exit $counter; } } # waits for all children to die. while ($diecount <= $sourcecount) { wait(); $diecount++; } # subroutine. sub callScan { sleep(2); print "callScan with counter = $counter.\n"; } print "Done.\n";
Thanks in advance

In reply to Fork limited childs and passing parameters by chanakya

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? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2023-02-07 11:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (39 votes). Check out past polls.

    Notices?