Madhu Desai |
07-04-2013 06:46 AM |
Quote:
Originally Posted by procfs
(Post 4981651)
Hi, I know this is a dumb question :(
I am trying to find the number of open files per process, and I found few commands but each gives deferrent values. below are the commands
ls /proc/<pid>/fd/*
and
lsof -p <PID>
Can some one tell me what I am doing wrong or am I reading the out put wrong
Thanks and Best Regards
|
I think its a valid question. I always used lsof whenever I was unable to unmount file system, so that I could see what files are still used by that file system. I never used /proc/<pid>/fd.
So it seems, both lsof and /proc/<pid>/fd bring different results for same pid.
I did little search in internet and it seems the difference between output from lsof and /proc/<pid>/fd/ are because lsof also displays other file descriptors and memory mapped .so files, which are mapped at /proc/<pid>/maps.
Code:
$ pidof firefox
3093
$ lsof -p 3093
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
firefox 3093 madhu cwd DIR 253,3 4096 2883585 /home/madhu
firefox 3093 madhu rtd DIR 253,1 4096 2 /
firefox 3093 madhu txt REG 253,1 94072 661414 /usr/lib64/firefox/firefox
firefox 3093 madhu mem REG 253,1 156872 661248 /lib64/ld-2.12.so
firefox 3093 madhu mem REG 253,1 22536 661252 /lib64/libdl-2.12.so
firefox 3093 madhu mem REG 253,1 1922152 661249 /lib64/libc-2.12.so
firefox 3093 madhu mem REG 253,1 145720 661250 /lib64/libpthread-2.12.so
firefox 3093 madhu mem REG 253,1 598680 661283 /lib64/libm-2.12.so
firefox 3093 madhu mem REG 253,1 47064 661251 /lib64/librt-2.12.so
<TRUNCATED>
$ lsof -p 3093 | grep -v '^COMMAND' | wc -l
303
$ ls -l /proc/3093/fd/*
lr-x------. 1 madhu madhu 64 Jul 4 11:17 /proc/3093/fd/0 -> /dev/null
lrwx------. 1 madhu madhu 64 Jul 4 11:17 /proc/3093/fd/1 -> /home/madhu/.xsession-errors
lrwx------. 1 madhu madhu 64 Jul 4 11:17 /proc/3093/fd/10 -> pipe:[20623]
lrwx------. 1 madhu madhu 64 Jul 4 16:37 /proc/3093/fd/101 -> /home/madhu/.mozilla/firefox/yu5ujt41.default/downloads.sqlite
lrwx------. 1 madhu madhu 64 Jul 4 16:37 /proc/3093/fd/102 -> socket:[42045]
lrwx------. 1 madhu madhu 64 Jul 4 11:17 /proc/3093/fd/11 -> socket:[20624]
lrwx------. 1 madhu madhu 64 Jul 4 11:17 /proc/3093/fd/12 -> /home/madhu/.mozilla/firefox/yu5ujt41.default/.parentlock
lrwx------. 1 madhu madhu 64 Jul 4 11:17 /proc/3093/fd/13 -> anon_inode:[eventpoll]
lrwx------. 1 madhu madhu 64 Jul 4 11:17 /proc/3093/fd/14 -> socket:[20630]
lrwx------. 1 madhu madhu 64 Jul 4 11:17 /proc/3093/fd/15 -> socket:[20631]
<TRUNCATED>
$ ls -l /proc/3093/fd/* | wc -l
79
So, as you can see lsof counts to 303, while /proc//fd counts to 79
Here is the list of File Descriptors
Quote:
cwd - current working directory
Lnn - library references (AIX)
err - FD information error (see NAME column)
jld - jail directory (FreeBSD)
ltx - shared library text (code and data)
Mxx - hex memory-mapped type number xx
m86 - DOS Merge mapped file
mem - memory-mapped file
mmap - memory-mapped device
pd - parent directory
rtd - root directory
tr - kernel trace file (OpenBSD)
txt - program text (code and data)
v86 - VP/ix mapped file
|
I put the File Descriptors in a file, and when used with grep, both are almost same now.
Code:
$ cat fds
cwd
Lnn
err
jld
ltx
Mxx
m86
mem
mmap
pd
rtd
tr
txt
v86
$ lsof -p 3093 | grep -v '^COMMAND' | grep -ivf fds | wc -l
84
$ ls -l /proc/3093/fd/* | wc -l
79
Almost Equal!!!
|