The problem you are having is that you are trying to use Win32::Process::KillProcess() on a $pid returned by fork.

Under AS 5.6.1, fork is a simulation of the unix fork, and it is done using native threads, not processes. This can be confirmed by printing out the $pid returned by fork. On my system they always come back as a negative number but process ids are always positive. You can also see the second thread come into existance, and disappear if you watch the Task Manager.

To kill the forked 'process', just use the standard perl kill function.  kill 9, $pid;.

#! perl -slw use strict; use Config; use Win32::Event; use Win32::Process; $|++; #print $Config{sig_name}; my $event = Win32::Event->new(1, 0, 'test'); if (not defined(my $pid = fork())) { warn "can not fork: $!\n"; return; } elsif ($pid) { print "Parent processid: $$; child:$pid"; if($event->wait( 5000 )) { print 'The child signalled and terminated'; } else { print 'Parent: child continuing after 5 secs'; if($event->wait( 5000 )) { print 'The child signalled and terminated'; } else { print 'Parent: child still continuing after 10 secs'; my $exitCode = 0; print "Parent: Attempting to kill processid $pid"; # Win32::Process::KillProcess( $pid, $exitCode ) or warn $^ +E; # print "Parent: KillProcess returned: $exitCode"; kill 9, $pid or die $!; print "Parent: KillProcess returned: $?"; } } print "Parent dying"; } else { for (1..20) { print "Child process: $$"; sleep 1; } print "Child: signalling parent"; $event->set; print "Child dying"; exit 12345; } __END__

Output

C:\test>217881 Parent processid: 273; child:-295 Child process: -295 Child process: -295 Child process: -295 Child process: -295 Child process: -295 Parent: child continuing after 5 secs Child process: -295 Child process: -295 Child process: -295 Child process: -295 Child process: -295 Parent: child still continuing after 10 secs Parent: Attempting to kill processid -295 Parent: KillProcess returned: 0 Parent dying C:\test>

Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.


In reply to Re: command execution & timeout in windows by BrowserUk
in thread command execution & timeout in windows by Anonymous Monk

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.