The library(rlimit)
library provides an interface to the
POSIX
getrlimit()/setrlimit() API that control the maximum
resource-usage of a process or group of processes. This call is
especially useful for servers such as CGI scripts and inetd-controlled
servers to avoid an uncontrolled script claiming too much resources.
as
Max address space cpu
CPU time in seconds fsize
Maximum filesize data
max data size stack
max stack size core
max core file size rss
max resident set size nproc
max number of processes nofile
max number of open files memlock
max locked-in-memory address
When the process hits a limit POSIX systems normally send the process a signal that terminates it. These signals may be caught using SWI-Prolog's on_signal/3 primitive. The code below illustrates this behaviour. Please note that asynchronous signal handling is dangerous, especially when using threads. 100% fail-safe operation cannot be guaranteed, but this procedure will inform the user properly‘most of the time’.
rlimit_demo :- rlimit(cpu, _, 2), on_signal(xcpu, _, cpu_exceeded), ( repeat, fail ). cpu_exceeded(_Sig) :- format(user_error, 'CPU time exceeded~n', []), halt(1).