2

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.

Community
  • 1
  • 1
Martin Ender
  • 43,427
  • 11
  • 90
  • 130
  • 1
    To begin with, you do understand that signals aren't native to Windows, right? Ruby is emulating them somehow. It sounds like SIGINT is being converted to a control-C or control-break, which affects the entire process group. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682083%28v=vs.85%29.aspx – Harry Johnston Sep 08 '14 at 21:53
  • @HarryJohnston Thank you, that did solve the problem with the signals. By using `Process.spawn` I can set `:new_pgroup => true` which creates a new process group. However, now I don't receive anything from the process's STDOUT any more, following [this example](http://stackoverflow.com/a/12202439/1633117). – Martin Ender Sep 08 '14 at 22:18

0 Answers0