注:本文档的实验设备是centos Linux7.2. 1511 (酷睿)版
1、ulimit概念
功能说明:控制外壳程序的资源。
语法: ulimit [-ahs ] [-c ] [-d ] [-f ] [-m ] [-n ] [-p ] [-t ] [-u ] [-v ]
补充说明: ulimit是一个内置于shell中的命令,可用于控制shell运行程序的资源。
2、ulimit通用参数
3、ulimita输出结果说明
4、常用操作
4.1、临时修改操作
注:使用ulimit命令更改的值仅在当前登录用户的当前使用环境下有效。 系统重新启动或用户退出时将禁用,ssh中新打开的窗口也将禁用。
4.1.1、显示系统资源设置
# ulimit -a
4.1.1、生成的核心转储文件大小没有限制
# ulimitc unlimited
4.1.1、限制最大dddds锁定内存大小
统一-统一
4.1.1、限制打开文件的最大数量
ulimitn unlimited
4.1.1、限制用户可用进程数
统一-统一
4.2、永久修改
在/etc/security/limits.conf文件中更改
[ root @ centos db~] # cat/etc/security/limits.conf
* soft nproc 16384
* hard nproc 65536
*软件文件2047
*硬件文件65536
*软件核心有限
*硬件核心有限
注: (1)需要重新启动系统,这在生产环境中是不可接受的。
)2)本人的测试结果表明,以上结果没有改变其他用户的max user processes
根帐户中显示的结果
centosdb数据库帐户中显示的结果
以上结果表明,centosdb的最大用户进程无效。
)3)从根帐户分析,
Open files显示为2047,该值是设置的软链接值,max user processes显示为65536,是硬链接值。 ulimita显示的结果是软链接的值还是硬链接的值? (根据网络资料,ulimit默认显示软链接。 这个要打上问号。 )
其他方法:
注意: (1)将ulimit修改命令放入/etc/profile后,internet上的所有用户都将变为有效。 经本人在centos72上测试,只有root用户有效,其他用户无效。 必须运行source /etc/profile命令才能启用其他用户
)2)将ulimit修改指令放入(/.bashrc ),只对当前用户有效,对其他用户无效。 要对其他用户启用,必须单独修改其他用户的(/.bashrc文件)。
总结:通过以上分析,可以看出每一个修改都有一点问题。 那么,如何进行永久修正呢? 这样,可以临时修改永久修改。 这样,在不重新启动系统的情况下资源限制就会生效,重新启动系统时也会生效。
4.3、案例—linux安装oracle修复资源限制
#cat /etc/security/limits.conf
oracle soft nproc 2047
oracle hard nproc 16384
oracle soft nofile 1024
oracle hard nofile 65536
注: npro表示maxnumberofprocesses
nofile表示max number of open files
5、c语言操作
在Linux命令行上查看帮助,man 3 ulimit
可以看出,描述性信息中有“This routine is obsolete”一词,表明这个程序已经过时了。 建议使用getrlimit和setrlimit
或sysconf代替;(注:bash下可以正常使用,只是c操作这个api过时)查看getrlimit与setrlimit帮助信息–man 2 setrlimit,
下面简单展示使用头文件及接口,具体可以看帮助信息
#include
#include
int getrlimit(int resource, struct rlimit *rlim);
int setrlimit(int resource, const struct rlimit *rlim);
int prlimit(pid_t pid, int resource, const struct rlimit *new_limit, struct rlimit *old_limit);
5.1、简单例子
以下操作表示一个进程能够最大创建文件的大小#include #include #include #include int main (int argc, char *argv[]){ struct rlimit limit; /* Set the file size resource limit. */ limit.rlim_cur = 65535; limit.rlim_max = 65535; if (setrlimit(RLIMIT_FSIZE, &limit) != 0) { printf("setrlimit() failed with errno=%dn", errno); exit(1); } /* Get the file size resource limit. */ if (getrlimit(RLIMIT_FSIZE, &limit) != 0) { printf("getrlimit() failed with errno=%dn", errno); exit(1); } printf("The soft limit is %llun", limit.rlim_cur); printf("The hard limit is %llun", limit.rlim_max); exit(0);}
注:RLIMIT_FSIZE :The maximum size of files that the process may create
5.2、例子修改core信息#include #include #include #include #define CORE_SIZE 8*1024int main(){ struct rlimit rlmt; if (getrlimit(RLIMIT_CORE, &rlmt) == -1) { return -1; } printf("Before set rlimit CORE dump current is:%d, max is:%dn", (int)rlmt.rlim_cur, (int)rlmt.rlim_max); rlmt.rlim_cur = (rlim_t)CORE_SIZE; rlmt.rlim_max = (rlim_t)CORE_SIZE; if (setrlimit(RLIMIT_CORE, &rlmt) == -1) { return -1; } if (getrlimit(RLIMIT_CORE, &rlmt) == -1) { return -1; } printf("After set rlimit CORE dump current is:%d, max is:%dn", (int)rlmt.rlim_cur, (int)rlmt.rlim_max);/*测试非法内存,产生core文件*/ int *ptr = NULL; *ptr = 10; return 0;}
5.3、Prlimit例子#define _GNU_SOURCE#define _FILE_OFFSET_BITS 64#include #include #include #include #include #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0) int main(int argc, char *argv[]) { struct rlimit old, new; struct rlimit *newp; pid_t pid; if (!(argc == 2 || argc == 4)) { fprintf(stderr, "Usage: %s [ " "]n", argv[0]); exit(EXIT_FAILURE); }pid = atoi(argv[1]); /* PID of target process */ newp = NULL; if (argc == 4) { new.rlim_cur = atoi(argv[2]); new.rlim_max = atoi(argv[3]); newp = &new; } /* Set CPU time limit of target process; retrieve and display * previous limit */ if (prlimit(pid, RLIMIT_CPU, newp, &old) == -1) errExit("prlimit-1"); printf("Previous limits: soft=%lld; hard=%lldn", (long long) old.rlim_cur, (long long) old.rlim_max); /* Retrieve and display new CPU time limit */if (prlimit(pid, RLIMIT_CPU, NULL, &old) == -1) errExit("prlimit-2"); printf("New limits: soft=%lld; hard=%lldn", (long long) old.rlim_cur, (long long) old.rlim_max); exit(EXIT_FAILURE);}