note
bojinlund
<p> [BrowserUk], thank you!</p>
<blockquote><i>
What I would say; and have said many times; is do not attempt to use fork & the windows signals emulation to try to port *nix idioms to windows; because you will be sadly let down.
</i></blockquote>
<p>Based on this, I will propose an addition to the documentation of function fork (and perhaps also kill) in the Perl Language reference.</p>
<blockquote><i>
<p>
You reinvented ... a thread-bomb.
</p>
<p>
And you are using a random delay -- sleep 0 equates to "relinquish the rest of the current timeslice" which is an unknowable quantum of time, that could range from 0 microseconds
</p>
</i></blockquote>
<p>
My “thread-bomb” resulted in a patch.
The patch [http://perl5.git.perl.org/perl.git/commitdiff/82e24582] adds to t/op/fork.t:
<code>
+system $^X, "-e", "if (\$pid=fork){sleep 1;kill(9, \$pid)} else {sleep 5}";
</code>
<p>
Is this also a thread-bomb, or is it OK with non zero sleep?
</p>
<p>
The patch also changes win32/win32.c:
<code>
--- a/win32/win32.c
+++ b/win32/win32.c
@@ -1282,6 +1282,13 @@ win32_kill(int pid, int sig)
case 9:
/* kill -9 style un-graceful exit */
if (TerminateThread(hProcess, sig)) {
+ /* Allow the scheduler to finish cleaning up the other thread.
+ * Otherwise, if we ExitProcess() before another context switch
+ * happens we will end up with a process exit code of "sig" instead
+ * of our own exit status.
+ * See also: https://rt.cpan.org/Ticket/Display.html?id=66016#txn-908976
+ */
+ Sleep(0);
remove_dead_pseudo_process(child);
return 0;
}
</code>
<p>
Is it OK with “Sleep(0)”, or must it be a non zero parameter value?
Can the non zero time value cause that we still sometimes get 9 as an erroneous exit value?
What is the smallest non-zero value that can be used?
</p>
1022364
1022591