Getrlimit and Setrlimit system calls for Linux

Source: Internet
Author: User
Tags arithmetic cpu usage

Transferred from: http://www.cnblogs.com/niocai/archive/2012/04/01/2428128.html

Function Description:
Gets or sets the resource usage limit. Each resource has a related soft and hard limit, and the soft limit is the limit that the kernel imposes on the corresponding resource, and the hard limit is the maximum of the soft limit. The non-authoritative calling process can only specify its soft limit as a value in the 0~ hard limit range, and can irreversibly reduce its hard limit. The authorization process can arbitrarily change its soft and hard limits. The value of Rlim_infinity indicates no resource restriction.


Usage:

#include <sys/resource.h>

int getrlimit (int resource, struct rlimit *rlim);
int setrlimit (int resource, const struct RLIMIT *rlim);

Parameters:

Resource: Possible options are

The maximum virtual memory space for the rlimit_as//process, in bytes.
The maximum length of the rlimit_core//kernel dump file.
RLIMIT_CPU//Maximum allowable CPU usage time, in seconds. When the process reaches the soft limit, the kernel sends a SIGXCPU signal to it, and the default behavior of the signal is to terminate the execution of the process. However, you can capture the signal and handle the handle to return control to the main program. If the process continues to consume CPU time, the core sends it a SIGXCPU signal at the frequency of every second until the hard limit is reached, at which point the process sends a SIGKILL signal to terminate its execution.
The maximum value of the Rlimit_data//process data segment.
The maximum length of the file that the Rlimit_fsize//process can establish. If a process attempts to exceed this limit, the core sends it a SIGXFSZ signal, which by default terminates the execution of the process.
Rlimit_locks//process can establish the maximum number of locks and leases.
The maximum amount of data, in bytes, that the Rlimit_memlock//process can lock in memory.
The maximum number of bytes that the Rlimit_msgqueue//process can allocate for POSIX Message Queuing.
The Rlimit_nice//process can be set to the maximum perfect value by setpriority () or Nice () call.
Rlimit_nofile//Specifies a value that exceeds the maximum number of file descriptors that can be opened by a process, beyond which a emfile error will be generated.
Rlimit_nproc//The maximum number of processes a user can have.
The maximum real-time priority that Rlimit_rtprio//processes can set through Sched_setscheduler and Sched_setparam.
Rlimit_sigpending//The maximum number of pending signals a user can have.
Rlimit_stack//maximum process stack, in bytes.

Rlim: A structure that describes the soft and hard constraints of resources, the prototype is as follows

struct Rlimit {
Rlim_t rlim_cur;
Rlim_t Rlim_max;
};

Return Description:

When executed successfully, returns 0. The failed return -1,errno is set to one of the following values
The space that the Efault:rlim pointer points to is inaccessible
EINVAL: Invalid Parameter
Eperm: Power is not allowed when adding resource limit values

Extended reading:

Ulimit and Setrlimit easily modify task process resource upper values

In Linux systems, resouce limit refers to the amount of resources that can be obtained during the execution of a process, such as the maximum value of the core file of the process, the maximum value of the virtual memory, and so on.

The size of the resouce limit can directly affect the execution status of the process. It has two of the most important concepts: soft limit and hard limit.

struct Rlimit {
Rlim_t rlim_cur; Soft limit
Rlim_t Rlim_max; Hard Limit
};

Soft limit refers to the resource limit that the kernel can support. For example, for rlimit_nofile (the maximum number of files a process can open, the kernel defaults to 1024x768), and the soft limit can only reach 1024. For Rlimit_core (core file size, the kernel does not limit), soft limit maximum can be unlimited.
Hard limit is only used as the upper limit of soft limit in resources. When you set the hard limit, the soft limit you set later can only be less than hard limit. To illustrate, hard limit is only for non-privileged processes, that is, the process's valid user ID (effective user ID) is not a 0 process. Process with privileged level (with attribute Cap_sys_resource), soft limit has only the kernel limit.


We can look at the output of the following two commands.

[Email protected]:~$ ulimit-c-n-s
Core file size (blocks,-c) 0
Open files (-N) 1024
Stack size (Kbytes,-s) 8192

[Email protected]:~$ ulimit-c-n-s-H
Core file size (blocks,-c) Unlimited
Open files (-N) 1024
Stack size (Kbytes,-s) unlimited

-H means hard limit is displayed. The difference between soft limit and hard limit can be seen from the results. Unlimited represents the No limit, which is the maximum value of the kernel.


There are two methods for read modification of Resouce limit.

* Use shell built-in command ulimit
* Using the Getrlimit and Setrlimit APIs

Ulimit is changing the shell's resouce limit and reaching the resouce limit effect (child process inheritance) that alters the shell-initiated process.

usage:ulimit [-SHACDEFILMNPQRSTUVX [limit]]

When limit is not specified, the command displays the current value. It is important to note that when you want to modify the limit, if you do not specify-s or-H, the default is to set both soft limit and hard limit. That is, after setting it can only be reduced to no increase. Therefore, it is recommended to use Ulimit to set the limit parameter to add-S.


The use of Getrlimit and Setrlimit is also very simple, manpage has a very clear description.

int getrlimit (int resource, struct rlimit *rlim);
int setrlimit (int resource, const struct RLIMIT *rlim);

Note that you are in the Setrlimit and need to check whether the new value has exceeded the hard limit in order to determine the success. The following example Linux system often encounters a sudden crash during application operation, prompting: segmentation fault, because the application received a SIGSEGV signal. This signal indicates that when the process has an invalid storage access, when the signal is received, the default action is: Terminate W/core. The meaning of terminating w/core is that the core file is generated in the current directory of the process and the memory image of the process is copied to the core file, and the default name of the core file is "core" (which is a long-standing feature of the Unix class system).
In fact, not only the SIGSEGV signal produces coredump, but also some of the following signals also produce COREDUMP:SIGABRT (abnormal termination), Sigbus (Hardware failure), SIGEMT (Hardware failure), SIGFPE (arithmetic anomaly), Sigill (illegal hardware instruction), Sigiot (Hardware failure), Sigquit,sigsys (Invalid system call), SIGTRAP (hardware failure), etc. The Linux system often encounters a sudden crash during application operation, prompting: segmentation fault, because the application received a SIGSEGV signal. This signal indicates that when the process has an invalid storage access, when the signal is received, the default action is: Terminate W/core. The meaning of terminating w/core is that the core file is generated in the current directory of the process and the memory image of the process is copied to the core file, and the default name of the core file is "core" (which is a long-standing feature of the Unix class system).
In fact, not only the SIGSEGV signal produces coredump, but also some of the following signals also produce COREDUMP:SIGABRT (abnormal termination), Sigbus (Hardware failure), SIGEMT (Hardware failure), SIGFPE (arithmetic anomaly), Sigill (illegal hardware instruction), Sigiot (Hardware failure), Sigquit,sigsys (Invalid system call), SIGTRAP (hardware failure), etc. There are two methods for read modification of Resouce limit.

* Use shell built-in command ulimit
* Use Getrlimit and setrlimit apisetrlimit:

if (Getrlimit (Rlimit_core, &rlim) ==0) {
Rlim_new.rlim_cur = Rlim_new.rlim_max = rlim_infinity;
if (Setrlimit (Rlimit_core, &rlim_new)!=0) {
Rlim_new.rlim_cur = Rlim_new.rlim_max = Rlim.rlim_max;
(void) Setrlimit (Rlimit_core, &rlim_new);
}
}

Getrlimit and Setrlimit system calls for Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.