[英]How limit time of execution and memory limit while execution of program
我正在编写一个python脚本来执行程序(C,C ++,Python,Java,C#,...)。
我想确保执行是在固定的时间限制内执行并使用有限的内存量。
我怎么能用Python或bash做到这一点?
目前我使用的Python是这样的:
os.system("time ./"+filename+"< input.txt >out.txt")
好吧,我找到了如何做到这一点,在python中运行以下内容,你会看到它工作,它应该失败,malloc为第二个命令:
import os
commandStr = 'timeout 4 bash -c "ulimit -v 100000000000; python -c \"[5]*10000\""'
print 'Running command with large ulimit'
os.system(commandStr)
commandStr = 'timeout 4 bash -c "ulimit -v 10; python -c \"[5]*10000\""'
print 'Running command with low ulimit'
os.system(commandStr)
传递给ulimit
的参数是以kB为单位
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.