From man page of setrlimit
, when RLIMIT_CPU
limit is set, process should receive SIGXCPU
every second once the soft limit is reached, and SIGKILL
when the hard limit is reached.
I have the following code where the child process is created using clone()
call. Then RLIMIT_CPU
is set followed by long loop, which should be terminated via SIGKILL
. This seems to work with SIGCHLD
flag set. However, when I use SIGCHLD | CLONE_NEWPID
flags, the loop doesn't terminate.
EDIT:
What happens on my machine: program writes pid 1 interrupted
two times, and then execution freezes (loop is running and isn't killed) until loop is finished. This lasts about 10 seconds. When I remove CLONE_NEWPID
program terminates after 3 seconds.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sched.h>
int inside(void *arg)
{
struct rlimit r = {1, 3};
setrlimit(RLIMIT_CPU, &r);
for (long long i = 0; i < 3e9; i++);
return 0;
}
void handler(int signal)
{
printf("pid %d interrupted\n", getpid());
fflush(stdout);
}
void setup_signals(void)
{
struct sigaction s;
bzero(&s, sizeof s);
s.sa_handler = handler;
sigaction(SIGXCPU, &s, NULL);
}
int main(void)
{
const int stack_size = 1 << 16;
char stack[stack_size];
setup_signals();
int pid = clone(inside, stack + stack_size, SIGCHLD | CLONE_NEWPID, NULL);
if (pid < 0) return 1;
wait(NULL);
return 0;
}
Why the child process is not killed (it gets killed if you remove CLONE_NEWPID
)?