Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I think I know what you are asking, but I'm not sure.

You want to, for an example, write a spell check program, in which you want each letter to be a child process:

##Extremely simple code for $x ('A'..'Z') { if ($pid=fork) { ## This child checks the letter $x exit; } }

The problem is that you want to "simultaneously execute the subroutine" AND you want the children to return their results in the exact order they were called. There is no simple way to have both: since each child is doing a different task, you have no way of knowing how soon each will finish. You can have the parent wait for the a child to finish before starting the next child, but that's the same as a non-forked loop.

If the return order does not matter, you can just gather the children's results as they finish. (How exactly you do this depends on a few things: appending to a temporary file may be the easiest way.)

If the order DOES matter, you must have a way for the parent to identify the children. In the example above, the child could output it's "letter" as the first character of it's result. Then it is up to the parent to sort out the results and display them in a pretty manner.

Here's some quick code illustrating some of the above:

$tmpfile = "/tmp/results.$^T$$"; for $x ('A'..'Z') { if (fork) { ## We don't need to know PID in this example $results = &SpellCheck($x); if (open (TMPFILE, ">> $tmpfile")) { print TMPFILE "$x|$results\n"; close(TMPFILE); } exit; ## Done with this child } } ## Wait for all the children to finish, then... if (open (TMPFILE, $tmpfile)) { @results = <TMPFILE>; close(RESULTS); sort(@results); ## Works well for this example!! }

In the example above, @results will contain the results from all 26 children, sorted in alphabetic order.


In reply to Re: Fork/Spawn by turnstep
in thread Fork/Spawn by Silas

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 avoiding work at the Monastery: (5)
As of 2024-04-25 10:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found