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, 99
at 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


In reply to fork() not working as expected by Gangabass

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.