prlimit64() — Linux system interface
Query or set process resource limits with glibc prlimit(), which uses the Linux prlimit64 syscall. The public wrapper accepts struct rlimit; its internal resource argument type varies by libc. PID 0 selects the caller. A NULL new_limit requests no change; a NULL old_limit skips reading the previous limit. Soft limits are enforced within resource-specific semantics, hard limits bound unprivileged increases; RLIM_INFINITY represents no limit. RLIMIT_NOFILE limits the descriptor number plus one. EINVAL can indicate invalid limits or resource; EPERM can indicate insufficient privileges; ESRCH can indicate missing target. The example only reads its own limits and compares with getrlimit. Do not assume cgroup limits or system-wide resource availability equal per-process rlimits. Compile the complete C example in a fresh private working directory using gcc -std=c17 -Wall -Wextra -Wpedantic -Werror example.c -o example; run ./example. Linux-specific interface or flags; examples target Linux/glibc on x86_64. These are libc-facing signatures, not raw architecture-specific syscall ABIs.
Formal Syntax & Structure
#define _GNU_SOURCE#include <sys/resource.h>int prlimit(pid_t pid, int resource, const struct rlimit *new_limit, struct rlimit *old_limit); /* manual synopsis; libc header defines actual type */Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| pid | pid_t | Target PID; 0 selects current process. |
| resource | resource selector | RLIMIT_* constant. |
| new_limit | const struct rlimit * | New soft/hard limits or NULL for no change. |
| old_limit | struct rlimit * | Output storage or NULL. |
Return Value
0 on success. On failure, returns -1 and sets errno.
Working Example
#define _GNU_SOURCE#define _POSIX_C_SOURCE 200809L#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/resource.h>
/* A failed check ends this short-lived demonstration. */#define CHECK(x) do { if (!(x)) { \ fprintf(stderr, "check failed at line %d: %s\n", __LINE__, #x); \ return EXIT_FAILURE; \} } while (0)
int main(void){ struct rlimit a, b; CHECK(prlimit(0, RLIMIT_NOFILE, NULL, &a) == 0); CHECK(getrlimit(RLIMIT_NOFILE, &b) == 0); CHECK(a.rlim_cur == b.rlim_cur && a.rlim_max == b.rlim_max); CHECK(a.rlim_cur <= a.rlim_max); puts("own descriptor limits agree; no limit changed"); return EXIT_SUCCESS;}