Learn R Programming

RAppArmor (version 1.0.0)

rlimit_cpu: Limit CPU time

Description

CPU time limit in seconds. When the process reaches the soft limit, it is sent a SIGXCPU signal.

Usage

rlimit_cpu(hardlim, softlim = hardlim, pid = 0,
    verbose = FALSE)

Arguments

hardlim
cpu time in seconds
softlim
cpu time in seconds
pid
id of the target process
verbose
print some C output (TRUE/FALSE)

Details

Note that CPU time is not the same as elapsed time. If a process is waiting/idle, it will not count towards the CPU time limit. See the example for ?rlimit_cpu. Use the 'timeout' parameter in ?eval.secure to limit the total elapsed time for a function call.

Use rlimit_cpu only as a last-resort if-all-else-fails method. When the cpu limit is hit, the kernel kills the process without any warning. It is pretty much a ticking timebomb that cannot be undone. When using eval.secure, best practice is to make sure that the value for rlimit_cpu is higher than timeout.

CPU time limit in seconds. When the process reaches the soft limit, it is sent a SIGXCPU signal. The default action for this signal is to terminate the process. However, the signal can be caught, and the handler can return control to the main program. If the process continues to consume CPU time, it will be sent SIGXCPU once per second until the hard limit is reached, at which time it is sent SIGKILL. (This latter point describes Linux behavior. Implementations vary in how they treat rocesses which continue to consume CPU time after reaching the soft limit. Portable applications that need to catch this signal should perform an orderly termination upon first receipt of SIGXCPU.)

References

Jeroen Ooms (2013). The RAppArmor Package: Enforcing Security Policies in {R} Using Dynamic Sandboxing on Linux. Journal of Statistical Software, 55(7), 1-34. http://www.jstatsoft.org/v55/i07/.

Ubuntu Manpage: getrlimit, setrlimit - get/set resource limits. http://manpages.ubuntu.com/manpages/precise/man2/getrlimit.2.html.

See Also

Other rlimit: rlimit_as, rlimit_core, rlimit_data, rlimit_fsize, rlimit_memlock, rlimit_msgqueue, rlimit_nice, rlimit_nofile, rlimit_nproc, rlimit_rtprio, rlimit_rttime, rlimit_sigpending, rlimit_stack

Examples

Run this code
testfun <- function(){
  Sys.sleep(3);
  repeat{
    svd(matrix(rnorm(1e6,1e3)));
  }
};
#will be killed after 8 seconds (3s idle, 5s CPU):
system.time(eval.secure(testfun(), RLIMIT_CPU=5));

#will be killed after 5 seconds
system.time(eval.secure(testfun(), timeout=5));

Run the code above in your browser using DataLab