I'm creating a shell and have a problem when creating my own ulimit
function : I want to limit the time of a process, and I use setrlimit
for it. But it seems that when I call execvp
then, the time limit is kind of erased.
In this example code, when I let the while(1)
, the child process receives a SIGXCPU
and is killed after 3 seconds. But when I do execvp(...)
instead, it's never killed.
#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);
}
}
If I'm right and the time limit I set with setrlimit
is erased, how to do it then ?
Thanks for help.