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)任何一个进程都可以将一个软限制值更改为 <= 其硬限制值。
2)任何一个进程都可降低其硬限制值,但它必须 >= 其软限制值。这种降低对普通用户而言是不可逆的。
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 和 cd
也必须是 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 |
阅读(2350) | 评论(0) | 转发(0) |