代码证明:
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc,char *argv[]){
int fd;
char name[1024];
int i = 0;
while(1){
sprintf(name,"file%d",++i);
fd = open(name,O_CREAT,0777); //超过最大打开数时再打开会出错,返回-1
if (fd == -1){
exit(1);
}
printf("%d ",i);
}
return 0;
}
运行代码:
#gcc -o haha open.c
#./haha
执行完成后会看到打印并创建了1021个文件,加上系统自带的0,1,2三个文件,总共为1024个
修改最大打开文件数。
查看系统定义的默认限制值:
# 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) 7674
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) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 7674
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
修改最大打开最大文件数:
# ulimit -n 4096
查看系统可以设置的打开的最大文件数:
# cat /proc/sys/fs/file-max
97267