Linux零碎記錄之ulimit【堆棧大小、stack size、進程數限制、文件句柄限制、linux用戶空間限制】


寫了個小程序 本來打算寫個hash表的,但是出現“段錯誤”

#include<stdio.h
struct a{
char a[4096];
char a1[4096];
char a2[4096];
char a3[4096];
};
int main(){
struct a b[1500];
int i=0;
for(;i<1500;i++)
memset(b.a,oxff,4096);
sleep(2000);
}

我就在納悶,不就是分配點空間,怎么就不行了?

后來知道,原來這是stack size 堆大小 達到最大后,就錯誤了。

在Linux下,這個其實是可以設置的。

使用 ulimit命令即可設置,不但可以設置這個,還可以設置其他很多限制,使用ulimit -a可以查看當前的設置。

ayanmw@ayanmw-desktop:~/server_epoll$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 20
file size (blocks, -f) unlimited
pending signals (-i) 16382
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 4096
cpu time (seconds, -t) unlimited
max user processes (-u) unlimited
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

 

看到stack size居然是8192KB。

程序一般是分為 堆 和 棧,堆 是存放變量名稱的地方,比如指針一個指針 本身占用4個字節,指向一個32位地址(64位系統加倍),一般的變量比如int a,這里a的名稱也是一個變量,指向存有值的a的空間地址。

而棧就是放數據的地方,只要內存還有,就可以一直分配,不信你可以使用 一個指針,然后malloc(599999)的大小,進程占用內存就有幾十MB了。

可以看到,linux下還可以設置文件大小,打開的文件的數量限制。等等。

============================================

以前發現普通用戶無法修改ulimit,root用戶 可以.

百度搜索得到:

/etc/security/limits.conf

添加

*               soft  nofile  8192
*               hard    nofile  8192

可以修改.

CentOS 或 RedHat這個文件的路徑貌似是:

[oracle@qs-xezf-db2 ~]$ cat /etc/sysconfig/limits.conf

oracle              soft    nproc   2047

oracle              hard    nproc   16384

oracle              soft    nofile  1024

oracle              hard    nofile  65536

修改文件應該就可以達到效果了.

有人說:在/root/.bashrc中增加:
ulimit -u unlimited
ulimit -n 10240
不一定管用.- -

sysctl -a 可以查看更多系統配置信息.

 

 

1、cat /proc/sys/fs/file-max,可以獲得整個系統的文件句柄數目。一般是8192。如果希望增加句柄的總數,可以在腳本/etc/rc.d/rc..local中添加一行:echo 10240 >; /proc/sys/fs/file-max  (舉例)

不解:使用lsof -f|wc -l 可以看到當前系統的打開文件總數,我的一台郵件服務器在最瘋狂的時候曾輸出值為4萬!!!就是說同時打開了(或未完全關閉)4萬個文件鏈接。而file-max值為8192,為什么??

2、通過設置/etc/security/limits.conf,實現控制每個進程的文件句柄數目。

添加兩行:   *      soft     nofile    1024
                  *      hard    nofile    8192

確認/etc/pam.d/system-auth文件中有下面一行:session required /lib/security/pam_limits.so

如果說每個進程能打開的文件句柄數目限制為8192,那該進程產生的線程所打開的文件句柄數目是限在該8192內?還是又一個8192呢?

 

這個問題,最后采取了一個比較簡單的方法:
在/boot/.bashrc中加入:
ulimit -u unlimited
ulimit -n 20480

然后所有普通用戶的open files都變為20480了,用戶最大進程數變為ulimited了。

 

我看到有的地方說AS3要該進程數只能在
1、設置fd_set支持的最大數量
 a、修改/usr/include/bits/typesizes.h 
  #define __FD_SETSIZE 1024 --->; 65536 
 b、修改/usr/src/linux/include/linux/posix_types.h  
  #define __FD_SETSIZE 1024 --->; 65536 

2、修改/usr/src/linux/include/linux/fs.h 
  設置最大打開文件數量(TCP連接數量) 
  #define INR_OPEN 1024 --->; 65536 
  #define NR_FILE 8192 --->; 65536 
  #define NR_RESERVED_FILES 10 --->; 128 
3、修改/usr/src/linux/include/net/tcp.h 
  a、設置TIMEOUT的時間為1秒 
  #define TCP_TIMEWAIT_LEN (60*HZ) 60 --->; 1*HZ 
  b、設置在backlog隊列里的半連接的重試次數,每次都會花相應的時間,本質上也是減少重試時間 
  #define TCP_SYNACK_RETRIES 5 --->; 3 

然后通過編譯內核的方式來增加, 這樣比ulimit 更直接修改內核參數.因為內核 是編譯出來的.

 

壓力測試:

yuan@yuan-desktop:~$ ab -n 30000 -c 5000 http://localhost/
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost [through 218.19.166.24:3129] (be patient)
socket: Too many open files (24)
提示出錯 上網查到是socket系統默認為1024 大於該直就報錯 修改.

 

參考:http://www.chinaunix.net/jh/4/563088.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM