Gangabass has asked for the wisdom of the Perl Monks concerning the following question:
Hi, Monks
I have very strange problem today. I run some code which works in the past but now it did't fork as i expect.
#!/usr/bin/perl use strict; use warnings; my %pids; my @chunk_indixes = find_chunk_indixes( 100, 10 ); foreach my $thread_id ( 0 .. 9 ) { if ( my $pid = fork() ) { $pids{$pid}++; } else { my $start = $chunk_indixes[$thread_id]->[0]; my $end = $chunk_indixes[$thread_id]->[1]; print "[$start, $end]\n"; sleep(10); exit; } while (%pids) { my $pid = waitpid( -1, 0 ); delete $pids{$pid}; } } sub find_chunk_indixes { my ( $total_size, $number_of_chunks ) = @_; my @result; my $small_chunk = int( $total_size / $number_of_chunks ); my $oversized_count = $total_size % $number_of_chunks; my @chunk_sizes = ( ( 1 + $small_chunk ) x ($oversized_count), ($ +small_chunk) x ( $number_of_chunks - $oversized_count ) ); my $current_position = 0; foreach my $chunk_size (@chunk_sizes) { push @result, [ $current_position, $current_position + $chunk_ +size - 1 ]; $current_position += $chunk_size; } return @result; }
I expect it forks 10 process at same time and prints
0, 9 10, 19 20, 29 30, 39 40, 49 50, 59 60, 69 70, 79 80, 89 90, 99at same moment but it prints only one range, wait for ten second and prints another range and so one.
Process explorer shows me only two process of this script. Also i test this on Windows XP in Linux -- same result.
Can you show me what's wrong with this program?
Thanks.
Roman
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: fork() not working as expected
by Crackers2 (Parson) on Mar 21, 2009 at 05:31 UTC | |
|
Re: fork() not working as expected
by zentara (Cardinal) on Mar 21, 2009 at 11:07 UTC |