linux文件描述符open file descriptors与open files的区别

Posted zengkefu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux文件描述符open file descriptors与open files的区别相关的知识,希望对你有一定的参考价值。

http://blog.itpub.net/15480802/viewspace-734062

什么是open file

An open file may be a regular file, a directory, a block special file, a character special file, an executing text reference, a library, a stream or a network file.

 

什么是file descriptor

A file descriptor is a data structure used by a program to get a handle on a file.  The most commonly known are:

0 for standard in

1 for standard out

2 for standard error

 

两者关系

一个文件即使被打开,也可能没有文件描述符,比如current working directories, memory mapped files and executable text files

lsof可以查出某个进程打开的文件数目,

[[email protected] ~]# lsof | grep ‘4448‘

oracle    4448  oracle  cwd       DIR        3,2      4096    1913941 /u01/app/oracle/product/10.2.0/db_1/dbs

oracle    4448  oracle  rtd       DIR        3,2      4096          2 /

oracle    4448  oracle  txt       REG        3,2  93300099    1915187 /u01/app/oracle/product/10.2.0/db_1/bin/oracle

oracle    4448  oracle  mem       REG        3,2     95148    2174926 /lib/libnsl-2.3.4.so

oracle    4448  oracle  mem       REG        3,2    106397    2174894 /lib/ld-2.3.4.so

oracle    4448  oracle  mem       REG        3,2   1454546    2501884 /lib/tls/libc-2.3.4.so

:

[[email protected] ~]# lsof | grep ‘4448‘ | wc -l

42

以下方法用于查询进程使用的文件描述符数目

[[email protected] ~]# ls -l /proc/4448/fd/

total 18

lr-x------  1 oracle oinstall 64 Mar  5 15:04 0 -> /dev/null

lr-x------  1 oracle oinstall 64 Mar  5 15:04 1 -> /dev/null

lrwx------  1 oracle oinstall 64 Mar  5 15:04 10 -> /u01/app/oracle/product/10.2.0/db_1/rdbms/audit/ora_4446.aud

lr-x------  1 oracle oinstall 64 Mar  5 15:04 11 -> /dev/zero

lr-x------  1 oracle oinstall 64 Mar  5 15:04 12 -> /dev/zero

:

[[email protected] ~]# ls -l /proc/4448/fd/ | wc -l

19

进程4448总共打开42个文件,只占用了19个文件描述符;

 

查看文件描述符的设置

$cat /proc/sys/fs/file-max

65536

--该参数可以动态修改

 

计算当前被使用的文件描述符数目

$cat /proc/sys/fs/file-nr

1380 180 65536

   |  |   |_ Max no. of file descriptors allowed on the system

   |  |     

   |  |__ Total free allocated file descriptors

   |

   |__  Total allocated file descriptors

To compute the number of file descriptors currently being used:

1380 - 180 = 1200

 

其他有用参数

NR_OPEN  = Maximum number of open files per process

NR_FILE  = Total number of files that can be open in the system at any time

FILE-MAX = Kernel parameter refers to maximum number of file descriptors allowed per system

FILE-NR  = Refers to the number of current file descriptors used at any moment.

LSOF     = Gives the number of open files.

 

ulimit –a查看当前用户的shell limit 

若某些资源设置过小会导致系统无法正常提供服务

http://space.itpub.net/15480802/viewspace-720722   关于nofile/nproc的测试

 

参考资料Open Files/Open File Descriptors [ID 787780.1]

 
 

以上是关于linux文件描述符open file descriptors与open files的区别的主要内容,如果未能解决你的问题,请参考以下文章

Nginx文件描述符缓存--open_file_cache

怎样改变Linux 系统 open files 选项

Linux文件描述符限制和单机最大长连接数

Linux基本文件IO/open/write

Linux基础IO

[OS-Linux]详解Linux的基础IO ------- 文件描述符fd