getrlimit、setrlimit

每个进程都有一组资源的限制,通过getrlimit、setrlimit能查寻和改变资源限制。

getrlimit, setrlimit, prlimit - get/set resource limits

#include <sys/time.h>
#include <sys/resource.h>

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

//Return: 0 if OK, nonzero on error

系统资源限制是系统启动时由进程0设置的,且由后续的进程继承。
其中特殊的结构rlimit如下:

struct rlimit {
               rlim_t rlim_cur;  /* Soft limit */
               rlim_t rlim_max;  /* Hard limit (ceiling for rlim_cur) */
};

改变资源限制的三个准则

  • 进程的软限制(soft limit)需要小于等于硬限制(hard limit)
  • 进程可以降低其硬限制,以大于等于soft limit。注意:降低硬限制对一般用户不可逆!
  • 仅仅超级用户进程(superuser process)可以提高硬限制

常量RLIM_INFINITY表示无限的限制
resource使用如下图的参数(Linux 3.2.0):

Resource
RLIMIT_ASThe maximum size in bytes of a process’s total available memory. This affects the sbrk function (Section 1.11) and the mmap function (Section 14.8).
RLIMIT_COREThe maximum size in bytes of a core file. A limit of 0 prevents the creation of a core file.
RLIMIT_CPUThe maximum amount of CPU time in seconds. When the soft limit is exceeded, the SIGXCPU signal is sent to the process.
RLIMIT_DATAThe maximum size in bytes of the data segment: the sum of the initialized data, uninitialized data, and heap from Figure 7.6.
RLIMIT_FSIZEThe 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_MEMLOCKThe maximum amount of memory in bytes that a process can lock into memory using mlock(2).
RLIMIT_MSGQUEUEThe maximum amount of memory in bytes that a process can allocate for POSIX message queues.
RLIMIT_NICEThe limit to which a process’s nice value (Section 8.16) can be raised to affect its scheduling priority.
RLIMIT_NOFILEThe 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.17 also.
RLIMIT_NPROCThe 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_RSSMaximum resident set size (RSS) in bytes. If available physical memory is low, the kernel takes memory from processes that exceed their RSS.
RLIMIT_SIGPENDINGThe maximum number of signals that can be queued for a process. This limit is enforced by the sigqueue function (Section 10.20).
RLIMIT_STACKThe maximum size in bytes of the stack. See Figure 7.6.
  • shelllimit command:set or report file size limit

Example:

打印出系统当前的soft limithard limit

#include <sys/resource.h>
#define doit(name) pr_limit(#name, name)
static void pr_time(char *name, int resource)
{
    struct rlimit limit;
    if(getrlimit(resource, &limit) < 0)//获取资源
        出错处理;
    printf("%s ", name);
    if(limit.rlim_cur == RLIM_INFINITY)//查看当前的soft limit
        printf("(infinity)");
    else
        printf("%d\n", limit.rlim_cur);
    if(limit.rlim_max == RLIM_INFINITY)//查看硬限制hard limit
        printf("(infinity)");
    else
        printf("%d\n", limit.rlim_max);
}

使用doit(RLIMIT_CORE);
等效于pr_limit("RLIMIT_CORE", RLIMIT_CORE);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猎羽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值