# resource limits -- how to use resource limits with Macaulay2

Many operating systems support limiting the amount of resources (for instance, memory or CPU time) that a given instance of a running program (process'') may use. Processes that use more than the allotted resource may have requests denied (as in the case of memory or disk usage), or may be terminated. In some cases it is useful to apply these limits to Macaulay2.

In Unix-like operating systems (including Linux and OS X) these limits are known as ulimits'' or rlimits''. Users typically set these limits using a built-in shell command called ulimit or similar; for help, consult the documentation for your shell (e.g., run man bash). In particular, the units of any limits can vary from system to system. There are two types of limits, hard and soft: soft levels are always less than or equal to the hard limits, and hard levels can only be adjusted down. When a process creates a child process, the child process receives the ulimits of the parent process.

As an example, we give commands for a Linux system using a Bash shell. To view the current limits, run:

ulimit -a

To set soft ulimits on your shell of 123456 kilobytes of memory and 5 seconds of CPU time, run:

ulimit -S -m 123456 -S -t 5

Because ulimits are inherited by child processes, any commands run later in your shell session (say, Macaulay2) will inherit these ulimits. Perhaps the best way to set the limits on a specific process is to run something like

(ulimit -S -m 123456 -S -t 5; M2)

which will run M2 with these limits while leaving the shell's ulimits unchanged.

From within Macaulay2, you can view the ulimits currently available to the Macaulay2 process by using the run command:

 i1 : run("ulimit -a") time(seconds) 700 file(blocks) unlimited data(kbytes) unlimited stack(kbytes) 8192 coredump(blocks) unlimited memory(kbytes) 850000 locked memory(kbytes) 742346 process 23071 nofiles 512 vmemory(kbytes) unlimited locks unlimited rtprio 0 o1 = 0

This starts a new shell and executes the command given, which in this case provides the list of ulimits of the shell. Since ulimits are inherited, this should be the same as the ulimits of Macaulay2 itself.

From within Macaulay2, it is not possible to set ulimits on the current Macaulay2 process because Macaulay2 does not provide access to the setrlimit system call (other than the limitFiles and limitProcesses commands). However, it is possible to set ulimits on child Macaulay2 processes that are started by runExternalM2, by using the PreRunScript option of runExternalM2.