getrlimit()和setrlimit()系统调用可用于获取和设置与进程关联的资源限制,例如文件,CPU,内存等。
Each resource has an associated soft and hard limit.
- soft limit: The soft limit is the actual limit enforced by the kernel for the corresponding resource.
- hard limit: The hard limit acts as a ceiling for the soft limit.
The soft limit ranges in between 0 and hard limit.
这两个限制由以下结构定义
struct rlimit {
rlim_t rlim_cur; /* Soft limit */
rlim_t rlim_max; /* Hard limit (ceiling for rlim_cur) */
};
系统调用的签名是
int getrlimit(int resource, struct rlimit *rlim);
int setrlimit(int resource, const struct rlimit *rlim);
资源是指您要检索或修改的资源限制。
要设置两个限制,请将具有新值的值设置为rlimit结构的元素。
要获得两个限制,请传递rlim的地址。成功调用getrlimit()会将rlimit元素设置为limit。
成功时,两者都返回0 。如果出现错误,则返回-1 ,并正确设置errno。
Here, is a program demonstrating the system calls by changing the value one greater than the maximum file descriptor number to 3.
// C program to demonstrate working of getrlimit()
// and setlimit()
#include
#include
#include
#include
#include
#include
#include
#include
int main() {
struct rlimit old_lim, lim, new_lim;
// Get old limits
if( getrlimit(RLIMIT_NOFILE, &old_lim) == 0)
printf("Old limits -> soft limit= %ld \t"
" hard limit= %ld \n", old_lim.rlim_cur,
old_lim.rlim_max);
else
fprintf(stderr, "%s\n", strerror(errno));
// Set new value
lim.rlim_cur = 3;
lim.rlim_max = 1024;
// Set limits
if(setrlimit(RLIMIT_NOFILE, &lim) == -1)
fprintf(stderr, "%s\n", strerror(errno));
// Get new limits
if( getrlimit(RLIMIT_NOFILE, &new_lim) == 0)
printf("New limits -> soft limit= %ld "
"\t hard limit= %ld \n", new_lim.rlim_cur,
new_lim.rlim_max);
else
fprintf(stderr, "%s\n", strerror(errno));
return 0;
}
输出:
Old limits -> soft limit= 1048576 hard limit= 1048576
New limits -> soft limit= 3 hard limit= 1024
旧限制值可能会因系统而异。
Now, If you try to open a new file, it will show run time error, because maximum 3 files can be opened and that are already being opened by the system(STDIN, STDOUT, STDERR).
// C program to demonstrate error when a
// process tries to access resources beyond
// limit.
#include
#include
#include
#include
#include
#include
#include
#include
int main() {
struct rlimit old_lim, lim, new_lim;
// Get old limits
if( getrlimit(RLIMIT_NOFILE, &old_lim) == 0)
printf("Old limits -> soft limit= %ld \t"
" hard limit= %ld \n", old_lim.rlim_cur,
old_lim.rlim_max);
else
fprintf(stderr, "%s\n", strerror(errno));
// Set new value
lim.rlim_cur = 3;
lim.rlim_max = 1024;
// Set limits
if(setrlimit(RLIMIT_NOFILE, &lim) == -1)
fprintf(stderr, "%s\n", strerror(errno));
// Get new limits
if( getrlimit(RLIMIT_NOFILE, &new_lim) == 0)
printf("New limits -> soft limit= %ld \t"
" hard limit= %ld \n", new_lim.rlim_cur,
new_lim.rlim_max);
else
fprintf(stderr, "%s\n", strerror(errno));
// Try to open a new file
if(open("foo.txt", O_WRONLY | O_CREAT, 0) == -1)
fprintf(stderr, "%s\n", strerror(errno));
else
printf("Opened successfully\n");
return 0;
}
输出:
Old limits -> soft limit= 1048576 hard limit= 1048576
New limits -> soft limit= 3 hard limit= 1024
Too many open files
还有另一个将两个系统调用结合在一起的系统调用prlimit()。
有关更多详细信息,请键入以下内容以查看手册
man 2 prlimit
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。