- 浏览: 208517 次
- 性别:
- 来自: 重庆
-
文章分类
最新评论
每一个进程都有自己的一组资源限制,在(*)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
EMFILE.
每个进程能够打开的最多文件数。更改此限制将影响到sysconf函数在参数_SC_CHILD_MAX中的返回值
RLIMIT_OFILE is the BSD name for RLIMIT_NOFILE.
这里BSD系统中RLIMIT_NOFILE的别名
RLIMIT_NPROC
The maximum number of processes that can be created for the
real user ID of the calling process. Upon encountering this
limit, fork() fails with the error EAGAIN.
每个实际用户ID所拥有的最大子进程数,更改此限制将影响到sysconf函数在参数_SC_CHILD_MAX中返回的值
RLIMIT_RSS
Specifies the limit (in pages) of the process鈙 resident set
(the number of virtual pages resident in RAM). This limit only
has effect in Linux 2.4 onwatrds, and there only affects calls
to madvise() specifying MADVISE_WILLNEED.
最大驻内存集字节长度(RSS)如果物理存储器供不应求则内核将从进程处取回超过RSS的部份
RLIMIT_STACK
The maximum size of the process stack, in bytes. Upon reaching
this limit, a SIGSEGV signal is generated. To handle this sig-
nal, a process must employ an alternate signal stack (sigalt-
stack(2)).
栈的最大长度
RLIMIT——VMEM 可映照地址空间的最大字节长茺,这影响到mmap函数
这些限制影响到调用进程并由子进程继承! 可以在SHELL中预设这些值ulimit命令设置
小试牛刀(代码只是演示,并没有作错误处理)
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/resource.h>
int main(void)
{
struct rlimit r;
if(getrlimit(RLIMIT_NOFILE,&r)<0)
{
fprintf(stderr,"getrlimit error\n");
exit(1);
}
printf("RLIMIT_NOFILE cur:%d\n",r.rlim_cur);
printf("RLIMIT_NOFILE max:%d\n",r.rlim_max);
/** set limit **/
r.rlim_cur=100;
r.rlim_max=200;
if (setrlimit(RLIMIT_NOFILE,&r)<0)
{
fprintf(stderr,"setrlimit error\n");
exit(1);
}
/** get value of set **/
if(getrlimit(RLIMIT_NOFILE,&r)<0)
{
fprintf(stderr,"getrlimit error\n");
exit(1);
}
printf("RLIMIT_NOFILE cur:%d\n",r.rlim_cur);
printf("RLIMIT_NOFILE max:%d\n",r.rlim_max);
return 0;
}
:!gcc test.c
:!./a.out
RLIMIT_NOFILE cur:1024
RLIMIT_NOFILE max:1024
RLIMIT_NOFILE cur:100
RLIMIT_NOFILE max:200
发表评论
-
线程属性pthread_attr_t简介
2013-01-05 10:57 3930本文编辑整理自: http://hi.baidu.c ... -
避免内存碎片
2012-12-13 10:49 1495许多书籍提到过内存碎片,也看到一些方法防治内存 ... -
Linux程序设计中由线程使用不当引起的内存泄漏
2012-12-13 10:27 1403Linux程序设计中,创建线程时调用pthread_ ... -
EPOLL ET 模式下事件触发的场景
2012-09-25 10:13 1880ET模式称为边缘触发模式,顾名思义,不到边缘情况,是死都 ... -
惊群问题的思考
2012-09-25 10:13 942“据说”惊群问题已经是一个很古老的问题了,并且在大多数系统中已 ... -
linux惊群问题之udp
2012-09-25 10:12 1608今天测试udp服务器进程时发现log中记录了 ... -
getaddrinfo()函数详解
2012-09-19 10:59 8811. 概述IPv4中使用gethostbyname()函数完成 ... -
Linux的mmap文件内存映射机制
2012-09-19 15:00 2348在讲述文件映 ... -
Berkeley DB 由浅入深【转自架构师杨建】
2012-09-19 15:00 1969在网上看到不少介绍Berkeley DB的文章,几乎 ... -
linux 多线程编程
2012-09-19 15:00 1522进程与线程 进程是程序执行时的一个实例 ... -
pthread_cond_wait()
2012-09-25 10:13 841/************pthread_cond_w ... -
linux下创建守护进程(daemon process)代码
2012-09-18 13:47 1655#include <stdio.h> ... -
dup and dup2的剖析
2012-09-18 13:35 672dup和dup2都可用来复制一个现存的文件描述符,使两个 ... -
UNIX缓冲机制
2012-09-18 11:11 1011某日一朋友写了一个HELL ...
相关推荐
6. **系统资源监控**:在C语言中,可以使用`sys/resource.h`头文件中的函数获取和设置资源限制,例如`getrlimit()`和`setrlimit()`。这些函数可以帮助我们了解系统的资源使用情况,如打开文件的数量、内存使用限制等...
本文将深入探讨rlimit的基本概念、常用资源类型、以及如何通过C语言中的`getrlimit`和`setrlimit`函数来查询和设置这些限制。 ### 基本概念 rlimit主要由两个部分组成:soft limit和hard limit。soft limit是当前...
Linux和UNIX系统编程涉及到许多关键的API,如`getrlimit()`和`setrlimit()`,用于获取和修改进程的资源限制。这些函数在"syslim"源码中可能会有详细的实现和示例。此外,源码可能还包括如何优雅地处理资源不足的情况...
- getrlimit/setrlimit:获取和设置资源限制。 以上是Linux系统调用的一些主要类别和常用系统调用的简要介绍。系统调用的设计和实现是操作系统最为核心的技术之一,是实现系统软件和高级编程的基础。本文档提及的...
10. **系统信息**:`sysinfo()`获取系统信息,`uname()`获取系统名,`getrlimit()`和`setrlimit()`管理资源限制。 掌握这些API是Linux系统编程的基础,而深入理解其工作原理和使用方法,可以帮助开发者编写出高效、...
**6.8 系统资源限制** - **资源限制:** 如何设置进程可以使用的资源上限。 **6.9 时间** - **时间测量:** 如何测量系统时间。 #### 七、UNIX进程控制 **7.1 进程控制概述** - **进程控制的重要意义:** 管理...
- **进程资源限制**:如`getrlimit`、`setrlimit`等函数用于获取和设置进程的资源限制。 - **文件描述符管理**:如`fcntl`函数用于文件描述符的管理。 - **进程优先级**:如`nice`函数用于调整进程的优先级。 #### ...
- **getrlimit/setrlimit函数**:获取/设置资源限制。 #### 第8章:进程控制 - **进程标识**:用于标识进程的信息。 - **fork函数**:创建新进程。 - **vfork函数**:轻量级进程创建。 - **exit函数**:终止进程。...