in reply to command execution & timeout in windows

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.

Replies are listed 'Best First'.
Re: Re: command execution & timeout in windows
by Anonymous Monk on Dec 05, 2002 at 23:03 UTC

    Hi BrowserUk,

    Thank you for your reply. I've changed the Win32::Process::KillProcess statements with kill(9, $pid) but still getting the same error.

    C:\venkat\Target\Work 2002\Perl>perl timeout.pl timed out... ^C C:\venkat\Target\Work 2002\Perl>

      The problem appears to stem from the presence of the child process (a real process this time). When the kill(9,$pid) executes, the pseudo-process that executes the open(CMD, ..) is killed. However the process created by the open to run the cmd is not killed, and despite the child thread having 'gone away' (according to the task manager), the child process it spawned persists, and the timeout branch of the parent code is taken. I can only assume that the child thread is marked for deletion and no longer scheduled, but that it is still there in memory waiting for the process to terminate.

      Worse than this, even killing the child process manually , still leaves perl running the original script. and it requires ^c to terminate it.

      These are my findings so far.... if anyone reading has better knowledge, or can offer the OP a solution, please jump in and set us both straight.


      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.