每个进程都有一组资源的限制,通过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_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 (Section 14.8). |
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_MEMLOCK | The maximum amount of memory in bytes that a process can lock into memory using mlock(2). |
RLIMIT_MSGQUEUE | The maximum amount of memory in bytes that a process can allocate for POSIX message queues. |
RLIMIT_NICE | The limit to which a process’s nice value (Section 8.16) can be raised to affect its scheduling priority. |
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.17 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_SIGPENDING | The maximum number of signals that can be queued for a process. This limit is enforced by the sigqueue function (Section 10.20). |
RLIMIT_STACK | The maximum size in bytes of the stack. See Figure 7.6. |
shell
有limit command
:set or report file size limit
Example:
打印出系统当前的soft limit
和hard 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);