SIGCHLD not called under Windows

K

kalahari

I'm attempting to write a perl app to keep a specific number of jobs
running at the same time, such that when one finishes, a new one is
started to take it's place. I created a test case that works well on my
Linux system, but under Windows 2000 (my intended target for this app)
SIGCHLD is never called. I didn't find anything in perlfork to explain
this anomaly. Perhaps there is a different strategy that will give me
the behavior I need? Here is my code, and the output from both Linux
and Windows.

# test perl forking
print time . "[$$] parent starting\n";

my $cur_children = 0;
my $total_children = 0;
my $max_concurrent_children = 3;
my $max_total_children = 10;

$SIG{CHLD} = \&sig_chld;

while(1) {
if($cur_children < $max_concurrent_children and $total_children <
$max_total_children) {
print time . "[$$] parent spawnding new child\n";
my $frk = fork;
if($frk == 0) {
&child_work;
} else {
$cur_children++;
$total_children++;
}
}
print time . "[$$] parent current children: $cur_children\n";
print time . "[$$] parent total children: $total_children\n";
if($cur_children <= 0 and $total_children >= $max_total_children) {
print time . "[$$] parent exiting\n";
exit;
}
sleep 1;
}

sub child_work {
print time . "[$$] child starting\n";
my $sleep = int(rand(9)) + 1;
print time . "[$$] child sleeping for $sleep seconds\n";
sleep $sleep;
print time . "[$$] child exiting\n";
exit;
}

sub sig_chld {
print time . "[$$] parent caught SIGCHLD\n";
$cur_children--;
$SIG{CHLD} = \&sig_chld;
}


Linux test (expected behavior):
blake@travis ~ $ perl -v

This is perl, v5.8.8 built for i686-linux

Copyright 1987-2006, Larry Wall

Perl may be copied only under the terms of either the Artistic License
or the
GNU General Public License, which may be found in the Perl 5 source
kit.

Complete documentation for Perl, including FAQ lists, should be found
on
this system using "man perl" or "perldoc perl". If you have access to
the
Internet, point your browser at http://www.perl.org/, the Perl Home
Page.

blake@travis ~ $ perl test.pl

1161993981[16762] parent starting
1161993981[16762] parent spawnding new child
1161993981[18724] child starting
1161993981[18724] child sleeping for 9 seconds
1161993981[16762] parent current children: 1
1161993981[16762] parent total children: 1
1161993982[16762] parent spawnding new child
1161993982[2331] child starting
1161993982[2331] child sleeping for 7 seconds
1161993982[16762] parent current children: 2
1161993982[16762] parent total children: 2
1161993983[16762] parent spawnding new child
1161993983[242] child starting
1161993983[242] child sleeping for 4 seconds
1161993983[16762] parent current children: 3
1161993983[16762] parent total children: 3
1161993984[16762] parent current children: 3
1161993984[16762] parent total children: 3
1161993985[16762] parent current children: 3
1161993985[16762] parent total children: 3
1161993986[16762] parent current children: 3
1161993986[16762] parent total children: 3
1161993987[242] child exiting
1161993987[16762] parent caught SIGCHLD
1161993987[16762] parent spawnding new child
1161993987[4903] child starting
1161993987[4903] child sleeping for 8 seconds
1161993987[16762] parent current children: 3
1161993987[16762] parent total children: 4
1161993988[16762] parent current children: 3
1161993988[16762] parent total children: 4
1161993989[2331] child exiting
1161993989[16762] parent caught SIGCHLD
1161993989[16762] parent spawnding new child
1161993989[24361] child starting
1161993989[24361] child sleeping for 5 seconds
1161993989[16762] parent current children: 3
1161993989[16762] parent total children: 5
1161993990[18724] child exiting
1161993990[16762] parent caught SIGCHLD
1161993990[16762] parent spawnding new child
1161993990[30709] child starting
1161993990[30709] child sleeping for 7 seconds
1161993990[16762] parent current children: 3
1161993990[16762] parent total children: 6
1161993991[16762] parent current children: 3
1161993991[16762] parent total children: 6
1161993992[16762] parent current children: 3
1161993992[16762] parent total children: 6
1161993993[16762] parent current children: 3
1161993993[16762] parent total children: 6
1161993994[24361] child exiting
1161993994[16762] parent caught SIGCHLD
1161993994[16762] parent spawnding new child
1161993994[19858] child starting
1161993994[19858] child sleeping for 9 seconds
1161993994[16762] parent current children: 3
1161993994[16762] parent total children: 7
1161993995[4903] child exiting
1161993995[16762] parent caught SIGCHLD
1161993995[16762] parent spawnding new child
1161993995[16691] child starting
1161993995[16691] child sleeping for 8 seconds
1161993995[16762] parent current children: 3
1161993995[16762] parent total children: 8
1161993996[16762] parent current children: 3
1161993996[16762] parent total children: 8
1161993997[30709] child exiting
1161993997[16762] parent caught SIGCHLD
1161993997[16762] parent spawnding new child
1161993997[28121] child starting
1161993997[28121] child sleeping for 7 seconds
1161993997[16762] parent current children: 3
1161993997[16762] parent total children: 9
1161993998[16762] parent current children: 3
1161993998[16762] parent total children: 9
1161993999[16762] parent current children: 3
1161993999[16762] parent total children: 9
1161994000[16762] parent current children: 3
1161994000[16762] parent total children: 9
1161994001[16762] parent current children: 3
1161994001[16762] parent total children: 9
1161994002[16762] parent current children: 3
1161994002[16762] parent total children: 9
1161994003[19858] child exiting
1161994003[16762] parent caught SIGCHLD
1161994003[16762] parent spawnding new child
1161994003[7000] child starting
1161994003[7000] child sleeping for 9 seconds
1161994003[16762] parent current children: 3
1161994003[16762] parent total children: 10
1161994003[16691] child exiting
1161994003[16762] parent caught SIGCHLD
1161994003[16762] parent current children: 2
1161994003[16762] parent total children: 10
1161994004[28121] child exiting
1161994004[16762] parent caught SIGCHLD
1161994004[16762] parent current children: 1
1161994004[16762] parent total children: 10
1161994005[16762] parent current children: 1
1161994005[16762] parent total children: 10
1161994006[16762] parent current children: 1
1161994006[16762] parent total children: 10
1161994007[16762] parent current children: 1
1161994007[16762] parent total children: 10
1161994008[16762] parent current children: 1
1161994008[16762] parent total children: 10
1161994009[16762] parent current children: 1
1161994009[16762] parent total children: 10
1161994010[16762] parent current children: 1
1161994010[16762] parent total children: 10
1161994011[16762] parent current children: 1
1161994011[16762] parent total children: 10
1161994012[7000] child exiting
1161994012[16762] parent caught SIGCHLD
1161994012[16762] parent current children: 0
1161994012[16762] parent total children: 10
1161994012[16762] parent exiting
blake@travis ~ $

Windows test:
D:\tasks>perl -v

This is perl, v5.8.8 built for MSWin32-x86-multi-thread
(with 25 registered patches, see perl -V for more detail)

Copyright 1987-2006, Larry Wall

Binary build 817 [257965] provided by ActiveState
http://www.ActiveState.com
Built Mar 20 2006 17:54:25

Perl may be copied only under the terms of either the Artistic License
or the
GNU General Public License, which may be found in the Perl 5 source
kit.

Complete documentation for Perl, including FAQ lists, should be found
on
this system using "man perl" or "perldoc perl". If you have access to
the
Internet, point your browser at http://www.perl.org/, the Perl Home
Page.


D:\tasks>perl test.pl
1161994177[1800] parent starting
1161994177[1800] parent spawnding new child
1161994177[1800] parent current children: 1
1161994177[-2192] child starting
1161994177[1800] parent total children: 1
1161994177[-2192] child sleeping for 3 seconds
1161994178[1800] parent spawnding new child
1161994178[1800] parent current children: 2
1161994178[1800] parent total children: 2
1161994178[-1636] child starting
1161994178[-1636] child sleeping for 3 seconds
1161994179[1800] parent spawnding new child
1161994179[1800] parent current children: 3
1161994179[1800] parent total children: 3
1161994179[-2944] child starting
1161994179[-2944] child sleeping for 2 seconds
1161994180[-2192] child exiting
1161994180[1800] parent current children: 3
1161994180[1800] parent total children: 3
1161994181[-1636] child exiting
1161994181[1800] parent current children: 3
1161994181[-2944] child exiting
1161994181[1800] parent total children: 3
1161994182[1800] parent current children: 3
1161994182[1800] parent total children: 3
1161994183[1800] parent current children: 3
1161994183[1800] parent total children: 3
1161994184[1800] parent current children: 3
1161994184[1800] parent total children: 3
1161994185[1800] parent current children: 3
1161994185[1800] parent total children: 3
1161994186[1800] parent current children: 3
1161994186[1800] parent total children: 3
1161994187[1800] parent current children: 3
1161994187[1800] parent total children: 3
1161994188[1800] parent current children: 3
1161994188[1800] parent total children: 3
1161994189[1800] parent current children: 3
1161994189[1800] parent total children: 3
1161994190[1800] parent current children: 3
1161994190[1800] parent total children: 3
1161994191[1800] parent current children: 3
1161994191[1800] parent total children: 3
1161994192[1800] parent current children: 3
1161994192[1800] parent total children: 3
1161994193[1800] parent current children: 3
1161994193[1800] parent total children: 3
1161994194[1800] parent current children: 3
1161994194[1800] parent total children: 3
1161994195[1800] parent current children: 3
1161994195[1800] parent total children: 3
1161994196[1800] parent current children: 3
1161994196[1800] parent total children: 3
1161994197[1800] parent current children: 3
1161994197[1800] parent total children: 3
....loops indefinately
 
K

kalahari

In case anyone else is interested, the solution I found is to use
waitpid(). This works on both platforms:

use POSIX ":sys_wait_h";

print time . "[$$] parent starting\n";

my $cur_children = 0;
my $total_children = 0;
my $max_concurrent_children = 3;
my $max_total_children = 10;

while(1) {
if($cur_children < $max_concurrent_children and $total_children <
$max_total_children) {
print time . "[$$] parent spawnding new child\n";
my $frk = fork;
if($frk == 0) {
&child_work;
} else {
$cur_children++;
$total_children++;
}
}
print time . "[$$] parent current children: $cur_children\n";
print time . "[$$] parent total children: $total_children\n";
if($cur_children <= 0 and $total_children >= $max_total_children) {
print time . "[$$] parent exiting\n";
exit;
}
waitpid(-1,WNOHANG);
if($? == 0) {
print time . "[$$] parent saw child exit\n";
$cur_children--;
}
sleep 1;
}

sub child_work {
print time . "[$$] child starting\n";
my $sleep = int(rand(9)) + 1;
print time . "[$$] child sleeping for $sleep seconds\n";
sleep $sleep;
print time . "[$$] child exiting\n";
exit;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top