測試ulimit的打開文件數(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代表著軟硬限制, 軟限制就是超過就提醒并不真正限制了,硬限制就是強(qiáng)制在內(nèi),超過報(bào)錯(cuò)。
  • a代表顯示所有參數(shù)
  • 只有寫到/etc/security/limits.conf文件里邊才會(huì)永久生效。
  • 使用ulimit只是臨時(shí)的,當(dāng)前用戶退出登錄即解除。

生成100個(gè)空文件

touch file{1..n}

編寫打開文件的C語言代碼

#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   //只寫,文件不存在那么就創(chuàng)建,文件長度戳為0  
#define MODE S_IRWXU | S_IXGRP | S_IROTH | S_IXOTH //創(chuàng)建文件的權(quán)限,用戶讀、寫、執(zhí)行、組讀、執(zhí)行、其他用戶讀、執(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è)置文件打開最高數(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 可見設(shè)置了100,但是最多可以打開97個(gè)文件。
  • 系統(tǒng)應(yīng)該占用了3個(gè)打開文件。
  • 那么設(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)程組管道: 打開的文件過多
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)程組管道: 打開的文件過多
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)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容