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条命令。