读书人

小弟我使用过的Linux命令之ulimit - 在

发布时间: 2012-07-02 17:46:22 作者: rapoo

我使用过的Linux命令之ulimit - 在shell进程中限制系统资源
我使用过的Linux命令之ulimit - 在shell进程中限制系统资源

本文链接:http://codingstandards.iteye.com/blog/967718 ? (转载请注明出处)

用途说明

ulimit是一个shell内建命令,用于控制由shell启动的进程的可用资源(Provides? control? over the resources available to the shell and to processes started by it, on systems that allow such? control.)。对资源的限制分为两种,一种是硬性限制,一种是软性限制。硬性限制一旦设定就不能增加(A hard limit cannot be? increased? once it? is set),而软性限制可以增加到硬性控制为止(a soft limit may be increased up to the value of the hard limit)。Linux系统可以对多种资源的使用进行限制,比如允许创建的文件数、允许打开的文件数、是否允许生成core等。要注意的是,该设置只对当前shell进程的子进程产生作用,并不会影响其他shell进程。

最初接触到这个命令,是在从事C/C++开发时经常会出现段错误、断言错误之类的,但是Linux操作系统中默认是不生成core文件的,所以无法确切的知道问题出现的位置。因此,通常的做法就是修改/etc/profile中的那个ulimit设置,如下所示:

#??????? - "hard" for enforcing hard limits
#
#<item> can be one of the following:
#??????? - core - limits the core file size (KB)
#??????? - data - max data size (KB)
#??????? - fsize - maximum filesize (KB)
#??????? - memlock - max locked-in-memory address space (KB)
#??????? - nofile - max number of open files
#??????? - rss - max resident set size (KB)
#??????? - stack - max stack size (KB)
#??????? - cpu - max CPU time (MIN)
#??????? - nproc - max number of processes
#??????? - as - address space limit
#??????? - maxlogins - max number of logins for this user
#??????? - priority - the priority to run user process with
#??????? - locks - max number of file locks the user can hold
#
#<domain>????? <type>? <item>???????? <value>
#

#*?????????????? soft??? core??????????? 0
#*?????????????? hard??? rss???????????? 10000
#@student??????? hard??? nproc?????????? 20
#@faculty??????? soft??? nproc?????????? 20
#@faculty??????? hard??? nproc?????????? 50
#ftp???????????? hard??? nproc?????????? 0
#@student??????? -?????? maxlogins?????? 4

# End of file
[root@node34 root]#

?

示例二 打印当前的资源限制

[root@node34 root]# ulimit -H -a
core file size??????? (blocks, -c) unlimited
data seg size???????? (kbytes, -d) unlimited
file size???????????? (blocks, -f) unlimited
max locked memory???? (kbytes, -l) 4
max memory size?????? (kbytes, -m) unlimited
open files??????????????????? (-n) 1024
pipe size????????? (512 bytes, -p) 8
stack size??????????? (kbytes, -s) unlimited
cpu time???????????? (seconds, -t) unlimited
max user processes??????????? (-u) 2048
virtual memory??????? (kbytes, -v) unlimited
[root@node34 root]# ulimit -S -a
core file size??????? (blocks, -c) 0
data seg size???????? (kbytes, -d) unlimited
file size???????????? (blocks, -f) unlimited
max locked memory???? (kbytes, -l) 4
max memory size?????? (kbytes, -m) unlimited
open files??????????????????? (-n) 1024
pipe size????????? (512 bytes, -p) 8
stack size??????????? (kbytes, -s) 10240
cpu time???????????? (seconds, -t) unlimited
max user processes??????????? (-u) 2048
virtual memory??????? (kbytes, -v) unlimited
[root@node34 root]#

?

示例三 修改允许打开的文件数

[root@node34 root]# ulimit -n
1024
[root@node34 root]# ulimit -n 4096
[root@node34 root]# ulimit -n
4096
[root@node34 root]#

要永久修改,请参见后面的资料。

?

问题思考相关资料

【1】developerWorks 中国?? 通过 ulimit 改善系统性能
http://www.ibm.com/developerworks/cn/linux/l-cn-ulimit/
【2】有意没思的专栏 linux修改ulimit应该注意的
http://blog.csdn.net/noizz/archive/2008/03/13/2177503.aspx
【3】走在路上的你。。。? 查看linux blocksize 大小
http://blogold.chinaunix.net/u/11765/showart_235505.html
【4】好好学习,天天向上 解决linux打开文件数1024限制的解决办法
http://www.51testing.com/?uid-13956-action-viewspace-itemid-209988
【5】我家空间? Linux 用户进程可打开文件数 TCP连接数 限制修改
http://www.myhomespace.net/p/2010/11/528

?

返回 我使用过的Linux命令系列总目录

?

读书人网 >UNIXLINUX

热点推荐