Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Managing the fork/execing and reaping of child processes

by Anonymous Monk
on Jul 16, 2015 at 16:56 UTC ( #1135051=note: print w/replies, xml ) Need Help??


in reply to Managing the fork/execing and reaping of child processes

Simplify and add lightness (oh, wait, adding lightness is for aircraft design :)

#!/usr/bin/perl # http://perlmonks.org/?node_id=1135034 use strict; $| = 1; my $active_readers = 0; my $current_reader_limit = 10; my $total = 0; while(1) { if($active_readers < $current_reader_limit) { $active_readers++; ++$total; if(my $pid = fork) # parent { print "Spawned child $active_readers pid $pid total $total\n"; } elsif(defined $pid) # child { exec "echo Hello from child $active_readers pid \$\$" or die "exec failed with $!"; } else # fork failed { die "fork failed with $!"; } } elsif((my $pid = wait()) > 0) { $active_readers--; print "Reaped $pid, active = $active_readers\n" } }

Replies are listed 'Best First'.
Re^2: Managing the fork/execing and reaping of child processes
by Anonymous Monk on Jul 16, 2015 at 18:10 UTC
    Rendered readable:
    #!/usr/bin/perl # http://perlmonks.org/?node_id=1135034 use strict; $| = 1; my $active_readers = 0; my $current_reader_limit = 10; my $total = 0; while(1) { if($active_readers < $current_reader_limit) { $active_readers++; ++$total; if(my $pid = fork) { # parent print "Spawned child $active_readers pid $pid total $tota +l\n"; } elsif(defined $pid) { # child exec "echo Hello from child $active_readers pid \$\$" or die "exec failed with $!"; } else { # fork failed die "fork failed with $!"; } } elsif((my $pid = wait()) > 0) { $active_readers--; print "Reaped $pid, active = $active_readers\n" } }
      Sigh...
Re^2: Managing the fork/execing and reaping of child processes
by mr_mischief (Monsignor) on Jul 16, 2015 at 19:19 UTC

    If the fork fails, you're still incrementing both $active_readers and $total. Those should probably be tracked in the parent.

      If the fork fails, the parent dies. Is that tracking enough? I figure that if the fork fails it's going to muck things up enough that the die is proper.

        Okay, let me put it this way. If one wanted to not just die and to attempt to actually deal with not being able to fork more than X children even though Y children were requested, the example would need more changes than just removing the die() and reporting the error. The counts would also need to be moved. So in an example program that's going to be expanded upon and altered, it may be fruitful to track those things in the proper place from the beginning.

Re^2: Managing the fork/execing and reaping of child processes
by Anonymous Monk on Jul 16, 2015 at 17:07 UTC
    It works. That's beautifully simple. Now to debug what I actually WANTED to debug. I can't thank you enough.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1135051]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2017-12-12 01:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (323 votes). Check out past polls.

    Notices?