UNIX system functions
Name
Getrlimit and setrlimit-control the maximum usage of system resources. [1] [2]
Outline
# Include <sys/resource. h>
Int getrlimit (INT resource, struct rlimit * RLP );
Int setrlimit (INT resource, const struct rlimit * RLP );
Description
In the operating system, we can use the getrlimit () and setrlimit () functions to obtain and set the maximum usage of various system resources that can be created by each process.
Call getrlimit () or setrlimit () to operate on the upper limit of a specified resource. Resource restrictions are a pair of values: One specifies the current (soft) limit, and the other represents a maximum (hard) limit. A soft restriction can be changed by a process, as long as it is ensured that it is not greater than the hard limit. A process can (cannot be recalled) reduce its hardware restrictions, provided that it is greater than or equal to the soft limit. Hardware restrictions can be raised only when a process works under the permissions of a system Super User. Call setrlimit () to change the hard and soft limits. The limit value may be an "infinite" value: rlim_infinity. The RLP parameter is a pointer to the rlimit struct, which contains the following members:
Rlim_t rlim_cur;/* Current (soft) limit */
Rlim_t rlim_max;/* hard limit */
Rlim_t is an arithmetic data type, which is equivalent to int, size_t, and off_t.
The resource restriction types are described as follows:
Rlimit_core
The maximum number of bytes of the core file. If the value is 0, the creation of the core file is blocked.
Rlimit_cpu
The maximum value of CPU time (in seconds). When this soft limit is exceeded, the system sends the S I g x C P U signal to the process.
Rlimit_data
The maximum length of a process's data segment. The sum of initialized data, non-initialized data, and heap in the data segment. When the BRK function is called to dynamically change the data segment of a process, if it fails, the errno value will be set to enomem.
Rlimit_fsize
The maximum length of a file that can be created. When this soft limit is exceeded, the sigxfsz signal is sent to the process.
Rlimit_nofile
Maximum number of files that a process can open.
Rlimit_stack
The maximum length of the stack. The system does not dynamically increase the stack size limit.
Within the process, setrlimit () will increase the limit to suit your stack size without moving the current memory segment to allow growth. To ensure that the process stack can grow, the limit must first change to adapt to the new stack size used by the running process.
In a multi-threaded process, if setrlimit () is called in the thread and the thread is not the main thread, the stack restrictions are not changed. Using the rlimit_stack parameter to call setrlimit () can only affect the stack of the main thread, and this should have been done by the main thread.
When the stack size limit is exceeded, the signal SIGSEGV is sent to the process. If the process ignores the signal or captures the signal but does not set any processing measures, the signal is set to the sig_dfl processing scheme by default.
Rlimit_vmem
The maximum length of bytes that can be mapped to the address space.
Rlimit_as
Maximum number of available memory bytes of a process.
Because the restriction information is stored in the information of each process, the shell creation restriction command must directly call these two system functions to affect all processes created after the shell.
The values currently restricted by the following resources will affect the expansion of the corresponding macro definition.
Limit macro definition Expansion
Rlimit_fsize fchr_max
Rlimit_nofile open_max
When the getrlimit () function is used, if a resource restriction can be correctly assigned a value of Type rlim_t, the value is returned. Otherwise, if the resource restriction is equivalent to the corresponding hardware limitation, the returned value is rlim_saved_max; otherwise, the returned value is rlim_saved_cur.
When setrlimit () is used, if the new limit value of the request is rlim_infinity, the new limit will be "unlimited"; otherwise, if the new limit of the request is rlim_saved_max, the new limit will be saved as a hardware limit. If the new limit of the request is rlim_saved_cur, the new limit will be saved as a soft limit; otherwise, the new limit will be assigned as a request value. In addition, if the input limit value is correct, the limit is treated as a new limit value.
The result of setting a limit of rlim_saved_max or rlim_saved_cur is unknown unless you call getrlimit to learn the corresponding soft limit or hard limit values.
A limit allows a value greater than rlim_infinity.
Return Value
After successful completion, getrlimit () and setrlimit () return 0. Otherwise,-1 is returned and errno is set to specify the corresponding error.
Error
The getrlimit () and setrlimit () functions fail as follows:
Efault
The RLP parameter points to an Invalid Address.
Einval
An invalid resource is specified. Alternatively, when the setrlimit () function is called, The New rlim_cur value exceeds the new rlim_max value.
Eperm
The setrlimit function is called to increase the maximum limit value, but the process is not a superuser.
The setrlimit function may fail:
Einval
The specified limit value cannot be reduced because the current usage is greater than the specified limit value.