免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1724 | 回复: 0
打印 上一主题 下一主题

getrlimit 和 setrlimit 函数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-13 10:09 |只看该作者 |倒序浏览

                                                                                                                1.  每个进程都有一组资源限制,其中一些可以用 getrlimit 和 setrlimit 函数查询和更改。
               
               
                    #include sys/resource.h>
int getrlimit(int resource, struct rlimit *rlptr);
int setrlimit(int resource, const struct rlimit *rlptr);
                                        Both return: 0 if OK, nonzero on error
这两个函数在 Single UNIX Specification 中定义为 XSI 扩展。进程的资源限制通常是在系统
初始化时由进程 0 建立的,然后由每个后续进程继承。每种实现都可以用自己的方法对各种限制做出调整。
    struct rlimit {
      rlim_t  rlim_cur;   /* soft limit: current limit */
      rlim_t  rlim_max;   /* hard limit: maximum value for rlim_cur */
    };
在更改资源限制时,须遵循下列三条规则:
    1)任何一个进程都可以将一个软限制值更改为 = 其软限制值。这种降低对普通用户而言是不可逆的。
    3)只有超级用户进程可以提高硬限制值。
   
    注意:
        1. 常量 RLIM_INFINITY 指定了一个无限量的限制。
        2. 可以使用 info ulimit 命令,查看系统支持的资源限制值(上述两个函数的第一个参数 resource)。
APUE2 上的说明:
RLIMIT_AS--    The maximum size in bytes of a process's total available memory.
               This affects the sbrk function (Section 1.11) and the mmap function.
RLIMIT_CORE-- The maximum size in bytes of a core file. A limit of 0 prevents the creation of a core file.
RLIMIT_CPU--  The maximum amount of CPU time in seconds. When the soft limit is exceeded,
                the SIGXCPU signal is sent to the process.
RLIMIT_DATA-- The maximum size in bytes of the data segment: the sum of the initialized      data, uninitialized data, and heap from Figure 7.6.
RLIMIT_FSIZE--The maximum size in bytes of a file that may be created. When the soft limit is exceeded, the process is sent the SIGXFSZ signal.
RLIMIT_LOCKS-- The maximum number of file locks a process can hold. (This number also includes file leases, a Linux-specific feature. See the Linux fcntl(2) manual page for more information.)
RLIMIT_MEMLOCK-- The maximum amount of memory in bytes that a process can lock into memory using mlock(2).
RLIMIT_NOFILE--  The maximum number of open files per process. Changing this limit affects the value returned by the sysconf function for its _SC_OPEN_MAX argument (Section 2.5.4). See Figure 2.16 also.
RLIMIT_NPROC--   The maximum number of child processes per real user ID. Changing this limit affects the value returned for _SC_CHILD_MAX by the sysconf function (Section 2.5.4).
RLIMIT_RSS--     Maximum resident set size (RSS) in bytes. If available physical memory is low, the kernel takes memory from processes that exceed their RSS.
RLIMIT_SBSIZE--  The maximum size in bytes of socket buffers that a user can consume at any given time.
RLIMIT_STACK--   The maximum size in bytes of the stack. See Figure 7.6.
RLIMIT_VMEM--    This is a synonym for RLIMIT_AS.
3. 资源限制影响到调用进程并由其子进程继承。这就意味着为了影响一个用户的所有
            后续进程,需要将资源限制的设置构造在 shell 之中。确实,Bourne shell, GNU Bourne-again shell
            和 Korn shell 具有内置的 ulimit 命令, C shell 具有内置的 limit 命令。(umask 和 chdir 函数
            也必须是 shell 内置的)
        4. 可以在 shell 中执行如下命令,查看当前的资源限制值:
            ulimit -a
        5. getrlimit 获取的长度单位是:字节。cpu 时间单位是:秒。
            ulimit 显示的出来限制值,其单位都有标注。
            
        6. 对于 RLIMIT_CORE 限制值,如果其值为 0, 则阻止创建 core 文件。
            (这是阻止创建 core 文件 5 种情况里的一种特殊情况)。
1-2. 举例说明:
#include stdio.h>
#include stdlib.h>
#include sys/time.h>
#include sys/resource.h>
#if defined(BSD) || defined(MACOS)
#define FMT "%10lld  "
#else
#define FMT "%10ld  "
#endif
#define doit(name) pr_limits(#name, name)
static void pr_limits(char *, int);
int main(int argc, char *argv[])
{
#ifdef RLIMIT_AS
        doit(RLIMIT_AS);
#endif
        doit(RLIMIT_CORE);
        doit(RLIMIT_CPU);
        doit(RLIMIT_DATA);
        doit(RLIMIT_FSIZE);
#ifdef RLIMIT_LOCKS
        doit(RLIMIT_LOCKS);
#endif
#ifdef RLIMIT_MEMLOCK
        doit(RLIMIT_MEMLOCK);
#endif
        doit(RLIMIT_NOFILE);
#ifdef RLIMIT_NPROC
        doit(RLIMIT_NPROC);
#endif
#ifdef RLIMIT_RSS
        doit(RLIMIT_RSS);
#endif
#ifdef RLIMIT_SBSIZE
        doit(RLIMIT_SBSIZE);
#endif
        doit(RLIMIT_STACK);
#ifdef RLIMIT_VMEM
        doit(RLIMIT_VMEM);
#endif
        exit(0);
}
static void pr_limits(char *name, int resource)
{
        struct rlimit limit;
        if (getrlimit(resource, &limit) != 0) {
                printf("getrlimit error for: %s: %m\n", name);
                return;
        }
        printf("%-14s  ", name);
        if (limit.rlim_cur == RLIM_INFINITY)
                printf("(infinite)  ");
        else
                printf(FMT, limit.rlim_cur);
        if (limit.rlim_max == RLIM_INFINITY)
                printf("(infinite)  ");
        else
                printf(FMT, limit.rlim_max);
        printf("\n");
}
注意:在 doit 宏中使用了 ISO C 的字符串创建运算符(#),以便为每个资源名产生字符串值。
例如:
    doit(RLIMIT_CORE);
这将由 C 预处理程序扩展为:
pr_limits("RLIMIT_CORE", RLIMIT_CORE);
a)执行结果:Linux 2.6.21-1.3194.fc7
RLIMIT_AS             (infinite)  (infinite)  
RLIMIT_CORE        0        (infinite)  
RLIMIT_CPU           (infinite)  (infinite)  
RLIMIT_DATA         (infinite)  (infinite)  
RLIMIT_FSIZE       (infinite)  (infinite)  
RLIMIT_LOCKS    (infinite)  (infinite)  
RLIMIT_MEMLOCK          32768            32768  
RLIMIT_NOFILE             1024              1024  
RLIMIT_NPROC               4096              4096  
RLIMIT_RSS           (infinite)  (infinite)  
RLIMIT_STACK        10485760    (infinite)
b)执行 ulimit -a 命令的结果:
[root@bogon myinclude]# ulimit -a
core file size                   (blocks, -c)  0
data seg size                     (kbytes, -d)  unlimited
scheduling priority                 (-e)          0
file size                            (blocks, -f)  unlimited
pending signals                         (-i)          4096
max locked memory            (kbytes, -l)  32
max memory size         (kbytes, -m)  unlimited
open files                                  (-n)          1024
pipe size                          (512 bytes, -p) 8
POSIX message queues        (bytes, -q)   819200
real-time priority                   (-r)          0
stack size                           (kbytes, -s)  10240
cpu time                              (seconds, -t) unlimited
max user processes                   (-u)          4096
virtual memory                   (kbytes, -v)  unlimited
file locks                                  (-x)          unlimited
               
               
               
               
               
               
               
               
               
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/105349/showart_2119720.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP