Hi,

I'm clearly doing something stupid but I can't see what it is. I've broken the test code down to a small case that exhibits the odd behaviour. Can anyone see/explain what I'm doing wrong here? (Apart from using fork() on winnt at all, that is.)

#!C:/Activeperl/bin/perl.exe use strict; use warnings; open (IN, "fork2.ini") or die("Can't open input file $!\n"); my $pid=0; my $x=0; while (my $computer=<IN>) { chomp($computer); $x++; $pid = fork(); if ($pid) { print STDERR "Parent $$ got PID $pid from fork($$ $computer)\n +"; sleep(1); }else { print STDERR "\t$$ $computer\n"; exit(0); } if($x > 4) { do { $pid=wait() } while ($pid != -1); $x=0; } } do { $pid=wait() } while ($pid != -1); print "wait() ed until $pid was returned.\n"; close(IN); __DATA__ Computer1 Computer2 Computer3

The file "fork2.ini" contains the asme as the __DATA__ part... another weirdness is that if I don't use <IN> and use <DATA> instead it prints out the source code!

The output I get from running this is:

C:\logchecks>fork3
Parent 433 got PID -605 from fork(433 test1)
        -605 test1
Parent 433 got PID -461 from fork(433 test2)
        -461 test2
Parent 433 got PID -533 from fork(433 test3)
        -533 test3
Parent 433 got PID -105 from fork(433 test2)
        -105 test2
Parent 433 got PID -521 from fork(433 test3)
        -521 test3
Parent 433 got PID -670 from fork(433 test3)
        -670 test3
wait() ed until -1 was returned.
Done

As you can see, it's printing values from the files out more than once. (Which I don't think it should.)

Seeking enlightenment... Mark.

UPDATE: if I change a couple of things it works...

my (@Computers=(<IN>); #Do this #while (my $computer=<IN>) { #change this while (my $computer=pop @Computers) { #to this

I still don't understand what was going wrong in the original, however.


In reply to Win32 fork() "problem" 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.