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.
asMax address space cpuCPU time in seconds fsizeMaximum filesize datamax data size stackmax stack size coremax core file size rssmax resident set size nprocmax number of processes nofilemax number of open files memlockmax 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).