分类: LINUX
2008-12-04 11:01:46
限制有物理限制(比如内存),系统策略(比如CPU时间),实现限制(比如整型的大小,文件名允许的字符个数等)。UNIX specification定义的限制见第7章。
limits.h中定义了一些常量。
Limit Constant
Purpose
NAME_MAX The maximum
number of characters in a filename
CHAR_BIT The number
of bits in a char value
CHAR_MAX The maximum
char value
INT_MAX The maximum int
value
NAME_MAX是针对文件系统的,为了移植性,推荐使用pathconf。
#include
int getpriority(int which, id_t who);
int setpriority(int which, id_t who, int priority);
int getrlimit(int resource, struct rlimit *r_limit);
int setrlimit(int resource, const struct rlimit
*r_limit);
int getrusage(int
who, struct rusage *r_usage);
id_t为整型,用于用户和组的识别。Rusage的结构如下:
rusage Member Description
struct timeval ru_utime The user time used
struct timeval ru_stime The system time used
timeval结构在sys/time.h中定义,代表tv_sec and tv_usec,分别代表秒和微秒。Getrusage记录CPU消耗的信息。
结构如下:
who Constant Description
RUSAGE_SELF Returns usage information about current program only.
RUSAGE_CHILDREN Includes usage information of child processes as well.
子进程和任务的介绍详见11章。普通用户可以降低程序的优先级,但是不可提高。相关函数:getpriority and setpriority。可以根据进程号,gid,uid等修改优先级。
which Parameter
Description
PRIO_PROCESS who is a
process identifier.
PRIO_PGRP who is a
process group.
PRIO_USER who is a
user identifier.
比如:priority = getpriority(PRIO_PROCESS, getpid());
优先级一般为-20 to +20,默认为0.
系统资源限制相关函数:getrlimit and setrlimit,使用sys/resource.h中定义的rlimit,
rlimit Member Description
rlim_t rlim_cur The current, soft limit
rlim_t rlim_max The hard limit
rlim_t是用来描述资源级别的整型。soft limit一般是建议值,不宜超过,否则可能导致库函数出错。hard limit越值可能导致系统中止程序。比如CPU时间到发送SIGXCPU,数据超过大小发送SIGSEGV。超级用户才可以增加hard limit。
更多的定义:sys/resource.h
resource Parameter Description
RLIMIT_CORE The core dump file size limit, in bytes
RLIMIT_CPU The CPU time limit, in seconds
RLIMIT_DATA The data () segment limit, in bytes
RLIMIT_FSIZE The file size limit, in bytes
RLIMIT_NOFILE The limit on the number of open files
RLIMIT_STACK The limit on stack size, in bytes
RLIMIT_AS The limit on address space (stack and data), in bytes
实例:
# cat limits.c
// 1 Include the header files for all the functions we're going to be using in this program.
#include
#include
#include
#include
#include
#include
#include
// 2 The void function writes a string to a temporary file 10000 times and then performs some arithmetic
// to generate load on the CPU.
void work()
{
FILE *f;
int i;
double x = 4.5;
f = tmpfile();
for(i = 0; i < 10000; i++) {
fprintf(f,"Do some output\n");
if(ferror(f)) {
fprintf(stderr,"Error writing to temporary file\n");
exit(1);
}
}
for(i = 0; i < 1000000; i++)
x = log(x*x + 3.21);
}
// 3 The main function calls work and then uses the getrusage function to discover how much CPU time it has used. It displays this information on screen.
int main()
{
struct rusage r_usage;
struct rlimit r_limit;
int priority;
work();
getrusage(RUSAGE_SELF, &r_usage);
printf("CPU usage: User = %ld.%06ld, System = %ld.%06ld\n",
r_usage.ru_utime.tv_sec, r_usage.ru_utime.tv_usec,
r_usage.ru_stime.tv_sec, r_usage.ru_stime.tv_usec);
// 4 Next, it calls getpriority and getrlimit to find out its current priority and file size limits respectively.
priority = getpriority(PRIO_PROCESS, getpid());
printf("Current priority = %d\n", priority);
getrlimit(RLIMIT_FSIZE, &r_limit);
printf("Current FSIZE limit: soft = %ld, hard = %ld\n",
r_limit.rlim_cur, r_limit.rlim_max);
// 5 Finally, we set a file size limit using setrlimit and call work again, which fails because
// it attempts to create too large a file.
r_limit.rlim_cur = 2048;
r_limit.rlim_max = 4096;
printf("Setting a 2K file size limit\n");
setrlimit(RLIMIT_FSIZE, &r_limit);
work();
exit(0);
}
注意编译的方式:
# gcc -o limits limits.c
/tmp/ccCUQIRi.o(.text+0xa2): In function `work':
: undefined reference to `log'
collect2: ld returned 1 exit status
[root@localhost chapter04]# gcc -o limits limits.c -lm
[root@localhost chapter04]#
运行结果:
# ./limits
CPU usage: User = 0.080987, System = 0.000000
Current priority = 0
Current FSIZE limit: soft = -1, hard = -1
Setting a 2K file size limit
File size limit exceeded
资源的限制也可以通过ulimit来设置。