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

comment on

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

The "fork" call starts another copy of original process. That copy continues running from exactly the same position as the parent. The only difference is the value returned from "fork". So it is obvious, that if one of those 2 processes calls "exec" and the other calls "sleep", then there's no more processes left to start one more bot. So, your second bot is started after the parent kills first bot.

So, to really have 2 processes running independently, you have to come up with some strategy for freeing parent process. In the simplest (and ugly) case, you can use 2 forks to launch 1 bot. Something like

sub fork_bot{ my $arg = shift; my $pid = fork(); die "Can't fork: $!\n" unless defined $pid; return if $pid != 0; $pid = fork(); die "Can't fork: $!\n" unless defined $pid; if($pid == 0){ exec($arg->{bot}) or die "Can't start $arg->{bot}: $!\n"; } sleep $arg->{runtime}; kill 1, $pid or die "Can't kill $arg->{bot}: $!\n"; exit(0); }
Your main code, after starting bots, may do then something like
my $chld; do{ $chld = wait(); }while($chld >= 0);
Again, this code will work, but it is not very useful. The bots that you start this way won't be able to communicate with each other or with the parent. The style of communication (one way or both ways) shall define the complexity of the system that you have to design.


In reply to Re: Forking two processes in parallel by andal
in thread Forking two processes in parallel by neilwatson

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 surveying the Monastery: (5)
As of 2024-03-28 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found