注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

liuyue18301的个人主页

追逐梦想 光辉岁月

 
 
 

日志

 
 

C语言系统资源控制(getrlimit && setrlimit)  

2010-01-12 18:21:09|  分类: c语言 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
每一个进程都有自己的一组资源限制,在(*)inux系统中我们可以通过
#include <sys/resource.h>
int getrlimit(int resource, struct rlimit *rlim);
int setrlimit(int resource, const struct rlimit *rlim);
这2个API来取得和设置资源
getrlimit用来取得setrlimit用来设置 这二个参数都需要一个要控制的资源 比如控制CPU、内存、文件描述符个数等等的控制,作为第一个参数传入,第二个参数是一个rlimit的结构体地址(指针),他的结构如下定义:
定义放在头文件/usr/include/bits/resource.h中
struct rlimit
       {
         /* The current (soft) limit.       */
         rlim_t rlim_cur;
         /* The hard limit.       */
         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
  评论这张
 
阅读(208)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018