sub REAPER { # don't work almost on windows
while (($child = waitpid(-1,WNOHANG)) > 0) {}
$SIG{CHLD} = \&REAPER;
}
$SIG{CHLD} = \&REAPER;
use POSIX ":sys_wait_h";
$|=1;
#$pid=fork(); # when i use setpriority i call a fork here and replace below in the if sentence.
#$pid1=fork();
#$pid2=fork();
#setpriority($$,0); # not implement on windows :-(
#setpriority($pid,1);
#setpriority($pid1,2);
#setpriority($pid2,3);
my %CHILD = ();
sub REAPER { # don't work almost on windows
while (($child = waitpid(-1,WNOHANG)) > 0) {}
$SIG{CHLD} = \&REAPER;
}
$SIG{CHLD} = \&REAPER;
# if i do something like $SIG{CHLD} = sub { print "it's work"; };
# well ... nothing happen, why?
if (!($CHLD{1} = fork())) { # when use setpriority i replace "if (!($CHLD{1} = fork())) {" to "if (!$pid) {"
print "child 1 -> $$\n";
sleep 2; # other small thing, why this sleep don't work here and sleep the parent process?
exit; # but only for 2 seconds, if i have three sleep with 2 seconds 2*3 = 6 seconds,
# itn't it? yeah yeah i'm lost ;-(
}
if (!($CHLD{2} = fork())) {
print "child 2 -> $$\n";
sleep 2;
exit;
}
if (!($CHLD{3} = fork())) {
print "child 3 -> $$\n";
sleep 2;
exit;
}
# if i replace $CHILD{1} with -1... dosen't work :-(
# obviusly, when i use this i don't use the signal $SIG{CHLD}
# if i only put one waitpid... well don't work
#waitpid $CHILD{1}, WNOHANG; # if i put wait; the script work fine
#waitpid $CHILD{2}, WNOHANG; # wait;
#waitpid $CHILD{3}, WNOHANG; # wait;
print "parent process\n";
Edited by [planetscape] - added [id://17558|code] tags and line breaks
#!/usr/bin/perl
use warnings;
use strict;
use threads;
my $thr1 = threads->new(\&sub1);
my $thr2 = threads->new(\&sub2);
my $thr3 = threads->new(\&sub3);
my $ReturnData1 = $thr1->join;
print "Thread1 returned @$ReturnData1\n";
my $ReturnData2 = $thr2->join;
print "Thread2 returned @$ReturnData2\n";
my $ReturnData3 = $thr3->join;
print "Thread3 returned @$ReturnData3\n";
print "Press any key to exit\n";
<>;
##############################################
sub sub1 {
my @values = ('Fifty-six','foo', 1);
sleep 1;
return \@values;
}
##############################################
sub sub2 {
my @values = ('Forty-two','bar', 2);
sleep 2;
return \@values;
}
##########################################
sub sub3 {
my @values = ('Sixty-six','baz', 3);
sleep 3;
return \@values;
}
# join() does three things: it waits for a thread to exit,
# cleans up after it, and returns any data the thread may
# have produced.
You may view the original node and the consideration vote tally.
You've got several issues going on. First, if you're using fork on Windows, you're really using Perl threads behind the scenes. See [doc://perlfork]. If you want to work with subprocesses on Windows, I recommend checking out [mod://Win32::Job]. As mentined above, getting data back from a fork is harder than getting it back from a thread -- you probably need to use some sort of external file or pipe. See [doc://perlipc]. If you're trying to do multiple child processes at the same time, you've got added complexity in handling returned data.
For managing multiple, forked processes in general, I've seen many recommendations for [mod://Parallel::ForkManager] as a cleaner interface. There's also a tutorial on it: [id://291446]. It looks like what you want can be boiled down to something like this (adapted from the docs for it):
use Parallel::ForkManager;
$pm = new Parallel::ForkManager($MAX_PROCESSES);
my @task_details = (
# data needed for each sub-process
);
foreach my $task (@task_details) {
$pm->start and next; # do the fork
# do the sub process work using whatever details are in $task
# write the results to a file somewhere, perhaps with a
# pre-set name passed in $task
$pm->finish; # do the exit in the child process
}
$pm->wait_all_children;
# Continue in parent and read in the results
-xdg
Code written by xdg and posted on PerlMonks is [http://creativecommons.org/licenses/publicdomain|public domain]. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
perlmonks.org content © perlmonks.org and apolo, NodeReaper, xdg, zentara
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03