Chinaunix
标题:
获取当前进程打开的文件数
[打印本页]
作者:
gg297231604
时间:
2010-07-11 09:58
标题:
获取当前进程打开的文件数
如何在程序中获取当前进程打开的文件数?
作者:
leewei
时间:
2010-07-11 20:25
lsof -g pid
作者:
leewei
时间:
2010-07-11 20:49
#include <linux/fs.h> /*struct file*/
#include <linux/file.h> /*fget() fput()*/
#include <linux/fdtable.h> /*NR_OPEN_DEFAULT*/
#include <linux/dcache.h> /*d_path()*/
void KernelFd_ShowFd(void)
{
int i_Loop = 0;
char ac_Buf[64];
char * pc_FdName = NULL;
struct file * pst_File = NULL;
printk("\nshow Fd:\n");
//遍历FD
for (i_Loop = 0; i_Loop < NR_OPEN_DEFAULT; i_Loop++)
{
//取出FD对应struct file并检验
pst_File = fget(i_Loop);
if (NULL != pst_File)
{
//取出FD对应文件路径及文件名并检验
pc_FdName = d_path(&(pst_File->f_path), ac_Buf, sizeof(ac_Buf));
if (NULL != pc_FdName)
{
printk("\tfd %02d is %s, addr is 0x%p\n", i_Loop, pc_FdName, pst_File);
}
else
{
printk("\tfd %02d name is NULL, addr is 0x%p\n", i_Loop, pst_File);
}
//fget(),fput()成对
fput(pst_File);
}
}
printk("\n");
}
作者:
gg297231604
时间:
2010-07-12 11:33
回复
3#
leewei
有没不用内核编程来通过fd获取文件名的方法吗?
作者:
gg297231604
时间:
2010-07-12 16:05
问题已解决
打开/proc/pid/fd,用readlink就可以得到其链接文件
谢谢leewei的帮助
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2