<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>https://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>https://jackxiang.com/post//</link>
<title><![CDATA[Linux Shell命令ulimit的用法]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Wed, 27 Jan 2010 05:03:48 +0000</pubDate> 
<guid>https://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	vi /etc/security/limits.conf<br/>[codes=php]<br/><br/># End of file <br/>* soft nofile 65535<br/>* hard nofile 65535<br/>*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;soft&nbsp;&nbsp;&nbsp;&nbsp;nofile&nbsp;&nbsp;65535<br/>*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hard&nbsp;&nbsp;&nbsp;&nbsp;nofile&nbsp;&nbsp;65535<br/>[/codes]<br/><br/>1,说明:<br/>ulimit用于shell启动进程所占用的资源.<br/>2,类别:<br/>shell内建命令<br/>3,语法格式:<br/>ulimit [-acdfHlmnpsStvw] [size]<br/>4,参数介绍:<br/>-H 设置硬件资源限制.<br/>-S 设置软件资源限制.<br/>-a 显示当前所有的资源限制.<br/>-c size:设置core文件的最大值.单位:blocks<br/>-d size:设置数据段的最大值.单位:kbytes<br/>-f size:设置创建文件的最大值.单位:blocks<br/>-l size:设置在内存中锁定进程的最大值.单位:kbytes<br/>-m size:设置可以使用的常驻内存的最大值.单位:kbytes<br/>-n size:设置内核可以同时打开的文件描述符的最大值.单位:n<br/>-p size:设置管道缓冲区的最大值.单位:kbytes<br/>-s size:设置堆栈的最大值.单位:kbytes<br/>-t size:设置CPU使用时间的最大上限.单位:seconds<br/>-v size:设置虚拟内存的最大值.单位:kbytes<br/>5.举例<br/>在Linux下写程序的时候，如果程序比较大，经常会遇到“段错误”（segmentation fault）这样的问题，这主要就是由于Linux系统初始的堆栈大小（stack size）太小的缘故，一般为10M。我一般把stack size设置成256M，这样就没有段错误了！命令为：<br/>ulimit&nbsp;&nbsp; -s 262140<br/>如果要系统自动记住这个配置，就编辑/etc/profile文件，在 “ulimit -S -c 0 &gt; /dev/null 2&gt;&amp;1”行下，添加“ulimit&nbsp;&nbsp; -s 262140”，保存重启系统就可以了！<br/><br/><br/><br/>ulimit -a 用来显示当前的各种用户进程限制。 <br/>&nbsp;&nbsp;&nbsp;&nbsp;Linux对于每个用户，系统限制其最大进程数。为提高性能，可以根据设备资源情况，<br/>&nbsp;&nbsp;&nbsp;&nbsp;设置各linux 用户的最大进程数，下面我把某linux用户的最大进程数设为10000个： <br/>&nbsp;&nbsp;&nbsp;&nbsp; ulimit -u 10000 <br/>&nbsp;&nbsp;&nbsp;&nbsp; 对于需要做许多 socket 连接并使它们处于打开状态的 Java 应用程序而言，<br/>&nbsp;&nbsp;&nbsp;&nbsp; 最好通过使用 ulimit -n xx 修改每个进程可打开的文件数，缺省值是 1024。 <br/>&nbsp;&nbsp;&nbsp;&nbsp; ulimit -n 4096 将每个进程可以打开的文件数目加大到4096，缺省为1024 <br/>&nbsp;&nbsp;&nbsp;&nbsp; 其他建议设置成无限制（unlimited）的一些重要设置是： <br/>&nbsp;&nbsp;&nbsp;&nbsp; 数据段长度：ulimit -d unlimited <br/>&nbsp;&nbsp;&nbsp;&nbsp; 最大内存大小：ulimit -m unlimited <br/>&nbsp;&nbsp;&nbsp;&nbsp; 堆栈大小：ulimit -s unlimited <br/>&nbsp;&nbsp;&nbsp;&nbsp; CPU 时间：ulimit -t unlimited <br/>&nbsp;&nbsp;&nbsp;&nbsp; 虚拟内存：ulimit -v unlimited <br/>　　<br/>&nbsp;&nbsp;&nbsp;&nbsp; 暂时地，适用于通过 ulimit 命令登录 shell 会话期间。<br/>&nbsp;&nbsp;&nbsp;&nbsp; 永久地，通过将一个相应的 ulimit 语句添加到由登录 shell 读取的文件中， 即特定于 shell 的用户资源文件，如： <br/> <br/>1)、解除 Linux 系统的最大进程数和最大文件打开数限制：<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vi /etc/security/limits.conf<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# 添加如下的行<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* soft noproc 11000<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* hard noproc 11000<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* soft nofile 4100<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* hard nofile 4100 <br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 说明：* 代表针对所有用户<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;noproc 是代表最大进程数<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nofile 是代表最大文件打开数 <br/>2)、让 SSH 接受 Login 程式的登入，方便在 ssh 客户端查看 ulimit -a 资源限制：<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a、vi /etc/ssh/sshd_config <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 把 UserLogin 的值改为 yes，并把 # 注释去掉<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b、重启 sshd 服务：<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/etc/init.d/sshd restart<br/>3)、修改所有 linux 用户的环境变量文件：<br/>vi /etc/profile <br/>ulimit -u 10000<br/>ulimit -n 4096<br/>ulimit -d unlimited <br/>ulimit -m unlimited <br/>ulimit -s unlimited <br/>ulimit -t unlimited <br/>ulimit -v unlimited <br/> <br/> <br/> <br/>/**************************************<br/> <br/>有时候在程序里面需要打开多个文件，进行分析，系统一般默认数量是1024，（用ulimit -a可以看到）对于正常使用是够了，但是对于程序来讲，就太少了。<br/><br/>修改2个文件。<br/>1./etc/security/limits.conf<br/>vi /etc/security/limits.conf<br/>加上：<br/>* soft nofile 8192<br/>* hard nofile 20480<br/>2./etc/pam.d/login<br/>session required /lib/security/pam_limits.so<br/>**********<br/>另外确保/etc/pam.d/system-auth文件有下面内容<br/>session required /lib/security/$ISA/pam_limits.so<br/>这一行确保系统会执行这个限制。<br/>***********<br/>3.一般用户的.bash_profile<br/>#ulimit -n 1024<br/>重新登陆ok<br/> <br/> <br/>-------------<br/>对于solaris<br/> <br/>其实在系统里面有这样一个命令ulimit，以下是ulimit -a执行的结果：<br/><br/>time(seconds) unlimitedfile(blocks) unlimiteddata(kbytes) unlimitedstack(kbytes) 8192coredump(blocks) unlimitednofiles(descriptors) 1024memory(kbytes) unlimited<br/>其中nofiles就是文件描述符的变量值，该值受rlim_fd_cur这个参数的影响，可以用ulimit -n number命令来修改。但不管怎么改,程序仍然不能突破fd=256的限制。在Solaris Tunable Parameters Reference Manua这本书里面能查到以下的资料：<br/><br/>A 32-bit program using standard I/O is limited to 256 file descriptors。A 64-bit program using standard I/O can use up to 2 billion descriptors。<br/>这也就是说32位的程序是没有办法突破这个限制的，只有64位的程序才能使用高达2亿个文件描述符，SUN的软硬件在很早以前就实现了64位的架构，现在唯一要解决的就是将程序编译成64位程序，为了生成64位程序,就必须要有64位的编译器（其实不是这样的）,如果你去www.sunfreeware.com下载64位编译器gcc，网站上没有特别注明是64位的gcc，但是会有个意外的收获，就是该软件的说明里面注明了只要在用gcc编译的时候加上-m64的option就能生成64位程序了。<br/><br/>于是用gcc -m64去编译生成一个64位程序后，用ulimit -n 102400将number of fd设成很大的情况下，所有问题迎刃而解，再也不存在文件描述符不够用的情况。<br/><br/>在/etc/system文件设置rlimi_fc_max和rlim_fd_cur格式如下：<br/><br/> <br/>* set hard limit on file descriptorsset rlim_fd_max = 4096* set soft limit on file descriptorsset rlim_fd_cur = 1024<br/>命令ulimit使用格式如下：<br/><br/>usage: ulimit [ -HSacdfnstv ] [ limit ]ulimit -a是显示各参数的设置值，ulimit -n是用来设置fd的最大值的。<br/>*************************************************<br/>如何修改文件描述符限制？<br/>Solaris有两个参数控制进程可打开的文件描述符：rlim_fd_max，rlim_fd_cur。前者修改是个硬设置，修改需要权限，后者 是个软设置，用户可以limit或者setrlimit() 修改，该值最大不能超过前者。一般我们在/etc/system里修改这两个参数 <br/><br/>set rlim_fd_max = 65535 <br/><br/>set rlim_fd_cur = 65535 <br/><br/> <br/><br/> <br/><br/>A: You have two ways to modify the limit number of files that a process<br/>can open simutanously.<br/>One: modify the /etc/system file add the following entry:<br/>set rlim_fd_cur = #n<br/>#n is the number you want. Should be no more than 1024.<br/>You should reboot the machine.<br/>Two: Use the system command: ulimit<br/>$ulimit -n #n<br/>Note: You should use B-shell.<br/>And using the same terminal session(in the same terminal<br/>window) to run the your application program( to guarantee your<br/>application process is a child process of the setting<br/>terminal.)You can man ulimit to see the detailed usaged.<br/>The disadvantage brought by incread the file limite for a<br/>process or the whole system is increasing the system memory<br/>usage. But, for today&#92;&#039;s machine, this disadvantage is not too<br/>expensive. (William said:) There is no limit for max open<br/>socket number in Java. But the operating system has a limit for<br/>max open file descriptors.A socket resource is treated as a<br/>file descriptor in Unix. The previous email answered your<br/>question. You can try as said.<br/><br/>
]]>
</description>
</item><item>
<link>https://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] Linux Shell命令ulimit的用法]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>