in reply to Learning to use fork()
It really feels messy and not clear. Also, it is not the most efficient because ALL of the 15 calls are not depend one on another. with that logic I can create 15 if-else statements but of course it is not readable and clear. Also, the names are not the same (and all of them gets arguments - I removed them so it will be more easy to see). How should I solve it? Also, should I kill the child process when its done by using exit?my $forks = 0; foreach my $i (1..3) { if ($i == 1 && fork() == 0) { $forks++; print "Running first with $$ \n"; call_sub1(); call_sub2(); call_sub3(); call_sub4(); call_sub5(); call_sub6(); exit; } elsif ($i == 2 && fork() == 0) { $forks++; print "Running second $$ \n"; call_sub1(); call_sub10(); call_sub3(); call_sub13(); call_sub5(); exit; } else { if (fork() == 0) { $forks++; print "Running third with $$ \n"; call_sub1(); call_sub4(); call_sub5(); call_sub15(); exit; } } } for (1 .. $forks) { my $pid = wait(); print "Parent saw $pid exiting\n"; } print "done\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Learning to use fork()
by Aldebaran (Curate) on Jan 16, 2019 at 04:59 UTC | |
|
Re^2: Learning to use fork() (use Proc::Background)
by Anonymous Monk on Jan 15, 2019 at 04:03 UTC | |
by AnomalousMonk (Archbishop) on Jan 15, 2019 at 04:51 UTC |