/proc/sys/kernel/threads-max 系统最大线程数量
/proc/sys/vm/max_map_count 限制一个进程可以拥有的VMA(虚拟内存区域)的数量
/proc/sys/kernel/pid_max 系统最大进程数量

  

默认情况下,执行ulimit -a,可以看到 
open files                      (-n) 1024 
我们如何来验证这个1024的真实性呢? 
写了个简单的测试程序分享如下: 
1、先创建文件数量100000个,用shell

mkdir ./filedir
i=0
while [ $i -lt 100000 ] ;
do
touch ./filedir/f_$i
o_file=./filedir/f_$i
echo "$o_file"
echo "file$i" > ./filedir/f_$i
i=$(($i+1))
#echo $i
done

该程序创建filedir目录下名为f_i的文件,i为文件编号,执行该程序,可以在filedir下生成f_0—f_100000的文件 
下面我们写个c程序来测试到底我们在默认情况下可否同时打开这100000个文件,代码如下:   

~]$ cat test_openfiles.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h> #include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> #define MAX_FILES 100000
int main()
{
int i = 0;
int fd;
char a[8];
int count = 0; for (i = 0; i < MAX_FILES; i++) {
char buf[24] = "./filedir/f_";
sprintf(a, "%d", i);
strcat(buf, a);
printf("file_name:%s\n", buf);
fd = open(buf, O_RDWR);
if (fd != -1) {
count++;
printf("==fd:%d==\n", fd);
printf("Opened %d files\n", count);
} else {
printf("Error, can only open %d files\n", count);
return 0;
}
}
return 0;
}

编译这个c文件

gcc test_openfiles.c

会生成一个a.out的文件

执行./a.out

输出结果如下:
==fd:1020==
Opened 1018 files
file_name:./filedir/f_1018
==fd:1021==
Opened 1019 files
file_name:./filedir/f_1019
==fd:1022==
Opened 1020 files
file_name:./filedir/f_1020
==fd:1023==
Opened 1021 files
file_name:./filedir/f_1021
Error, can only open 1021 files

测试发现最大只能打开1021个文件

调整最大打开文件数

修改/etc/security/limits.conf ,在最后面添加如下:

* - nofile  1048576

重新登录终端生效

[tuser@localhost ~]$ exit
logout
[root@localhost ~]# su - tuser
Last login: Tue Oct 16 09:18:01 CST 2018 on pts/2
[tuser@localhost ~]$ ulimit -n
1048576

再次执行./a.out文件

file_name:./filedir/f_99995
==fd:99998==
Opened 99996 files
file_name:./filedir/f_99996
==fd:99999==
Opened 99997 files
file_name:./filedir/f_99997
==fd:100000==
Opened 99998 files
file_name:./filedir/f_99998
==fd:100001==
Opened 99999 files
file_name:./filedir/f_99999
==fd:100002==
Opened 100000 files

linux打开文件数测试的更多相关文章

  1. linux 打开文件数 too many open files 解决方法

    linux 打开文件数 too many open files 解决方法 too many open files 出现这句提示的原因是程序打开的文件/socket连接数量超过系统设定值. 查看每个用户 ...

  2. Linux 打开文件数

    linux设置最大打开文件数 - daiyudong2020的博客 - CSDN博客 https://blog.csdn.net/daiyudong2020/article/details/77828 ...

  3. ulimit open files linux打开文件数设置验证

    #include <stdio.h> #include <sys/types.h> #include <fcntl.h> #include <stdlib.h ...

  4. linux 打开文件数too many open files解决方法

    出现这句提示的原因是程序打开的文件/socket连接数量超过系统设定值.查看每个用户最大允许打开的文件数量ulimit -a 其中 open files (-n) 1024 表示每个用户最大允许打开的 ...

  5. (转)linux 打开文件数 too many open files 解决方法

    too many open files 出现这句提示的原因是程序打开的文件/socket连接数量超过系统设定值. 查看每个用户最大允许打开文件数量 ulimit -a fdipzone@ubuntu: ...

  6. linux系统工程师修改打开文件数限制代码教程。服务器运维技术

    提示linux文件打开错误,修改linux打开文件数参数. /etc/pam.d/login 添加 session required /lib/security/pam_limits.so 注意看这个 ...

  7. 放开Linux内核对用户进程可打开文件数和TCP连接的限制

    一. 检查linux内核uname -alsb_release -a 二. 用户进程可打开文件数限制1) vim /etc/security/limits.conf*       -      nof ...

  8. 在Linux最大打开文件数限制下 MySQL 对参数的调整

    http://www.actionsky.com/docs/archives/78  2016年4月7日  周文雅 目录 1 起因 2 说明 3 MySQL调整参数的方式 3.1 计算 request ...

  9. 调整Linux最大打开文件数

    #!/bin/bash ## 文件数限制 ulimit -n ulimit -Sn ulimit -Hn ## fs.nr_open,进程级别 ## fs.file-max,系统级别 ## 最大文件描 ...

随机推荐

  1. IOHelper(自制常用的输入输出的帮助类)

    常用的读写文件,和地址转换(win和linux均支持),操作文件再也不是拼接那么的low了 using System; using System.Diagnostics; using System.I ...

  2. [WIP]php 基本语法

    创建: 2019/06/14 https://www.php.net/manual/zh/langref.php php标记   当解析一个文件时,PHP 会寻找起始和结束标记,也就是 <?ph ...

  3. 清北刷题冲刺 10-29 a.m

    遭遇 /* 因为选的楼是个集合,与顺序无关 而且总花费=c[1]+c[2]+c[3]+|h[1]-h[2]|+|h[2]-h[3]| 我们规定走的顺序从高到低,那么绝对值就可以去掉 所以就可以约掉中间 ...

  4. 洛谷P3258 [JLOI2014]松鼠的新家

    P3258 [JLOI2014]松鼠的新家 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他 ...

  5. JavaWeb:Cookie处理和Session跟踪

    JavaWeb:Cookie处理和Session跟踪 Cookie处理 什么是Cookie Cookie 是存储在客户端计算机上的文本文件,保留了各种跟踪信息.因为HTTP协议是无状态的,即服务器不知 ...

  6. MCP|DYM|Quantitative mass spectrometry to interrogate proteomic heterogeneity in metastatic lung adenocarcinoma and validate a novel somatic mutation CDK12-G879V (利用定量质谱探究转移性肺腺瘤的蛋白质组异质性及验证新体细胞突变)

    文献名:Quantitative mass spectrometry to interrogate proteomic heterogeneity in metastatic lung adenoca ...

  7. PJzhang:英国通信总部GCHQ开源产品-网络瑞士军刀CyberChef

    猫宁!!! 参考链接:https://www.4hou.com/info/news/981.html 这个产品免费开源易用,如果称之为网络瑞士军刀,没什么异议. github地址:https://gi ...

  8. 【Kafka】《Kafka权威指南》入门

    发布与订阅消息系统 在正式讨论Apache Kafka (以下简称Kafka)之前,先来了解发布与订阅消息系统的概念, 并认识这个系统的重要性.数据(消息)的发送者(发布者)不会直接把消息发送给接收 ...

  9. day22作业详解

    1.面向对象作业1 2.作业详解 点击查看详细内容 #1. class Li(object): def func1(self): print('in func1') obj = Li() obj.fu ...

  10. ELK系列(5) - Logstash怎么分割字符串并添加新的字段到Elasticsearch

    问题 有时候我们想要在Logstash里对收集到的日志等信息进行分割,并且将分割后的字符作为新的字符来index到Elasticsearch里.假定需求如下: Logstash收集到的日志字段mess ...