struct rlimit介绍

1、rlimit介绍

在Linux系统中,Resouce limit指在一个进程的执行过程中,它所能得到的资源的限制,比如进程的core file的最大值,虚拟内存的最大值等。

 

Resouce limit的大小可以直接影响进程的执行状况。其有两个最重要的概念:soft limit 和 hard limit。

struct rlimit {
rlim_t rlim_cur;
rlim_t rlim_max;
};

 

是指内核所能支持的资源上限。比如对于RLIMIT_NOFILE(一个进程能打开的最大文件 数,内核默认是1024),soft limit最大也只能达到1024。对于RLIMIT_CORE(core文件的大小,内核不做限制),soft limit最大能是unlimited。
hard limit在资源中只是作为soft limit的上限。当你设置hard limit后,你以后设置的soft limit只能小于hard limit。要说明的是,hard limit只针对非特权进程,也就是进程的有效用户ID(effective user ID)不是0的进程。具有特权级别的进程(具有属性CAP_SYS_RESOURCE),soft limit则只有内核上限。


soft limit

 

 

~>ulimit -c -n -s 

core file size          (blocks, -c) 0
open files                      (-n) 100001
stack size              (kbytes, -s) 8192

 

~>ulimit -c -n -s -H
core file size          (blocks, -c) unlimited
open files                      (-n) 100001
stack size              (kbytes, -s) unlimited

-H表示显示的是hard limit。从结果上可以看出soft limit和hard limit的区别。unlimited表示no limit, 即内核的最大值。


对于resouce limit的读取修改,有两种方法。

使用shell内建命令ulimit 
使用getrlimit和setrlimit API

 

ulimit是改变shell的resouce limit,并达到改变shell启动的进程的resouce limit效果(子进程继承)。
usage:ulimit [-SHacdefilmnpqrstuvx [limit]]
当不指定limit的时候,该命令显示当前值。这里要注意的是,当你要修改limit的时候,如果不指定-S或者-H,默认是同时设置soft limit和hard limit。也就是之后设置时只能减不能增。所以,建议使用ulimit设置limit参数是加上-S。
getrlimit和setrlimit的使用也很简单,manpage里有很清楚的描述。
int getrlimit(int resource, struct rlimit *rlim);
int setrlimit(int resource, const struct rlimit *rlim);
需要注意的是你在setrlimit,需要检查是否成功来判断新值有没有超过hard limit。如下例:
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);
}
}

 

2、setrligetrlimit和setrlimit函数

每个进程都有一组资源限制,其中某一些可以用getrlimit和setrlimit函数查询和更改。

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

返回:若成功为0,出错为非0

对这两个函数的每一次调用都指定一个资源以及一个指向下列结构的指针。

getrlimit用来取得setrlimit用来设置 这二个参数都需要一个要控制的资源比如控制CPU、内存、文件描述符个数等等的控制,作为第一个参数传入,第二个参数是一个rlimit的结构体地址(指针),他的结构如下定义:
定义放在头文件/usr/include/bits/resource.h中
struct rlimit
{        
   rlim_t rlim_cur;        
   rlim_t rlim_max;
};
结构体中 rlim_cur是要取得或设置的资源软限制的值,rlim_max是硬限制
这两个值的设置有一个小的约束:
1) 任何进程可以将软限制改为小于或等于硬限制
2)任何进程都可以将硬限制降低,但普通用户降低了就无法提高,该值必须等于或大于软限制
3) 只有超级用户可以提高硬限制
一个无限的限制由常量RLIM_INFINITY指定(The       value       RLIM_INFINITY       denotes no limit on a resource )

RLIMIT_AS
                   The       maximum       size       of       the       process鈙       virtual memory (address
                   space) in bytes.       This limit affects calls to       brk(2),       mmap(2)
                   and       mremap(2), which fail with the error ENOMEM upon exceeding
                   this limit. Also automatic stack expansion will fail (and       gen-
                   erate       a SIGSEGV that kills the process when no alternate stack
                   has been made available).        Since       the       value       is       a       long,       on
                   machines with a 32-bit long either this limit is at most 2 GiB,
                   or this resource is unlimited.
RLIMIT_CORE
                   Maximum size of core file. When 0 no core dump files       are       cre-
                   ated.       When nonzero, larger dumps are truncated to this size.
设定最大的core文件,当值为0时将禁止core文件非0时将设定产生的最大core文件大小为设定的值
RLIMIT_CPU
                   CPU       time       limit in seconds.       When the process reaches the soft
                   limit, it is sent a SIGXCPU signal.        The       default       action       for
                   this       signal       is to terminate the process.       However, the signal
                   can be caught, and the handler can return control to       the       main
                   program.       If the process continues to consume CPU time, it will
                   be sent SIGXCPU       once       per       second       until       the       hard       limit       is
                   reached,       at which time it is sent SIGKILL.       (This latter point
                   describes Linux 2.2 and 2.4 behaviour.       Implementations vary in
                   how       they       treat       processes       which continue to consume CPU time
                   after reaching the soft limit.       Portable applications that need
                   to catch this signal should perform an orderly termination upon
                   first receipt of SIGXCPU.)
CPU时间的最大量值(秒),当超过此软限制时向该进程发送SIGXCPU信号
RLIMIT_DATA

                   The maximum size of the       process鈙       data       segment       (initialized
                   data,       uninitialized data, and heap).       This limit affects calls
                   to brk() and sbrk(), which fail       with       the       error       ENOMEM       upon
                   encountering the soft limit of this resource.
数据段的最大字节长度
RLIMIT_FSIZE
                   The       maximum       size       of       files       that       the       process       may       create.
                   Attempts to extend a file beyond this limit result in       delivery
                   of a SIGXFSZ signal.       By default, this signal terminates a pro-
                   cess, but a process can catch this       signal       instead,       in       which
                   case the relevant system call (e.g., write(), truncate()) fails
                   with the error EFBIG.
可以创建的文件的最大字节长度,当超过此软限制时向进程发送SIGXFSZ
RLIMIT_MEMLOCK

                   The maximum number of bytes       of       virtual       memory       that       may       be
                   locked into RAM using mlock() and mlockall().
RLIMIT_NOFILE
                   Specifies       a value one greater than the maximum file descriptor
                   number that can be opened by this process.        Attempts       (open(),
                   pipe(),       dup(),       etc.)        to       exceed       this limit yield the error
                   EMFILE.
每个进程能够打开的最多文件数。更改此限制将影响到sysconf函数在参数_SC_CHILD_MAX中的返回值
RLIMIT_OFILE is the BSD name for RLIMIT_NOFILE.
这里BSD系统中RLIMIT_NOFILE的别名
RLIMIT_NPROC

                   The maximum number of processes that can       be       created       for       the
                   real       user       ID       of the calling process.       Upon encountering this
                   limit, fork() fails with the error EAGAIN.
每个实际用户ID所拥有的最大子进程数,更改此限制将影响到sysconf函数在参数_SC_CHILD_MAX中返回的值
RLIMIT_RSS

                   Specifies the limit (in pages) of the       process鈙       resident       set
                   (the number of virtual pages resident in RAM).       This limit only
                   has effect in Linux 2.4 onwatrds, and there only affects       calls
                   to madvise() specifying MADVISE_WILLNEED.
最大驻内存集字节长度(RSS)如果物理存储器供不应求则内核将从进程处取回超过RSS的部份
RLIMIT_STACK

                   The maximum size of the process stack, in bytes.       Upon reaching
                   this limit, a SIGSEGV signal is generated.       To handle this sig-
                   nal,       a       process must employ an alternate signal stack (sigalt-
                   stack(2)).
栈的最大长度
RLIMIT——VMEM 可映照地址空间的最大字节长茺,这影响到mmap函数
这些限制影响到调用进程并由子进程继承!可以在SHELL中预设这些值ulimit命令设置

posted @ 2013-03-27 10:33  小尼人00  阅读(341)  评论(0编辑  收藏  举报