对于Linux系统, 万物皆文件, 包括普通文件, 目录, 设备, 共享库, socket, nfs等
观察vi /proc/meminfo
的文件打开数
查询进程ID
# ps aux |grep meminfo
root 31542 0.0 0.0 126308 1700 pts/0 T 15:13 0:00 vi /proc/meminfo
root 31544 0.0 0.0 112648 960 pts/0 S+ 15:13 0:00 grep --color=auto meminfo
根据进程ID, 查看文件打开数
# lsof -p 31542
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
vi 31542 root cwd DIR 253,0 84 939716632 /opt
vi 31542 root rtd DIR 253,0 4096 64 /
vi 31542 root txt REG 253,0 910040 100666070 /usr/bin/vi
vi 31542 root mem REG 253,0 61752 1261 /usr/lib64/libnss_files-2.17.so
vi 31542 root mem REG 253,0 106070960 100664815 /usr/lib/locale/locale-archive
vi 31542 root mem REG 253,0 143352 1269 /usr/lib64/libpthread-2.17.so
vi 31542 root mem REG 253,0 19888 1558 /usr/lib64/libattr.so.1.1.0
vi 31542 root mem REG 253,0 19344 1249 /usr/lib64/libdl-2.17.so
vi 31542 root mem REG 253,0 398264 1371 /usr/lib64/libpcre.so.1.2.0
vi 31542 root mem REG 253,0 2116736 1243 /usr/lib64/libc-2.17.so
vi 31542 root mem REG 253,0 37056 1561 /usr/lib64/libacl.so.1.1.0
vi 31542 root mem REG 253,0 174520 1355 /usr/lib64/libtinfo.so.5.9
vi 31542 root mem REG 253,0 155744 2991 /usr/lib64/libselinux.so.1
vi 31542 root mem REG 253,0 155064 1236 /usr/lib64/ld-2.17.so
vi 31542 root 0u CHR 136,0 0t0 3 /dev/pts/0
vi 31542 root 1u CHR 136,0 0t0 3 /dev/pts/0
vi 31542 root 2u CHR 136,0 0t0 3 /dev/pts/0
vi 31542 root 3u REG 253,0 4096 15191812 /var/tmp/meminfo.swp
FD => file description
- cwd = 当前工作目录
- txt = 程序代码
- mem = 内存映射文件, 一般为共享库
- 0=标准输入, 1=标准输出, 2=标准错误, u=读写, r=只读, w=只写
TYPE
- DIR = 目录
- CHR = 字符类型
- REG = 文件
/proc/31542/fd/
目录, 显示vi
进程下有4个文件打开数, 剔除了进程本身(包括工作目录, 二进制程序, 共享库等), 只保留操作的目标文件, 所以统计上略有差异
# ll /proc/31542/fd/
total 0
lrwx------ 1 root root 64 Dec 6 15:14 0 -> /dev/pts/0
lrwx------ 1 root root 64 Dec 6 15:14 1 -> /dev/pts/0
lrwx------ 1 root root 64 Dec 6 15:13 2 -> /dev/pts/0
lrwx------ 1 root root 64 Dec 6 15:14 3 -> /var/tmp/meminfo.swp
lsof = list open files
# lsof /usr/bin/vi
# lsof -u root
# lsof -p 31542
# lsof -i:5258
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
docker-pr 129353 root 4u IPv4 683948 0t0 TCP *:5258 (LISTEN)
docker-pr 129359 root 4u IPv6 675762 0t0 TCP *:5258 (LISTEN)