Hi, folks

ActivePerl's fork emulation has much improved in perl 5.8.4 and I've been testing it on NT and have received some 'surprising' results.

My input file was 10_000 lines long, one 'computername' per line of the form computer00001, computer00002 etc. The script spawns (correctly) ten threads which each read from the file then prints it to a results file with the PID of the thread.

All 10_000 computernames are in the results file, in order with no duplicates. What surprised me was that each thread appears to process 256 consecutive lines from the input file even though the threads all 'take a turn'. It's not important but I wondered if anyone could shed any light on why it would do that?

When the output is shown on STDOUT each thread is takings its turn but jumping around the file, rather than taking sequential lines... not what I expected to see at all.

#!C:/ActivePerl5.8.4/bin/perl.exe use strict; use warnings; require 5.8.4; #We only run if we're using Perl v5.8.4 $|++; #Unbuffer I/O my @pids=(); my $parentpid=$$; my $pid; print "The parents PID -s $parentpid\n"; my $thread_counter=0; my $testinput = "C:/Logchecks/test/forklist.txt"; my $testoutput = "C:/Logchecks/test/forkresults.csv"; open (IN,"<$testinput") or die("can't open input file $!\n"); open (OUT,">$testoutput") or die ("can't open output file $!\n"); OUTER: for (1..10) { $thread_counter++; $pid=fork(); if ($pid==0) { #child @pids=(); $pid=$$; $parentpid=0; last OUTER; }else{ push @pids,$pid; print "Parent has spawned child $pid\n"; } } if ($parentpid == 0) { #kid my $items=0; while (my $cname= <IN> ) { chomp $cname; $items++; print "$cname checked by $$\n"; print OUT "$cname,checked by $$\n"; sleep 1; } print "Thread $$ processed $items items.\n"; exit(0); } else { #parent print "$thread_counter threads started - waiting on completion.\n"; Reaper(); print "Parent: Goodbye(:-)\n"; exit(0); } sub Reaper { while (my $kid = shift(@pids)) { #warn "$$ to reap $kid\n" ; my $reaped = waitpid($kid,0); unless ($reaped != -1) { warn "waitpid $reaped: $?\n" ; } } }

Thanks in advance - Mark


In reply to Unexpected output from fork (Win32) by maa

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.