For the purpose of a challenge over on PPCG.SE I want to run programs (submissions) with a strict time limit. In order for those submissions not to waste their precious time with I/O, I want to run the following scheme:
- Submissions do their computation in an infinite loop (as far as they get) and register a signal handler.
- After 10 minutes
SIGINT
is sent, and they have one second to produce whatever output is necessary. SIGKILL
is sent to terminate the submission for good.
However, submissions should have the option not to register a signal handler and just produce output in their own time.
I will run the submissions on Windows 8 and wanted to use Ruby to orchestrate this. However, I'm running into some trouble. I thought I'd be able to do this:
solver = IO.popen(solver_command, 'r+')
sleep(10*60)
Process.kill('INT', solver.pid)
sleep(1)
Process.kill('KILL', solver.pid)
puts solver.read
solver.close
However, the moment I send SIGINT
, not only the submission process but the controlling Ruby process immediately aborts, too! Even worse, if I do this in PowerShell, PowerShell also shuts down.
A simple Ruby "submission" to reproduce this:
puts "My result"
$stdout.flush
next while true
What am I doing wrong here? I feel that it's very likely I'm misunderstanding something about processes and signals in general and not Ruby in particular, but I'm definitely missing something.