Ruby rlimit_cpu不在生成调用上工作
我正在尝试创建一个函数,该函数将执行一个进程,并在2秒后将其终止:Ruby rlimit_cpu不在生成调用上工作,ruby,linux-kernel,ruby-on-rails-4,Ruby,Linux Kernel,Ruby On Rails 4,我正在尝试创建一个函数,该函数将执行一个进程,并在2秒后将其终止: def execute(command, input_file, output_file) pid = Kernel.spawn(command,{ STDIN => input_file, STDOUT => output_file, :rlimit_cpu => [2,2], #2 secs, :rli
def execute(command, input_file, output_file)
pid = Kernel.spawn(command,{
STDIN => input_file,
STDOUT => output_file,
:rlimit_cpu => [2,2], #2 secs,
:rlimit_nproc => 0,
:rlimit_as => 16 * 1024 * 1024
}
);
Process.wait(pid)
puts "exit status = " + $?.exitstatus.to_s
return File.read(output_file)
end
我用短时间运行的进程和长时间运行的进程(通过一个“sleep”调用)测试了这个函数。命令始终完成。我需要spawn调用使用rlimit_cpu在2秒后杀死“命令”。我怎么能做到
编辑:看起来rlimit_cpu不像我想的那样工作。根据:
CPU限制是对CPU秒数的限制,而不是对已用时间的限制
此外:
当您执行fib调用时,您会敲打CPU,使经过的时间和CPU时间接近(大部分处理时间都是使用CPU)。打印时并非如此,因为大部分时间都花在I/O上
我将使用另一种方法,因为我需要终止进程,不管进程是CPU绑定的还是IO绑定的要在两秒钟后终止进程,请运行计时器两秒钟,然后尝试调用子进程上的
进程。kill