[英]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.