Note that there are some explanatory texts on larger screens.

plurals
  1. POPerl kill(0, $pid) in Windows always returning 1
    text
    copied!<p>I'm trying to make a Perl script that will run a set of other programs in Windows. I need to be able to capture the stdout, stderr, and exit code of the process, and I need to be able to see if a process exceeds it's allotted execution time. Right now, the pertinent part of my code looks like:</p> <pre><code>... $pid = open3($wtr, $stdout, $stderr, $command); if($time &lt; 0){ waitpid($pid, 0); $return = $? &gt;&gt; 8; $death_sig = $? &amp; 127; $core_dump = $? &amp; 128; } else{ # Do timeout stuff, currently not working as planned print "pid: $pid\n"; my $elapsed = 0; #THIS LOOP ONLY TERMINATES WHEN $time &gt; $elapsed ...? while(kill 0, $pid and $time &gt; $elapsed){ Time::HiRes::usleep(1000); # sleep for milliseconds $elapsed += 1; $return = $? &gt;&gt; 8; $death_sig = $? &amp; 127; $core_dump = $? &amp; 128; } if($elapsed &gt;= $time){ $status = "FAIL"; print $log "TIME LIMIT EXCEEDED\n"; } } #these lines are needed to grab the stdout and stderr in arrays so # I may reuse them in multiple logs if(fileno $stdout){ @stdout = &lt;$stdout&gt;; } if(fileno $stderr){ @stderr = &lt;$stderr&gt;; } ... </code></pre> <p>Everything is working correctly if <code>$time = -1</code> (no timeout is needed), but the system thinks that <code>kill 0, $pid</code> is always 1. This makes my loop run for the entirety of the time allowed.</p> <p>Some extra details just for clarity:</p> <ul> <li>This is being run on Windows.</li> <li>I know my process does terminate because I have get all the expected output.</li> <li>Perl version: This is perl, v5.10.1 built for MSWin32-x86-multi-thread (with 2 registered patches, see perl -V for more detail) Copyright 1987-2009, Larry Wall Binary build 1007 [291969] provided by ActiveState <a href="http://www.ActiveState.com" rel="nofollow">http://www.ActiveState.com</a> Built Jan 26 2010 23:15:11</li> <li>I appreciate your help :D</li> </ul> <p><em>For that future person who may have a similar issue</em> I got the code to work, here is the modified code sections:</p> <pre><code> $pid = open3($wtr, $stdout, $stderr, $command); close($wtr); if($time &lt; 0){ waitpid($pid, 0); } else{ print "pid: $pid\n"; my $elapsed = 0; while(waitpid($pid, WNOHANG) &lt;= 0 and $time &gt; $elapsed){ Time::HiRes::usleep(1000); # sleep for milliseconds $elapsed += 1; } if($elapsed &gt;= $time){ $status = "FAIL"; print $log "TIME LIMIT EXCEEDED\n"; } } $return = $? &gt;&gt; 8; $death_sig = $? &amp; 127; $core_dump = $? &amp; 128; if(fileno $stdout){ @stdout = &lt;$stdout&gt;; } if(fileno $stderr){ @stderr = &lt;$stderr&gt;; } close($stdout); close($stderr); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload