Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Learning to use fork()

by ovedpo15 (Pilgrim)
on Jan 15, 2019 at 01:14 UTC ( [id://1228565]=note: print w/replies, xml ) Need Help??


in reply to Learning to use fork()

Some progress:
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";
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?

Replies are listed 'Best First'.
Re^2: Learning to use fork()
by Aldebaran (Curate) on Jan 16, 2019 at 04:59 UTC
    It really feels messy and not clear.

    fork is fraught in perl without it really being perl's fault. I had recently endeavored to do some multitasking project that way and became convinced that the implementation is not worth the trade-offs, the first being that it must occur in an eval/die pair. My source looks about like yours, and my results were just about as unsatisfying as well. This is about where I dealt with it first: Re^2: using online translation engines with perl.

    Also, should I kill the child process when its done by using exit?

    There were a couple posts on that thread that showed how to kill the processes. I used a different way to get done what I needed.

Re^2: Learning to use fork() (use Proc::Background)
by Anonymous Monk on Jan 15, 2019 at 04:03 UTC

    It really feels messy and not clear

    11_42

    You're really asking how to clean 11_42? 11_42 you say? What is 11_42? Exactly!

    1 use Proc::Background

    #!/usr/bin/perl -- use strict; use warnings; use Proc::Background; my @jobs = ( [ $^X, 'job1.pl' ], [ $^X, 'job2.pl' ], [ $^X, 'job3.pl' ], [ $^X, 'job4.pl' ], [ $^X, 'job5.pl' ], [ $^X, 'job6.pl' ], [ $^X, 'job7.pl' ], [ $^X, 'job8.pl' ], [ $^X, 'job9.pl' ], [ $^X, 'job10.pl' ], [ $^X, 'job11.pl' ], [ $^X, 'job12.pl' ], [ $^X, 'job13.pl' ], ); my @procs = map { Proc::Background->new( @$_ ) } @jobs; while( @procs ){ @procs = { $_->alive ? $_ : ItsOverMaybeRestart($_) } map @procs; sleep 1; } sub ItsOverMaybeRestart { my( $proc ) = @_; ... return; }
      @procs = { $_->alive ? $_ : ItsOverMaybeRestart($_) } map @procs;

      I don't know anything about Proc::Background, but shouldn't that be something like

      @procs = map { $_->alive ? $_ : ItsOverMaybeRestart($_) } @procs;
      ?


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1228565]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found