2016-01-15 57 views
4

基本上我想運行我編譯的C++代碼,並像在線評委一樣限制執行時間(例如到第二秒)和內存(到100k)。可以通過在命令中添加選項嗎?這必須在不修改源代碼的情況下完成。有沒有辦法限制運行bash命令的時間和內存資源?

+0

閱讀「cgroups」,這可能是限制流程資源的另一種方式。請參閱[ArchWiki](https://wiki.archlinux.org/index.php/cgroups)和[RedHat文檔](https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Resource_Management_Guide /Starting_a_Process.html)。特別是['cgexec'](http://manpages.ubuntu.com/manpages/precise/man1/cgexec.1.html)命令可能會有所幫助。 –

回答

6

嘗試ulimit命令,它可以設置CPU時間和內存限制。

試試這個例子

bash -c 'ulimit -St 1 ; while true; do true; done;' 

你會得到的結果將是

CPU time limit exceeded (core dumped) 
+0

嘿,謝謝,我認爲這解決了它。您可以使用最大千字節數(用於虛擬內存)添加-v選項。 – kanslulz

2

要限制時間,你可以使用 「暫停」 命令

timeout 15s command 

檢查此瞭解更多詳情:link

相關問題