getrlimit(2)
โจ getrlimit โจ
okay so bestie, welcome to the getrlimit(2) page from the System Calls Manual ๐
this is where the kernel tells you how much of a thing you're allowed to have, and your girl is gonna walk you through every single byte of it, no cap.
๐ NAME
getrlimit, setrlimit, prlimit โ get/set resource limits
๐ LIBRARY
Standard C library (libc, -lc) ๐ซถ
๐ SYNOPSIS
#include <sys/resource.h>
int getrlimit(int resource, struct rlimit *rlim);
int setrlimit(int resource, const struct rlimit *rlim);
int prlimit(pid_t pid, int resource,
const struct rlimit *_Nullable new_limit,
struct rlimit *_Nullable old_limit);
struct rlimit {
rlim_t rlim_cur; /* Soft limit */
rlim_t rlim_max; /* Hard limit (ceiling for rlim_cur) */
};
typedef /* ... */ rlim_t; /* Unsigned integer type */
๐ธ Feature Test Macro Requirements for glibc (peep feature_test_macros(7)):
prlimit():_GNU_SOURCE
๐ DESCRIPTION
So getrlimit() and setrlimit() are the system calls that get and set resource limits, bestie โ reading them and writing them, iconic behavior. Every single resource has two limits attached to it, a soft one and a hard one, and they live inside the rlimit structure. ๐
The soft limit is literally the value the kernel actually enforces for that resource. The hard limit is the ceiling for the soft limit, like the soft limit's overprotective older sibling. An unprivileged process can only set its soft limit to something in the range from 0 up to the hard limit, and it can lower its hard limit (irreversibly, so think before you do it ๐). A privileged process โ under Linux, that's one holding the CAP_SYS_RESOURCE capability in the initial user namespace โ can make arbitrary changes to either limit value. main character energy only, apparently.
The value RLIM_INFINITY means no limit on a resource at all, and that applies both in the structure handed back by getrlimit() and in the structure you pass to setrlimit(). โจ
The resource argument has to be one of these:
๐ RLIMIT_AS
This is the max size of the process's virtual memory (address space). The limit is in bytes, and it gets rounded down to the system page size. It affects calls to brk(2), mmap(2), and mremap(2), which all fail with the error ENOMEM once you blow past this limit. On top of that, automatic stack expansion just fails โ and it generates a SIGSEGV that kills the process if no alternate stack has been made available via sigaltstack(2). ๐ญ And heads up: since the value is a long, on machines with a 32-bit long either this limit is at most 2 GiB, or this resource is unlimited.
๐ฉธ RLIMIT_CORE
This is the maximum size of a core file (see core(5)) in bytes that the process may dump. When it's 0, no core dump files get created at all. When it's nonzero, larger dumps get truncated down to this size. ๐
โฐ RLIMIT_CPU
This is a limit, in seconds, on how much CPU time the process can consume. When the process hits the soft limit, it gets sent a SIGXCPU signal. The default action for that signal is to terminate the process. However, the signal can be caught, and the handler can return control to the main program โ sneaky. If the process keeps consuming CPU time after that, it gets sent SIGXCPU once per second until the hard limit is reached, and at that point it gets sent SIGKILL. ๐
That last part is describing Linux behavior specifically, bestie. Implementations vary in how they treat processes that keep consuming CPU time after reaching the soft limit. Portable applications that need to catch this signal should do an orderly termination the first time they receive SIGXCPU โ no gambling.
๐งฎ RLIMIT_DATA
This is the maximum size of the process's data segment (that's initialized data, uninitialized data, and the heap). The limit is specified in bytes, and gets rounded down to the system page size. It affects calls to brk(2), sbrk(2), and โ since Linux 4.7 โ mmap(2), which fail with the error ENOMEM when they bump into the soft limit of this resource. โ
๐ RLIMIT_FSIZE
This is the maximum size in bytes of files the process may create. Attempts to extend a file past this limit result in delivery of a SIGXFSZ signal. By default that signal terminates the process, but a process can catch it instead, in which case the relevant system call (like write(2), truncate(2)) fails with the error EFBIG. ๐
๐ RLIMIT_LOCKS (Linux 2.4.0 to Linux 2.4.24)
This is a limit on the combined number of flock(2) locks and fcntl(2) leases that this process may establish. R.I.P. this one, it's a period piece. ๐
๐๏ธ RLIMIT_MEMLOCK
This is the maximum number of bytes of memory that may be locked into RAM. It's in effect rounded down to the nearest multiple of the system page size. This limit affects mlock(2), mlockall(2), and the mmap(2) MAP_LOCKED operation. Since Linux 2.6.9, it also affects the shmctl(2) SHM_LOCK operation, where it sets a maximum on the total bytes in shared memory segments (see shmget(2)) that may be locked by the real user ID of the calling process. And get this โ the shmctl(2) SHM_LOCK locks are accounted for separately from the per-process memory locks established by mlock(2), mlockall(2), and mmap(2) MAP_LOCKED. A process can lock bytes up to this limit in each of those two categories. two separate budgets, we love to see it โจ
Before Linux 2.6.9, this limit controlled the amount of memory that could be locked by a privileged process. Since Linux 2.6.9, no limits are placed on how much memory a privileged process may lock, and this limit instead governs the amount of memory that an unprivileged process may lock.
๐ฌ RLIMIT_MSGQUEUE (since Linux 2.6.8)
This is a limit on the number of bytes that can be allocated for POSIX message queues for the real user ID of the calling process. This limit is enforced for mq_open(3). Every message queue that the user creates counts against this limit โ until it's removed โ according to this formula:
Since Linux 3.5:
bytes = attr.mq_maxmsg * sizeof(struct msg_msg) +
MIN(attr.mq_maxmsg, MQ_PRIO_MAX) *
sizeof(struct posix_msg_tree_node)+
/* For overhead */
attr.mq_maxmsg * attr.mq_msgsize;
/* For message data */
Linux 3.4 and earlier:
bytes = attr.mq_maxmsg * sizeof(struct msg_msg *) +
/* For overhead */
attr.mq_maxmsg * attr.mq_msgsize;
/* For message data */
where attr is the mq_attr structure specified as the fourth argument to mq_open(3), and the msg_msg and posix_msg_tree_node structures are kernel-internal structures. ๐ง
The "overhead" addend in the formula accounts for overhead bytes required by the implementation, and it also makes sure the user can't create an unlimited number of zero-length messages (those messages still each consume some system memory for bookkeeping overhead, so, yeah). ๐
๐ RLIMIT_NICE (since Linux 2.6.12, but see BUGS below)
This specifies a ceiling to which the process's nice value can be raised using setpriority(2) or nice(2). The actual ceiling for the nice value is calculated as 20 - rlim_cur. The useful range for this limit is thus from 1 (which corresponds to a nice value of 19) to 40 (which corresponds to a nice value of -20). This unusual choice of range was necessary because negative numbers cannot be specified as resource limit values, since they typically have special meanings โ for example, RLIM_INFINITY typically is the same as -1. For more detail on the nice value, see sched(7). ๐ค
๐ข RLIMIT_NOFILE
This specifies a value one greater than the maximum file descriptor number that can be opened by this process. Attempts (open(2), pipe(2), dup(2), etc.) to exceed this limit yield the error EMFILE. (Historically, this limit was named RLIMIT_OFILE on BSD โ a little glow-up moment for it. โจ)
Since Linux 4.5, this limit also defines the maximum number of file descriptors that an unprivileged process (one without the CAP_SYS_RESOURCE capability) may have "in flight" to other processes, by being passed across UNIX domain sockets. This limit applies to the sendmsg(2) system call. For further details, see unix(7). ๐ธ
๐งต RLIMIT_NPROC
This is a limit on the number of extant processes (or, more precisely on Linux, threads) for the real user ID of the calling process. So long as the current number of processes belonging to this process's real user ID is greater than or equal to this limit, fork(2) fails with the error EAGAIN. ๐ซ
The RLIMIT_NPROC limit is not enforced for processes that have either the CAP_SYS_ADMIN or the CAP_SYS_RESOURCE capability, or that run with real user ID 0. they're built different, iykyk ๐
๐ RLIMIT_RSS
This is a limit (in bytes) on the process's resident set โ that's the number of virtual pages resident in RAM. This limit has effect only in Linux 2.4.x, x < 30, and there it affects only calls to madvise(2) specifying MADV_WILLNEED. antique behavior, ngl ๐ซถ
๐ง RLIMIT_RTPRIO (since Linux 2.6.12, but see BUGS)
This specifies a ceiling on the real-time priority that may be set for this process using sched_setscheduler(2) and sched_setparam(2). ๐ฅ
For further details on real-time scheduling policies, see sched(7)
โณ RLIMIT_RTTIME (since Linux 2.6.25)
This is a limit (in microseconds) on the amount of CPU time that a process scheduled under a real-time scheduling policy may consume without making a blocking system call. For the purpose of this limit, each time a process makes a blocking system call, the count of its consumed CPU time is reset to zero. The CPU time count is not reset if the process continues trying to use the CPU but is preempted, its time slice expires, or it calls sched_yield(2). sneaky detail, remember it โ
Upon reaching the soft limit, the process is sent a SIGXCPU signal. If the process catches or ignores this signal and keeps consuming CPU time, then SIGXCPU will be generated once each second until the hard limit is reached, at which point the process is sent a SIGKILL signal. ๐
The intended use of this limit is to stop a runaway real-time process from locking up the system. public service, honestly ๐
For further details on real-time scheduling policies, see sched(7)
and bestie, we are literally not even done yet โ there's way more where this came from ๐
RLIMIT_SIGPENDING (since Linux 2.6.8)
okay so this one caps how many signals can be queued up for the real user ID of the process doing the calling ๐ and bestie, both standard signals and realtime signals get counted when the kernel checks this limit.
BUT. here's the gag. the limit is only actually enforced for sigqueue(3). it's always possible to use kill(2) to queue one instance of any of the signals that aren't already queued to the process. the vibes are immaculate and the loophole is real ๐ณ๏ธโจ
RLIMIT_STACK
the max size of the process stack, in bytes ๐ซถ hit this limit and a SIGSEGV signal is generated ๐ญ and to handle that signal, a process must employ an alternate signal stack (sigaltstack(2)). non-negotiable.
since Linux 2.6.23, this limit also determines the amount of space used for the process's command-line arguments and environment variables; for the details, go read execve(2) ๐
prlimit()
The Linux-specific prlimit() system call is the glow-up combo of setrlimit() and getrlimit(). it can do both โ set and get the resource limits of an arbitrary process. main character energy, honestly ๐
the resource argument means exactly the same thing as it does for setrlimit() and getrlimit().
if new_limit is not NULL, then the rlimit structure it points to is used to set new values for the soft and hard limits for resource. and if old_limit is not NULL, then a successful call to prlimit() drops the previous soft and hard limits for resource into the rlimit structure pointed to by old_limit ๐
the pid argument says which process ID the call operates on. if pid is 0, the call applies to the calling process itself. to set or get the resources of a process that isn't itself, the caller must have the CAP_SYS_RESOURCE capability in the user namespace of the process whose resource limits are being changed, or the real, effective, and saved set user IDs of the target process must match the real user ID of the caller and the real, effective, and saved set group IDs of the target process must match the real group ID of the caller. iykyk ๐
โจ RETURN VALUE โจ
on success, these system calls return 0 ๐ on error, -1 is returned, and errno is set to indicate the error ๐
๐จ ERRORS ๐จ
EFAULTโ a pointer argument points to a location outside the accessible address space. rude of it.EINVALโ the value specified in resource is not valid; or, forsetrlimit()orprlimit():rlim->rlim_curwas greater thanrlim->rlim_max.EPERMโ an unprivileged process tried to raise the hard limit; theCAP_SYS_RESOURCEcapability is required to do this. sorry bestie, you need the keys to the kingdom.EPERMโ the caller tried to increase the hardRLIMIT_NOFILElimit above the maximum defined by/proc/sys/fs/nr_open(seeproc(5))EPERMโ (prlimit()) the calling process did not have permission to set limits for the process specified by pid.ESRCHโ could not find a process with the ID specified in pid. she's not here ๐ป
๐ซถ ATTRIBUTES ๐ซถ
for an explanation of the terms used in this section, see attributes(7).
getrlimit(),setrlimit(),prlimit()โ Thread safety:MT-Safeโจ (yes all three of them, matching set, slay)
๐ STANDARDS ๐
getrlimit()setrlimit()โ POSIX.1-2024.prlimit()โ Linux.
RLIMIT_MEMLOCK and RLIMIT_NPROC derive from BSD and are not specified in POSIX.1; they are present on the BSDs and Linux, but on few other implementations. RLIMIT_RSS derives from BSD and is not specified in POSIX.1; it is nevertheless present on most implementations. RLIMIT_MSGQUEUE, RLIMIT_NICE, RLIMIT_RTPRIO, RLIMIT_RTTIME, and RLIMIT_SIGPENDING are Linux-specific ๐ธ
๐ฐ๏ธ HISTORY ๐ฐ๏ธ
getrlimit(),setrlimit()โ 4.3BSD, SVr4, SUSv1, POSIX.1-2001 XSI, POSIX.1-2024.prlimit()โ Linux 2.6.36, glibc 2.13.
๐ญ NOTES ๐ญ
a child process created via fork(2) inherits its parent's resource limits. and resource limits are preserved across execve(2) ๐งฌ
resource limits are per-process attributes that are shared by all of the threads in a process. everybody in the group chat sees the same limits ๐ฏ
lowering the soft limit for a resource below the process's current consumption of that resource will succeed (but it will prevent the process from further increasing its consumption of the resource). so you can trap yourself, basically ๐ชค
you can set the resource limits of the shell using the built-in ulimit command (limit in csh(1)). and the shell's resource limits are inherited by the processes that it creates to execute commands.
since Linux 2.6.24, the resource limits of any process can be inspected via /proc/pid/limits; see proc(5).
ancient systems provided a vlimit() function with a similar purpose to setrlimit(). for backward compatibility, glibc also provides vlimit(). but all new applications should be written using setrlimit(). periodt ๐
C library/kernel ABI differences
since glibc 2.13, the glibc getrlimit() and setrlimit() wrapper functions no longer invoke the corresponding system calls, but instead employ prlimit(), for the reasons described in BUGS below.
the name of the glibc wrapper function is prlimit(); the underlying system call is prlimit64().
๐ BUGS ๐
in older Linux kernels, the SIGXCPU and SIGKILL signals delivered when a process encountered the soft and hard RLIMIT_CPU limits were delivered one (CPU) second later than they should have been. this was fixed in Linux 2.6.8 ๐ฉน
in Linux 2.6.x kernels before Linux 2.6.17, a RLIMIT_CPU limit of 0 is wrongly treated as "no limit" (like RLIM_INFINITY). since Linux 2.6.17, setting a limit of 0 does have an effect, but is actually treated as a limit of 1 second.
a kernel bug means that RLIMIT_RTPRIO does not work in Linux 2.6.12; the problem is fixed in Linux 2.6.13.
in Linux 2.6.12, there was an off-by-one mismatch between the priority ranges returned by getpriority(2) and RLIMIT_NICE. this had the effect that the actual ceiling for the nice value was calculated as 19 - rlim_cur. this was fixed in Linux 2.6.13.
since Linux 2.6.12, if a process reaches its soft RLIMIT_CPU limit and has a handler installed for SIGXCPU, then, in addition to invoking the signal handler, the kernel increases the soft limit by one second. this behavior repeats if the process continues to consume CPU time, until the hard limit is reached, at which point the process is killed ๐ other implementations do not change the RLIMIT_CPU soft limit in this manner, and the Linux behavior is probably not standards conformant; portable applications should avoid relying on this Linux-specific behavior. the Linux-specific RLIMIT_RTTIME limit exhibits the same behavior when the soft limit is encountered.
kernels before Linux 2.4.22 did not diagnose the error EINVAL for setrlimit() when rlim->rlim_cur was greater than rlim->rlim_max.
Linux doesn't return an error when an attempt to set RLIMIT_CPU has failed, for compatibility reasons. silently failing, the iconique move ๐
Representation of "large" resource limit values on 32-bit platforms
the glibc getrlimit() and setrlimit() wrapper functions use a 64-bit rlim_t data type, even on 32-bit platforms. however, the rlim_t data type used in the getrlimit() and setrlimit() system calls is a (32-bit) unsigned long. furthermore, in Linux, the kernel represents resource limits on 32-bit platforms as unsigned long. but here's the issue queen: a 32-bit data type is not wide enough ๐ฌ
the most pertinent limit here is RLIMIT_FSIZE, which specifies the maximum size to which a file can grow: to be useful, this limit must be represented using a type that is as wide as the type used to represent file offsets โ that is, as wide as a 64-bit off_t (assuming a program compiled with _FILE_OFFSET_BITS=64).
to work around this kernel limitation, if a program tried to set a resource limit to a value larger than can be represented in a 32-bit unsigned long, then the glibc setrlimit() wrapper function silently converted the limit value to RLIM_INFINITY. in other words, the requested resource limit setting was silently ignored ๐
since glibc 2.13, glibc works around the limitations of the getrlimit() and setrlimit() system calls by implementing setrlimit() and getrlimit() as wrapper functions that call prlimit().
๐งช EXAMPLES ๐งช
the program below demonstrates the use of prlimit().
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#include <err.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <time.h>
int
main(int argc, char *argv[])
{
pid_t pid;
struct rlimit old, new;
struct rlimit *newp;
if (!(argc == 2 || argc == 4)) {
fprintf(stderr, "Usage: %s <pid> [<new-soft-limit> "
"<new-hard-limit>]\n", argv[0]);
exit(EXIT_FAILURE);
}
pid = atoi(argv[1]); /* PID of target process */
newp = NULL;
if (argc == 4) {
new.rlim_cur = atoi(argv[2]);
new.rlim_max = atoi(argv[3]);
newp = &new;
}
/* Set CPU time limit of target process; retrieve and display
previous limit */
if (prlimit(pid, RLIMIT_CPU, newp, &old) == -1)
err(EXIT_FAILURE, "prlimit-1");
printf("Previous limits: soft=%jd; hard=%jd\n",
(intmax_t) old.rlim_cur, (intmax_t) old.rlim_max);
/* Retrieve and display new CPU time limit */
if (prlimit(pid, RLIMIT_CPU, NULL, &old) == -1)
err(EXIT_FAILURE, "prlimit-2");
printf("New limits: soft=%jd; hard=%jd\n",
(intmax_t) old.rlim_cur, (intmax_t) old.rlim_max);
exit(EXIT_SUCCESS);
}
๐ SEE ALSO ๐
go read these next, bestie, they're all connected ๐ฆ
prlimit(1)dup(2)fcntl(2)fork(2)getrusage(2)mlock(2)mmap(2)open(2)quotactl(2)sbrk(2)shmctl(2)malloc(3)sigqueue(3)ulimit(3)core(5)capabilities(7)cgroups(7)credentials(7)signal(7)
and that's the whole page, you made it to the end ๐ this lil' artifact came from Linux man-pages 6.20, and it was stamped 2026-02-08 โ that's the source and the date, keep them in the group chat receipts. now go forth and set your limits responsibly ๐ โจ๐ซถ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 1992, Drew Eckhardt <drew@cs.colorado.edu>
- Copyright 2002-2010, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms