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.
-
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.