我創建一個shell並在創建自己的時候遇到問題ulimit
函數:我想限制一個進程的時間,並且我使用setrlimit
。但是,如果我撥打execvp
,那麼時間限制就會被刪除。設置子進程的時間限制
在此示例代碼中,當我讓while(1)
時,子進程收到SIGXCPU
並在3秒後終止。但是,當我做execvp(...)
,而不是被殺死。
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
int main(void) {
struct rlimit t = {3, 8};
uint32_t child_pid = fork();
// father
if (child_pid != 0) {
waitpid(child_pid, NULL, 0);
// child
} else {
setrlimit(RLIMIT_CPU, &t);
char* s[3];
s[0] = "sleep";
s[1] = "1000";
s[2] = NULL;
/* while(1); */
execvp(*s, s);
}
}
如果我是正確的,我設定setrlimit
的期限被刪除,怎麼辦呢呢?
感謝您的幫助。
'sleep'幾乎不使用CPU時間。它永遠不會達到3秒的極限。 – Barmar
創建自己的執行無限循環的程序,並執行而不是「睡眠」。 – Barmar
好了解。我需要一種方法來衡量實時,而不是CPU時間,我認爲'setitimer'會幫助我。謝謝 –