Getrlimit and setrlimit

Source: Internet
Author: User

Getrlimit and setrlimit Functions
Each process has a set of resource restrictions, some of which can be queried and modified using the getrlimit and setrlimit functions.

# Include
# Include
Int getrlimit (INT resource, struct rlimit * rlptr );
Int setrlimit (INT resource, const struct rlimit rlptr );
Both return: 0 if OK, nonzero on error two functions

Return Value: If the success is 0, the error is not 0.

Specify a resource and a pointer pointing to the following structure for each call of these two functions.

Struct rlimit {
Rlim ur;/* Soft limit: current limit */
Rlim ax;/* hard limit: maximum value of rlimcur */
};

These two functions do not belong to posix.1, but svr4 and 4.3 + BSD provide them. Svr4 uses the basic system data type rlim in the above structure. For other systems

The two members are defined as integer or long integer.

Process resource restrictions are generally established by the 0 # process at the beginning of the system, and then inherited by the subsequent process. In svr4, you can view

The/etc/CONF/CFD/mtune file is in 4.3 + BSD, and the default value of the system is scattered in multiple head files.

When changing resource limits, you must follow the following three rules:

1. Any process can change a soft limit to a value smaller than or equal to its hard limit.
2. Any process can reduce its hard limit value, but it must be greater than or equal to its soft limit value. This reduction cannot be reversed for common users.
3. Only Super Users can raise the hard limit.

An infinite limit is specified by the constant rlim nfinity.

The resource parameters of these two functions take one of the following values. Note that not all resource restrictions are supported by svr4 and 4.3 + BSD.

The maximum number of bytes of the rlmitcore (svr4 and 4.3 + BSD) core file. If the value is 0, the creation of the core file is blocked.
The maximum CPU time of the rlimit Pu (svr4 and 4.3 + BSD). When the limit is exceeded, the sigxcpu signal is sent to the process.
The maximum length of the rlimit ATA (svr4 and 4.3 + BSD) data segment. This is the sum of initialization data, non-initialization data, and heap in Figure 7.3.
Rlimit size (svr4 and 4.3 + BSD) indicates the maximum length of a file. When this soft limit is exceeded, the sigfsz signal is sent to the process.
Rlimit emlock (4.3 + BSD) is locked in the memory address space (implemented at the end ).
The maximum number of files opened by each process in rlimit ofile (svr4. Changing this limit affects the value returned by the sysconf function in the parameter-SC-OPEN-MAX (section 2.5.4

). See program 2.3.
Rlimit proc (4.3 + BSD) the maximum number of sub-processes owned by each actual user ID. Changing this limit will affect the value returned by the sysconf function in the childmax parameter.

Value (section 2.5.4 ).
The rlimit file (4.3 + BSD) is the same as the rlimit ofile of svr4.
Rlimit SS (4.3 + BSD) Maximum length of bytes in memory (RSS ). If the physical memory is used for subrequest, the system core will retrieve the part beyond RSS from the process.
The maximum byte length of the rlimit tack (svr4 and 4.3 + BSD) stack. See the Figure 7.3.
Rlimit MEM (svr4) can reflect the maximum length of bytes in the address space. This affects the MMAP function (section 12.9 ).

Resource restrictions affect the calling process and are inherited by its child processes. This means that in order to affect all subsequent processes of a user, you need to set the resource limit in Shell

. Indeed, Bourne shell and kornshell have internal ulimit commands, and cshell has internal limit commands. (Umask and chdir must also be in shell)

.

Earlier bourmeshell, such as one provided by Berkeley, does not support the ulimit command. The new kornshell ulimit command has the-H and-s options

Check and modify the hard and soft limits respectively, but they are still written into the document.

?

? Simple use cases:

# Include <sys/time. h>
# Include <sys/resource. h>
# Include <unistd. h>
Int main ()
{
Struct rlimit limit;
Char P = '1 ';
Limit. rlim_cur = rlim_infinity;
Limit. rlim_max = rlim_infinity;
If (setrlimit (rlimit_core, & limit ))
{
Printf ("set limit failed/N ");
}

Printf ("P = % s/n", P );
}

Certificate ---------------------------------------------------------------------------------------------------------------------------------------------

Each process has a set of resource restrictions, some of which can be queried and modified using the getrlimit and setrlimit functions.

Getrlimit and setrlimit: resource limit (getrlimit and setrlimit: resource limits)

  • These two system functions are related to resource limit. Have you used the ulimit command? (I have never used it anyway)
  • For each resource, there are two limit: one is hard limit and the other is soft limit. The latter cannot exceed the former, and only processes with superuser permissions can change the former.
  • The getrlimit and setrlimit parameters are the same: the first parameter is the resource type, and the second parameter is the rlimit structure pointer. In this structure, there are only two members: Hard limit and soft limit.
  • Several important resource types
    • Rlimit_cpu: Maximum CPU time for program execution, in seconds. The program is aborted after it is exceeded. The abort signal is sigxcpu.
    • Rlimit_data: maximum memory for Program Execution
    • Rlimit_nproc: Maximum number of child processes hatched by the program
    • Rlimit_nofile: Maximum number of files opened by the program

Reference: http://blog.csdn.net/zhoujunyi/archive/2007/04/14/1564579.aspx

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.