ulimit

Controlling the resources of shell programs

Supplementary instructions

ulimit command is used to restrict system users' access to shell resources. If you don’t understand what it means, the following paragraph can help you understand:

Suppose there is a situation where 10 people are logged in to a Linux host at the same time. If the system resources are unlimited, these 10 users open 500 documents at the same time. Assuming that the size of each document is 10M, this At this time, the system's memory resources will be greatly challenged.

The actual application environment is much more complex than this assumption. For example, in an embedded development environment, all aspects of resources are very scarce, such as the number of open file descriptors, the size of the allocation stack, CPU time, virtual Memory size, etc., have very strict requirements. Reasonable restriction and allocation of resources is not only a necessary condition to ensure system availability, but is also inextricably linked to the performance of software running on the system. At this time, ulimit can play a big role. It is a simple and effective way to implement resource limits.

ulimit is used to limit the resources occupied by the shell startup process and supports the following types of restrictions: the size of the kernel file created, the size of the process data block, the size of the file created by the Shell process, the size of the memory lock, and the resident memory The size of the set, the number of open file descriptors, the maximum size of the allocation stack, CPU time, the maximum number of threads for a single user, the maximum virtual memory that can be used by the shell process. At the same time, it supports both hard and soft resource limits.

As a temporary limit, ulimit can act on the shell session logged in by using its command. The limit ends when the session terminates and does not affect other shell sessions. For long-term fixed limits, the ulimit command statement can be added to the file read by the login shell, acting on a specific shell user.

grammar

ulimit(option)

Options

-a: Display the current resource limit settings;
-c <core file upper limit>: Set the maximum value of the core file, in blocks;
-d <data section size>: the maximum value of the program data section, in KB;
-e default process priority, the smaller the value, the higher the priority
-f <file size>: The largest file that the shell can create, in blocks;
-H: Set the hard limit of resources, which is the limit set by the administrator;
-m <memory size>: Specifies the upper limit of usable memory, in KB;
-n <Number of files>: Specifies the maximum number of files that can be opened at the same time;
-p <buffer size>: Specifies the size of the pipe buffer, in 512 bytes;
-s <Stack size>: Specifies the upper limit of the stack, in KB;
-S: Set the elastic limit of resources;
-t <CPU time>: Specifies the upper limit of CPU usage time, in seconds;
-u <Number of programs>: The maximum number of programs that a user can open;
-v <virtual memory size>: Specifies the upper limit of virtual memory that can be used, in KB.

Example

[root@localhost ~]# ulimit -a
core file size (blocks, -c) 0 #The maximum value of core file is 100 blocks.
data seg size (kbytes, -d) unlimited #The data segment of the process can be arbitrarily large.
scheduling priority (-e) 0
file size (blocks, -f) unlimited #The file can be arbitrarily large.
pending signals (-i) 98304 #There are up to 98304 pending signals.
max locked memory (kbytes, -l) 32 #The maximum value of physical memory locked by a task is 32KB.
max memory size (kbytes, -m) unlimited #The maximum value of a task’s resident physical memory.
open files (-n) 1024 #A task can open up to 1024 files at the same time.
pipe size (512 bytes, -p) 8 #The maximum space of the pipe is 4096 bytes.
POSIX message queues (bytes, -q) 819200 #The maximum value of POSIX message queue is 819200 bytes.
real-time priority (-r) 0
stack size (kbytes, -s) 10240 #The maximum value of the process stack is 10240 bytes.
cpu time (seconds, -t) unlimited #CPU time used by the process.
max user processes (-u) 98304 #The maximum number of processes (including threads) opened by the current user at the same time is 98304.
virtual memory (kbytes, -v) unlimited #There is no limit to the maximum address space of the process.
file locks (-x) unlimited #There is no limit to the maximum number of files that can be locked.