Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks,

I am trying to a write a simple subroutine in perl to take a command as argument, execute the command, if the command does not complete with in specified timeout interval, kill the process. If the program got completed within timeout interval, then return its output to the calling routine.

I did it in Unix without any problem, now I am trying to do it in Windows (ActivePerl 5.6.1), but windows does not support alarm functions, so is there a way to achieve it in Windows? The Win32::Process module may not work for me because it does not give the output of the executed program back.

Here is the code I tried doing, it work well but I think I am really messing up with Mutex, so the program says it is timed out but it is not exiting.

Can anyone suggest any correction or a better way to do it...

#!/usr/local/bin/perl # # set alarm signal to die with timeout message use Win32; use Win32::Mutex; use Win32::IPC; use Win32::Process; # timeout by creating a mutex and spawning a child process # let the child lock the mutex first and attempt lock on # the mutex and timeout after $timeout period. The child # should release the mutex upon completion of the command # so if the parent could not get the mutex during timeout # period, then it means the child is still running. Kill # the child and return the status. # if you are able to get the lock on mutex, then it means # the command got completed. The output is available in # the output_ref. sub timeout { my ($cmd, $timeout, $output_ref) = @_; # create a new mutex $mutex = Win32::Mutex->new; if (not defined($pid = fork())) { warn "can not fork: $!\n"; return; } elsif ($pid) { # parent process # wait for some time and request lock on mutex Win32::Sleep(1000); if ($rc = $mutex->wait($timeout * 1000) == 0) { # timed out # kill the child process and return Win32::Process::KillProcess($pid, 0); $mutex->wait(0); $mutex->release; return 0; } elsif ($rc > 0) { # completed Win32::Process::KillProcess($pid, 0); $mutex->release; return 1; } else { # negative value indicates the mutex is abandoned Win32::Process::KillProcess($pid, 0); $mutex->wait(0); $mutex->release; return -1; } } else { # Child process # get a lock on mutex and run the command $mutex->wait(0); open(CMD, "$cmd|") or die "Error running $cmd: $!\n"; @$output_ref = <CMD>; close(CMD); #release the lock $mutex->release; } } if (&timeout("ping -t tmrprod100", 1, \@output)) { print @output; } else { print "timed out...\n"; }

Edit by tye to add READMORE tag

YAE by jeffa to remove colors

Replies are listed 'Best First'.
Re: command execution & timeout in windows
by BrowserUk (Patriarch) on Dec 05, 2002 at 22:31 UTC

    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.

      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.

Re: command execution & timeout in windows
by BrowserUk (Patriarch) on Dec 06, 2002 at 19:05 UTC

    After going all around the houses playing with Win32::Process, Win32::Pipe and numerous other things, I finally arrived back at what I'm sure many others already knew. I wonder why they didn't speak up??

    IPC::Open3 can do what you want.

    #! perl -slw use strict; use IPC::Open3; $|++; sub timeout { my ($timeout, $output_ref, @cmd ) = @_; print "@cmd"; my ($wtr, $rdr); my $pid = open3( $wtr, $rdr, $rdr, @cmd ); print $pid; my $start = time(); while( <$rdr> ) { push @$output_ref, $_; if (($start+$timeout) < time() ) { kill( 9, $pid) or warn $!; return 0; # Timeout. @$output_ref may contain some output. } } return -1; # Successful completion. } my @output; if ( timeout(10, \@output, 'ping', '-t', 'bbc.co.uk') ) { print @output; } else { print "timed out...\n@output"; }

    As coded above, it will capture both STDOUT and STDERR from the child process and return it all to the caller. If you want to ignore the STDERR output use IPC::Open2, if you want the two seperated, it's a fairly trivial change. See the docs for details.


    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.

Re: command execution & timeout in windows
by Theseus (Pilgrim) on Dec 07, 2002 at 14:57 UTC
    As an unrelated question, what trick did you use to do that color coding? I'd hate to think you added all those font tags manually, but if it's a perl script you whipped up or something, you should post it in the code or snippets section, I'd love to see it! I also encourage you to register and get a username first though, you'll find it's a lot easier to get help if you assist people in getting a message to you.
      Enter perltidy.

      Makeshifts last the longest.