暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Linux文件描述符和文件句柄数

原创 做个俗人 2021-04-15
1768

1.打开/etc/security/limits.conf 或者 /etc/security/limits.d/20-nproc.conf(此名称

第一列表示用户,表示所有用户
soft nproc:单个用户可用的最大进程数量(超过会警告);
hard nproc:单个用户可用的最大进程数量(超过会报错);
soft nofile:可打开的文件描述符的最大数(超过会警告);
hard nofile:可打开的文件描述符的最大数(超过会报错);
重新登录后生效。
2.Ulimit:
Ulimit官方描述:
Provides control over the resources available to the shell and to processes started by it, on systems that allow such control. The -H and -S options specify that the hard or soft limit is set for the given resource. A hard limit cannot be increased once it is set; a soft limit may be increased up to the value of the hard limit. If neither -H nor -S is specified, both the soft and hard limits are set. The value of limit can be a number in the unit specified for the resource or one of the special values hard, soft, or unlimited, which stand for the current hard limit, the current soft limit, and no limit, respectively.
If limit is omitted, the current value of the soft limit of the resource is printed, unless the -H option is given. When more than one resource is specified, the limit name and unit are printed before the value.
红色部分说明ulimit是对shell可用资源和由shell启动的进程打开文件的控制;非root用户的ulimit -n的值只能设置的越来越小,而root用户没有限制;非root用户ulimit -SHn设置的值不能大于/etc/security/limits.conf设置的值,但是root用户没有限制。
Ulimit -SHn命令的修改只对当前终端生效,不是永久生效。
/proc/sys/fs/file-max:是系统给出的建议值,系统会计算资源给出一个合理值,一般跟内存有关系,内存越大,改值越大,但是仅仅是一个建议值;指定了系统范围内所有进程可打开的最大句柄数限制(系统级);file-max一般为内存(KB)的10%。可以通过执行echo fs.file-max = 5242880 >> /etc/sysctl.conf命令永久修改file-max值,然后执行sysctl -p立即生效。
/proc/sys/fs/file-nr:文件中的三个值分别表示:已分配的文件句柄数、已分配但未使用的文件句柄数、最大的文件句柄数。
/proc/sys/fs/nr_open:内核参数,指的是单个进程可打开的最大文件句柄数,默认值是1048576(1024
1024)。
注:(centos7.5)如果在/etc/security/limits.d/20-nproc.conf文件和/etc/security/limits.conf文件同时设置了nproc和nofile时,以/etc/security/limits.d/20-nproc.conf为准。
3.文件描述符和句柄数的区别
Linux系统中,每当进程打开一个文件时,系统就为其分配一个唯一的整型文件描述符,用来标识这个文件。标准C中每个进程默认打开的有三个文件,标准输入,标准输出,标准错误,分别用一个FILE结构的指针来表示,即stdin,stout,sterr,这三个结构分别对应着三个文件描述符0,1,2。
文件描述符是一个简单的整数,用以标明每一个进程所打开的文件和socket。第一个打开的文件是0,第二个打开的是1,依此类推。
内核参数:单个进程可分配的最大文件数(nofile): /proc/sys/fs/nr_open;单个进程可分配的最大文件句柄数可以通过ulimit -n(ulimit -a)命令查看;
注:在/etc/security/limits.d/20-nproc.conf或者/etc/security/limits.conf中设置的soft nofile与hard nofile数值不能超过内核限制的文件句柄数。
文件描述符是进程级别的,文件句柄是系统级别的,不能混用。它们在不同级别表示已打开的文件。
文件描述符与文件句柄直接关联,文件句柄与inode直接关联。
附:
nr_open:
This denotes the maximum number of file-handles a process can allocate.
Default value is 1024*1024 (1048576) which should be
enough for most machines.
Actual limit depends on RLIMIT_NOFILE
resource limit.
file-max:
The value in file-max denotes the maximum number of file-
handles that the Linux kernel will allocate.
When you get lots of error messages about running out of file handles,
you might want to increase this limit.
Historically,the kernel was able to allocate file handles
dynamically, but not to free them again.
file-nr:
The three values in file-nr denote the number of allocated file handles,
the number of allocated but unused file handles, and the maximum number of
file handles.
Linux 2.6 always reports 0 as the number of free
file handles – this is not an error, it just means that the
number of allocated file handles exactly matches the number of used file handles.
Attempts to allocate more file descriptors than file-max
are reported with printk, look for “VFS: file-max limit reached”.

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论