简体   繁体   English

如何在Ruby on Rails Web服务器上进行编译和编码,从而限制CPU和内存的使用?

[英]How do I compile and code from a Ruby on Rails web server, limiting CPU and memory usage?

I have a cpp code being submitted by user. 我有一个由用户提交的CPP代码。 I also have input and output files in my db. 我的数据库中也有输入和输出文件。 How do compile and run this cpp code on the input and output file I have. 如何在我拥有的输入和输出文件上编译和运行此cpp代码。

Additionally how can I restrict running time and memory consumption of the above running of code. 另外,如何限制以上代码运行的运行时间和内存消耗。 Also how do I measure how much memory is being and how much time it took to execute the above code. 另外,我该如何测量正在执行的代码的内存量和时间。

Edit I got this below hack working. 编辑我在黑客工作下面得到了这个。 Now the only problem how do I limit the memory usage of running program. 现在唯一的问题是如何限制正在运行的程序的内存使用量。 I will appreciate if it would be OS independent. 如果它将独立于操作系统,我将不胜感激。 But if its not possible solution for both Windows and Linux are welcome. 但是,如果无法为Windows和Linux提供解决方案,则欢迎使用。

require 'timeout' 需要“超时”

    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)

You can create some run script with your ruby code, say run.sh if you're using *nix-like operating system. 您可以使用红宝石代码创建一些运行脚本,如果使用的是类似* nix的操作系统,请说run.sh。 The context of this script will be smth like this: 该脚本的上下文如下所示:

#!/bin/bash
/path/to/timeout /path/to/your/compiled/cpp/program < /your/stdin > /your/stdout

Where the timeout program is this perl script https://github.com/pshved/timeout It will kill your program if it breaches time or memory limit. 超时程序就是这个perl脚本的地方https://github.com/pshved/timeout如果违反时间或内存限制,它将杀死您的程序。 From ruby you will just execute run.sh script ( run.sh ) or smth like that. 从ruby中,您将只执行run.sh脚本( run.sh )或类似的东西。 For Windows you can use same aproach 对于Windows,您可以使用相同的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM