A method of calling--getrlimit () and Setrlimit () functions based on Linux system _c language

Source: Internet
Author: User
Tags arithmetic cpu usage
Function Description:
Gets or sets the resource usage limit. Each resource has an associated soft and hard limit, which is the limit that the kernel imposes on the resource, and the hard limit is the maximum value of the soft limit. The 0~ call process can only specify its soft limit as a value in the hard limit range, while reducing its hard limit irreversibly. The authorization process can arbitrarily change its soft and hard limits. The value of Rlim_infinity indicates no resource restrictions.
Usage:
Copy Code code as follows:

#include <sys/resource.h>
int getrlimit (int resource, struct rlimit *rlim);
int setrlimit (int resource, const struct RLIMIT *rlim);

Parameters:
Resource: The possible options are
rlimit_asThe maximum virtual memory space for the process, in bytes.
Rlimit_coreThe maximum length of the kernel dump file.
rlimit_cpuThe maximum allowable CPU usage time, in seconds. When a process reaches a soft limit, the kernel sends it a SIGXCPU signal, and the default behavior of the signal is to terminate the execution of the process. However, signals can be captured, and handle handles can return control to the main program. If the process continues to consume CPU time, the core sends the SIGXCPU signal at once per second, until the hard limit is reached, at which point the process is sent a sigkill signal to terminate its execution.
Rlimit_dataThe maximum value of the 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 a SIGXFSZ signal to it, and by default the execution of the process is terminated.
Rlimit_locksThe maximum number of locks and leases that a process can establish.
Rlimit_memlockThe maximum amount of data that a process can lock in memory, in bytes.
Rlimit_msgqueueThe maximum number of bytes that a process can allocate for POSIX message queues.
Rlimit_niceThe maximum perfect value that a process can set through setpriority () or Nice () calls.
Rlimit_nofileSpecifies a value that is greater than the maximum number of file descriptors that can be opened by the process, exceeding this value, resulting in a emfile error.
Rlimit_nprocThe maximum number of processes that a user can have.
Rlimit_rtprioThe maximum real-time priority that a process can set through Sched_setscheduler and Sched_setparam.
rlimit_sigpendingThe maximum number of pending signals that the user can have.
Rlimit_stackThe largest process stack, in bytes.
Rlim: A structure that describes the soft and hard limits of resources, with prototypes as follows
Copy Code code as follows:

struct Rlimit {
Rlim_t rlim_cur;
Rlim_t Rlim_max;
};

Return Description:
When successfully executed, returns 0. Failure returns -1,errno is set to one of the following values
Efault:rlim The space that the pointer points to is inaccessible
EINVAL: Invalid Parameter
Eperm: When increasing resource limits, power does not allow

Extended reading:
Ulimit and Setrlimit easy to modify task process resource cap values
In a Linux system, resouce limit refers to the amount of resources that it can obtain during the execution of a process, such as the maximum value of the core file of a process, the maximum value of virtual memory, and so on.
The size of the resouce limit can directly affect the execution status of the process. It has two most important concepts: soft limit and hard limit.
Copy Code code as follows:

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

Soft limit refers to the resource caps that the kernel can support. For example, for rlimit_nofile (a process can open the maximum number of files, the kernel default is 1024), soft limit maximum can only reach 1024. For Rlimit_core (core file size, the kernel does not limit), soft limit maximum can be unlimited.
Hard limit in resources only as the upper limit of soft limit. After you set up hard limit, you can set the soft limit only less than hard limit. To illustrate, hard limit only for unprivileged processes, that is, the process's valid user ID (Effective) is not a 0 process. A process with a privileged level (with attribute Cap_sys_resource), soft limit only the kernel upper limit.
We can look at the output of the following two commands.
Copy Code code as follows:

sishen@sishen:~$ ulimit-c-n-s
Core file size (blocks,-c) 0
Open files (-N) 1024
Stack size (Kbytes,-s) 8192
sishen@sishen:~$ ulimit-c-n-s-H
Core file size (blocks,-c) Unlimited
Open files (-N) 1024
Stack size (Kbytes,-s) unlimited

-h indicates that hard limit is displayed. The difference between soft limit and hard limit can be seen from the result. Unlimited represents no limit, which is the maximum value of the kernel.
There are two ways to read changes to Resouce limit.
* Use Shell built-in command ulimit
* Using the Getrlimit and Setrlimit APIs
Ulimit is the resouce limit that changes the shell and achieves the resouce limit effect of changing the shell-initiated process (subprocess inheritance).
Copy Code code as follows:

usage:ulimit [-SHACDEFILMNPQRSTUVX [limit]]

When limit is not specified, the command displays the current value. Note here 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 the setting can only be reduced not increase. Therefore, it is recommended to use the Ulimit setting limit parameter is plus-S.
the use of getrlimit and Setrlimit is also very simple, manpage has a very clear description.
Copy Code code as follows:

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

Note that you are setrlimit and need to check to see if the new value is over hard limit. The following example Linux system in the process of running the application will often encounter a sudden collapse of the program, prompted: Segmentation fault, this is because the application received the 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 into the core file, and the default name of the core file is "core" (this is a long-standing feature of the Unix class system).

In fact, not only SIGSEGV signal generation Coredump, there are 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. In a Linux system, a program crashes frequently when the application is running, prompting: segmentation fault, this is because the application received the 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 into the core file, and the default name of the core file is "core" (this is a long-standing feature of the Unix class system).

In fact, not only SIGSEGV signal generation Coredump, there are 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. For the resouce limit There are two ways to read changes.
* Use Shell built-in command ulimit
* Using Getrlimit and Setrlimit apisetrlimit:
Copy Code code as follows:

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);
}
}

Related Article

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.