Linux的open file 与 file descriptor区别

1 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.

2 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。

3 区别

  • lsof 可以查出某个进程打开的文件数目(即 open file)。
root@testbox ~]# 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

[root@testbox ~]# lsof | grep '4448' | wc -l

42                    //打开的文件数目
  • ls用于查询进程使用的文件描述符数目
[root@testbox ~]# 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

:

[root@testbox ~]# ls -l /proc/4448/fd/ | wc -l

19                    //文件描述符数目 + 1 (注意还有一行 “total 18”)

4 查看文件描述符的设置

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

65536

--该参数可以动态修改

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

$cat /proc/sys/fs/file-nr           //该文件只能读,不能修改

1380    180     65536

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

   |     |          (与file-max一致)

   |     |__ Total free allocated file descriptors

   |

   |__  Total allocated file descriptors

To compute the number of file descriptors currently being used:

1380 - 180 = 1200

参考文献:
[1] http://blog.itpub.net/15480802/viewspace-734062/ 博主:myownstars

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值