linux 查看文件引用计数

一.在对文件操作的系统调用接口,并没有提供获取对文件引用计数的信息,所有想要获取此信息,有两种方法:


    1.在内核中添加一个系统调用.将内核中文件的引用count计数返回到应用层,这种比较麻烦.不推荐
    2.遍历整个系统中所有进程打开此文件的个数./proc/pid/fd/**, 以此来统计引用计数.lsof和fuser可能就是用的此方法,没有做考证.

例子如下:

june_hou@ubuntu:~$ ps
  PID TTY          TIME CMD
  936 pts/9    00:00:00 bash
 3586 pts/9    00:00:00 a.out
 4533 pts/9    00:00:00 ps
june_hou@ubuntu:~$ ls -al /proc/3586/fd/ -al
total 0
dr-x------ 2 june_hou june_hou  0 Jan 11 13:26 .
dr-xr-xr-x 9 june_hou june_hou  0 Jan 11 13:26 ..
lrwx------ 1 june_hou june_hou 64 Jan 11 13:26 0 -> /dev/pts/9
lrwx------ 1 june_hou june_hou 64 Jan 11 13:26 1 -> /dev/pts/9
lrwx------ 1 june_hou june_hou 64 Jan 11 13:26 2 -> /dev/pts/9
lrwx------ 1 june_hou june_hou 64 Jan 11 13:26 3 -> /home/june_hou/test.log
lrwx------ 1 june_hou june_hou 64 Jan 11 13:26 4 -> /home/june_hou/test.log

june_hou@ubuntu:~$ lsof test.log 
COMMAND  PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
a.out   3586 june_hou    3u   REG  252,7        0  357 test.log
a.out   3586 june_hou    4u   REG  252,7        0  357 test.log

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值