测试ulimit的打开文件数量控制

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代表着软硬限制, 软限制就是超过就提醒并不真正限制了,硬限制就是强制在内,超过报错。
  • a代表显示所有参数
  • 只有写到/etc/security/limits.conf文件里边才会永久生效。
  • 使用ulimit只是临时的,当前用户退出登录即解除。

生成100个空文件

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   //只写,文件不存在那么就创建,文件长度戳为0  
#define MODE S_IRWXU | S_IXGRP | S_IROTH | S_IXOTH //创建文件的权限,用户读、写、执行、组读、执行、其他用户读、执行  
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;
}

详细请看

验证

1、设置文件打开最高数量为100

ulimit -SHn 100

2、运行程序

[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 可见设置了100,但是最多可以打开97个文件。
  • 系统应该占用了3个打开文件。
  • 那么设置成3,应该所有命令都不能使用,设置成4则可以使用一个命令。

3、设置ulimit为3和4

[root@node04 test]# ulimit -SHn 3
[root@node04 test]# ls
-bash: start_pipeline: 进程组管道: 打开的文件过多
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: 进程组管道: 打开的文件过多
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

  • 果然设置成3的时候连ls命令都不能够使用。
  • 当设置成4的时候可以使用1条命令。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342

推荐阅读更多精彩内容

  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,362评论 0 5
  • 在Linux下程序不寻常退出时,内核会在当前工作目录下生成一个core文件(是一个内存映像,同时加上调试信息)。使...
    随风化作雨阅读 44,578评论 2 15
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 12,128评论 2 34
  • Linux打开文件限制 1、修改用户进程可打开文件数限制 在linux平台上,无论是客户端程序还是服务器端程序,在...
    词穷又词贫阅读 18,676评论 1 9
  • 菩提大叔阅读 121评论 0 1