測(cè)試ulimit的打開(kāi)文件數(shù)量控制

ulimit使用

ulimit -SHn
ulimit -a
[root@node04 test]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 4823
max locked memory       (kbytes, -l) unlimited
max memory size         (kbytes, -m) unlimited
open files                      (-n) 4
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 65536
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
  • SHn代表著軟硬限制兴想, 軟限制就是超過(guò)就提醒并不真正限制了只酥,硬限制就是強(qiáng)制在內(nèi)荧降,超過(guò)報(bào)錯(cuò)妈倔。
  • a代表顯示所有參數(shù)
  • 只有寫(xiě)到/etc/security/limits.conf文件里邊才會(huì)永久生效。
  • 使用ulimit只是臨時(shí)的伪煤,當(dāng)前用戶(hù)退出登錄即解除瘩绒。

生成100個(gè)空文件

touch file{1..n}

編寫(xiě)打開(kāi)文件的C語(yǔ)言代碼

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>

#define FLAGS O_WRONLY | O_CREAT | O_TRUNC   //只寫(xiě),文件不存在那么就創(chuàng)建带族,文件長(zhǎng)度戳為0  
#define MODE S_IRWXU | S_IXGRP | S_IROTH | S_IXOTH //創(chuàng)建文件的權(quán)限,用戶(hù)讀蟀给、寫(xiě)蝙砌、執(zhí)行、組讀跋理、執(zhí)行择克、其他用戶(hù)讀、執(zhí)行  
int main(int argc, char *argv[]){

  const char* pathname;  
  int fd;//文件描述符  
  char pn[100]="/root/test/file";  //定義文件路勁
  int i;
  char c[3];
 
  for(i=1;i<=100;i++)
  {
    sprintf(c,"%d",i);
    strcat(pn,c);

    if((fd = open(pn,FLAGS, MODE)) == -1) {
        printf("open file error\n");
        
        strcpy(pn, "/root/test/file");

        continue;
    }
    printf("open file successful");
    puts(pn);
    printf("fc = %d", fd);    //輸出fd文件描述符

    strcpy(pn, "/root/test/file");
  }
  return 0;
}

詳細(xì)請(qǐng)看

驗(yàn)證

1前普、設(shè)置文件打開(kāi)最高數(shù)量為100

ulimit -SHn 100

2肚邢、運(yùn)行程序

[root@node04 test]# ./test
open file successful/root/test/file1
fc = 3open file successful/root/test/file2
fc = 4open file successful/root/test/file3
fc = 5open file successful/root/test/file4
fc = 6open file successful/root/test/file5
fc = 7open file successful/root/test/file6
fc = 8open file successful/root/test/file7
fc = 9open file successful/root/test/file8
fc = 10open file successful/root/test/file9
fc = 11open file successful/root/test/file10
fc = 12open file successful/root/test/file11
fc = 13open file successful/root/test/file12
fc = 14open file successful/root/test/file13
fc = 15open file successful/root/test/file14
fc = 16open file successful/root/test/file15
fc = 17open file successful/root/test/file16
fc = 18open file successful/root/test/file17
fc = 19open file successful/root/test/file18
fc = 20open file successful/root/test/file19
fc = 21open file successful/root/test/file20
fc = 22open file successful/root/test/file21
fc = 23open file successful/root/test/file22
fc = 24open file successful/root/test/file23
fc = 25open file successful/root/test/file24
fc = 26open file successful/root/test/file25
fc = 27open file successful/root/test/file26
fc = 28open file successful/root/test/file27
fc = 29open file successful/root/test/file28
fc = 30open file successful/root/test/file29
fc = 31open file successful/root/test/file30
fc = 32open file successful/root/test/file31
fc = 33open file successful/root/test/file32
fc = 34open file successful/root/test/file33
fc = 35open file successful/root/test/file34
fc = 36open file successful/root/test/file35
fc = 37open file successful/root/test/file36
fc = 38open file successful/root/test/file37
fc = 39open file successful/root/test/file38
fc = 40open file successful/root/test/file39
fc = 41open file successful/root/test/file40
fc = 42open file successful/root/test/file41
fc = 43open file successful/root/test/file42
fc = 44open file successful/root/test/file43
fc = 45open file successful/root/test/file44
fc = 46open file successful/root/test/file45
fc = 47open file successful/root/test/file46
fc = 48open file successful/root/test/file47
fc = 49open file successful/root/test/file48
fc = 50open file successful/root/test/file49
fc = 51open file successful/root/test/file50
fc = 52open file successful/root/test/file51
fc = 53open file successful/root/test/file52
fc = 54open file successful/root/test/file53
fc = 55open file successful/root/test/file54
fc = 56open file successful/root/test/file55
fc = 57open file successful/root/test/file56
fc = 58open file successful/root/test/file57
fc = 59open file successful/root/test/file58
fc = 60open file successful/root/test/file59
fc = 61open file successful/root/test/file60
fc = 62open file successful/root/test/file61
fc = 63open file successful/root/test/file62
fc = 64open file successful/root/test/file63
fc = 65open file successful/root/test/file64
fc = 66open file successful/root/test/file65
fc = 67open file successful/root/test/file66
fc = 68open file successful/root/test/file67
fc = 69open file successful/root/test/file68
fc = 70open file successful/root/test/file69
fc = 71open file successful/root/test/file70
fc = 72open file successful/root/test/file71
fc = 73open file successful/root/test/file72
fc = 74open file successful/root/test/file73
fc = 75open file successful/root/test/file74
fc = 76open file successful/root/test/file75
fc = 77open file successful/root/test/file76
fc = 78open file successful/root/test/file77
fc = 79open file successful/root/test/file78
fc = 80open file successful/root/test/file79
fc = 81open file successful/root/test/file80
fc = 82open file successful/root/test/file81
fc = 83open file successful/root/test/file82
fc = 84open file successful/root/test/file83
fc = 85open file successful/root/test/file84
fc = 86open file successful/root/test/file85
fc = 87open file successful/root/test/file86
fc = 88open file successful/root/test/file87
fc = 89open file successful/root/test/file88
fc = 90open file successful/root/test/file89
fc = 91open file successful/root/test/file90
fc = 92open file successful/root/test/file91
fc = 93open file successful/root/test/file92
fc = 94open file successful/root/test/file93
fc = 95open file successful/root/test/file94
fc = 96open file successful/root/test/file95
fc = 97open file successful/root/test/file96
fc = 98open file successful/root/test/file97
fc = 99open file error
open file error
open file error
  • 99-3+1=97 可見(jiàn)設(shè)置了100,但是最多可以打開(kāi)97個(gè)文件。
  • 系統(tǒng)應(yīng)該占用了3個(gè)打開(kāi)文件骡湖。
  • 那么設(shè)置成3贱纠,應(yīng)該所有命令都不能使用,設(shè)置成4則可以使用一個(gè)命令响蕴。

3谆焊、設(shè)置ulimit為3和4

[root@node04 test]# ulimit -SHn 3
[root@node04 test]# ls
-bash: start_pipeline: 進(jìn)程組管道: 打開(kāi)的文件過(guò)多
ls: error while loading shared libraries: libselinux.so.1: cannot open shared object file: Error 24
[root@node04 test]# ulimit -SHn 4
[root@node04 test]# ls
-bash: start_pipeline: 進(jìn)程組管道: 打開(kāi)的文件過(guò)多
AWA??AVI??AUI??ATL?%X?   file22  file36  file5   file63  file77  file90
file1                    file23  file37  file50  file64  file78  file91
file10                   file24  file38  file51  file65  file79  file92
file11                   file25  file39  file52  file66  file8   file93
file12                   file26  file4   file53  file67  file80  file94
file13                   file27  file40  file54  file68  file81  file95
file14                   file28  file41  file55  file69  file82  file96
file15                   file29  file42  file56  file7   file83  file97
file16                   file3   file43  file57  file70  file84  nginx.retry
file17                   file30  file44  file58  file71  file85  nginx.yml
file18                   file31  file45  file59  file72  file86  test
file19                   file32  file46  file6   file73  file87  test1
file2                    file33  file47  file60  file74  file88  test1.c
file20                   file34  file48  file61  file75  file89  test.c
file21                   file35  file49  file62  file76  file9   var.yml

  • 果然設(shè)置成3的時(shí)候連ls命令都不能夠使用。
  • 當(dāng)設(shè)置成4的時(shí)候可以使用1條命令浦夷。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末辖试,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子劈狐,更是在濱河造成了極大的恐慌罐孝,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,104評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件肥缔,死亡現(xiàn)場(chǎng)離奇詭異莲兢,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)辫继,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)怒见,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人姑宽,你說(shuō)我怎么就攤上這事遣耍。” “怎么了炮车?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,697評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵舵变,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我瘦穆,道長(zhǎng)纪隙,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,836評(píng)論 1 298
  • 正文 為了忘掉前任扛或,我火速辦了婚禮绵咱,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘熙兔。我一直安慰自己悲伶,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,851評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布住涉。 她就那樣靜靜地躺著麸锉,像睡著了一般。 火紅的嫁衣襯著肌膚如雪舆声。 梳的紋絲不亂的頭發(fā)上花沉,一...
    開(kāi)封第一講書(shū)人閱讀 52,441評(píng)論 1 310
  • 那天柳爽,我揣著相機(jī)與錄音,去河邊找鬼碱屁。 笑死磷脯,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的忽媒。 我是一名探鬼主播争拐,決...
    沈念sama閱讀 40,992評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼晦雨!你這毒婦竟也來(lái)了架曹?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,899評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤闹瞧,失蹤者是張志新(化名)和其女友劉穎绑雄,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體奥邮,經(jīng)...
    沈念sama閱讀 46,457評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡万牺,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,529評(píng)論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了洽腺。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片脚粟。...
    茶點(diǎn)故事閱讀 40,664評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖蘸朋,靈堂內(nèi)的尸體忽然破棺而出核无,到底是詐尸還是另有隱情,我是刑警寧澤藕坯,帶...
    沈念sama閱讀 36,346評(píng)論 5 350
  • 正文 年R本政府宣布团南,位于F島的核電站,受9級(jí)特大地震影響炼彪,放射性物質(zhì)發(fā)生泄漏吐根。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,025評(píng)論 3 334
  • 文/蒙蒙 一辐马、第九天 我趴在偏房一處隱蔽的房頂上張望拷橘。 院中可真熱鬧,春花似錦喜爷、人聲如沸膜楷。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,511評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至穷绵,卻和暖如春轿塔,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,611評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工勾缭, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留揍障,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,081評(píng)論 3 377
  • 正文 我出身青樓俩由,卻偏偏與公主長(zhǎng)得像毒嫡,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子幻梯,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,675評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • 官網(wǎng) 中文版本 好的網(wǎng)站 Content-type: text/htmlBASH Section: User ...
    不排版閱讀 4,407評(píng)論 0 5
  • 在Linux下程序不尋常退出時(shí)兜畸,內(nèi)核會(huì)在當(dāng)前工作目錄下生成一個(gè)core文件(是一個(gè)內(nèi)存映像,同時(shí)加上調(diào)試信息)碘梢。使...
    隨風(fēng)化作雨閱讀 45,806評(píng)論 2 15
  • linux資料總章2.1 1.0寫(xiě)的不好抱歉 但是2.0已經(jīng)改了很多 但是錯(cuò)誤還是無(wú)法避免 以后資料會(huì)慢慢更新 大...
    數(shù)據(jù)革命閱讀 12,175評(píng)論 2 33
  • Linux打開(kāi)文件限制 1咬摇、修改用戶(hù)進(jìn)程可打開(kāi)文件數(shù)限制 在linux平臺(tái)上,無(wú)論是客戶(hù)端程序還是服務(wù)器端程序煞躬,在...
    詞窮又詞貧閱讀 18,736評(píng)論 1 9
  • 菩提大叔閱讀 131評(píng)論 0 1