Technical skill

Linux Process: limits

A running program has its own limits

Imagine a program opens many files or uses a lot of memory. A limit can cap how much of a selected resource one running program may use. If the program reaches that cap, the operation may fail; the entire computer does not necessarily stop. A running program is called a process. Linux calls these per-process caps resource limits, often called an rlimit.

You can check the limits in the shell where a command will run:

ulimit -Sn
ulimit -Hn
cat /proc/$$/limits

ulimit -a lists the shell's limits. -S selects a soft limit, -H a hard limit, and -n the open-file limit. cat /proc/$$/limits shows the resource names, values, and units for that shell. For another process, read /proc/PID/limits, replacing PID with its process ID. The file may be unavailable if that process exits during inspection; permissions and procfs configuration can also limit access.

What soft and hard limits mean

Each resource has a soft limit and a hard limit. The kernel enforces the soft value. An unprivileged process can raise its soft value only as far as the hard value permits. It can lower the hard value but cannot raise it again without the required privilege. A child created with fork() inherits its parent's limits, and those limits remain when a program replaces its image with execve(). The settings belong to the process launch chain, not to the whole machine.

Different limits affect different operations. RLIMIT_NOFILE caps open file descriptors, the small handles a program uses for files and sockets. RLIMIT_CORE limits the size of a core file, a diagnostic file that may be written after some program failures. RLIMIT_CPU limits CPU time and involves signals when reached. Trying to open another descriptor can fail, while a CPU-time limit has a different effect. Inspect the resource tied to the failure instead of assuming every limit stops a process in the same way.

Change the setting before starting the program

In Bash, ulimit -Sn 2048 asks the current shell to set its soft open-file limit to 2048. The request must not exceed the hard limit. It changes the shell and programs started from it afterward, not programs that are already running. Lowering a hard limit can be irreversible for an unprivileged process, so check both values first. Read the limits again afterward to confirm what the shell accepted.

The shell's settings pass to programs it launches. A command from a new terminal can therefore have different limits from a long-running service started by a manager or another parent process. When a program reports an unexpected value, inspect that program's /proc/PID/limits and how it was launched.

A process limit and a cgroup limit apply at different scopes. An rlimit applies to an individual process and its threads. A cgroup controller can apply a resource policy to a group of processes, such as those belonging to a service. A per-process open-file limit and a service-level memory cap answer different questions. Check which scope produces the behavior before changing a setting, and review the launch manager's configuration when a service differs from an interactive shell.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful