[英]How do I compile and code from a Ruby on Rails web server, limiting CPU and memory usage?
我有一个由用户提交的CPP代码。 我的数据库中也有输入和输出文件。 如何在我拥有的输入和输出文件上编译和运行此cpp代码。
另外,如何限制以上代码运行的运行时间和内存消耗。 另外,我该如何测量正在执行的代码的内存量和时间。
编辑我在黑客工作下面得到了这个。 现在唯一的问题是如何限制正在运行的程序的内存使用量。 如果它将独立于操作系统,我将不胜感激。 但是,如果无法为Windows和Linux提供解决方案,则欢迎使用。
需要“超时”
runDir = 'run\\'
def code_file( sid )
return "#{sid}.cpp"
end
def executable_file( sid )
return "#{sid}.exe"
end
def input_file( sid )
return "#{sid}.in"
end
def output_file( sid )
return "#{sid}.out"
end
def get_complie_command_line( sid , runDir)
return "g++ -w -O2 #{code_file(sid)} -o #{runDir}#{executable_file(sid)}"
end
def get_run_command_line( sid , runDir )
return "#{runDir}#{executable_file(sid)} < #{sid}.in"
end
def run_submission( sid , runDir )
begin
timeout(5) {
run_cmd_line = get_run_command_line( 1 , runDir)
puts run_cmd_line
runOutput = %x[#{run_cmd_line}]
puts runOutput
}
puts "Timeout didn't occur"
rescue Timeout::Error
puts "Timed out!"
end
end
def compile( sid , runDir )
#make the directory
%x[mkdir #{runDir}]
#get compile command line and produce the exe
cmd_line = get_complie_command_line( 1 , runDir)
puts cmd_line
compile_error = %x[#{cmd_line}].to_s.strip
#run the code
if compile_error.length != 0
puts "Compile Errors"
else
run_submission( 1 , runDir )
end
end
compile( 1 , runDir)
您可以使用红宝石代码创建一些运行脚本,如果使用的是类似* nix的操作系统,请说run.sh。 该脚本的上下文如下所示:
#!/bin/bash
/path/to/timeout /path/to/your/compiled/cpp/program < /your/stdin > /your/stdout
超时程序就是这个perl脚本的地方https://github.com/pshved/timeout如果违反时间或内存限制,它将杀死您的程序。 从ruby中,您将只执行run.sh脚本( run.sh
)或类似的东西。 对于Windows,您可以使用相同的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.