Using prlimit(1) – examples

Example 1—querying limits:

$ prlimit

Output for the preceding command is as follows:

$ ps
PID TTY TIME CMD
2917 pts/7 00:00:00 bash
3339 pts/7 00:00:00 ps
$ prlimit --pid=2917
RESOURCE DESCRIPTION SOFT HARD UNITS
AS address space limit unlimited unlimited bytes
CORE max core file size unlimited unlimited bytes
CPU CPU time unlimited unlimited seconds
[...]
$

Here, we have abbreviated the output for better readability.

Example 2—set the resource limits for max file size and max stack size for the (preceding) shell process:

$ prlimit --pid=2917 --fsize=2048000 --stack=12582912 
$ prlimit --pid=2917 | egrep -i "fsize|stack"
FSIZE max file size 2048000 2048000 bytes
STACK max stack size 12582912 12582912 bytes
$

Example 3—a program, rlimit_primes, that generates prime numbers; have it generate a large number of primes but give it only two seconds of CPU time to do so.

Note that the rlimit_primes program, along with its source code, is described in detail in the API interfaces section.

For now, we just run it within the scope of the built-in prlimit program, ensuring that the rlimit_primes process only gets the CPU bandwidth (in seconds) that we pass via the prlimit --cpu= option switch. In the example, we ensure the following:

  • We give our prime number generator process two seconds (via prlimit)
  • We pass -2 as the second parameter; this will cause the rlimit_primes program to skip setting the CPU resource limit itself
  • We ask it to generate primes up to the number 8,000,000:
$ ./rlimit_primes 
Usage: ./rlimit_primes limit-to-generate-primes-upto CPU-time-limit
arg1 : max is 10000000
arg2 : CPU-time-limit:
-2 = don't set
-1 = unlimited
0 = 1s
$ prlimit --cpu=2 ./rlimit_primes 8000000 -2
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53,
59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131,

[...]

18353, 18367, 18371, 18379, 18397, 18401, 18413, 18427, 18433, 18439,
18443, 18451, 18457, 18461, 18481, 18493, 18503, 18517, 18521, 18523,
18539, 18541, 18553, 18583, 18587, 18593,
Killed
$

Note how, once it's out of its newly constrained CPU time resource (two seconds, in the preceding example), it gets killed by the kernel! (Technically, by the SIGKILL signal; a lot more on signals follows in Chapter 11Signaling - Part I, and Chapter 12, Signaling - Part II). Note how the word Killed appears, indicating that the OS has killed the process.

Refer to the man page on prlimit(1) for further details.

A practical case: When running fairly heavy software such as Eclipse and Dropbox, I have found it necessary to bump up the resource limits for them (as advised); otherwise, they abort as they run out of resources.

Advanced: From the Linux kernel version 2.6.24 onward, one can look up the resource limits for a given process PID via the powerful proc filesystem: /proc/<PID>/limits.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset