For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > June 2005 > count of file descriptors









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author count of file descriptors
upendrao@gmail.com

2005-06-02, 9:02 am

Hi,
I have a requirement in which I need to count the number
of file descriptors associated with a file. To be more clear...A file
can be opened by several processes say in read only mode. I would like
to count the number of such file opens on particular file at a given
instant of time. Any C library function or unix command that can solve
my problem please...?

vishal

2005-06-02, 9:02 am

try the `lsof` command. I don't think that its included in all Unix
systems by default, but it has been ported to most Unix systems. It is
included in Linux (Slackware 10.0) by default.

you may also consider `fuser` command, which gives a list of processes
using a particular file. I don't think it gives a list of FDs per
process.

vishal

Rodrick Brown

2005-06-03, 3:57 am


"vishal" <vishal.ag@gmail.com> wrote in message
news:1117696439.229219.148890@f14g2000cwb.googlegroups.com...
> try the `lsof` command. I don't think that its included in all Unix
> systems by default, but it has been ported to most Unix systems. It is
> included in Linux (Slackware 10.0) by default.
>
> you may also consider `fuser` command, which gives a list of processes
> using a particular file. I don't think it gives a list of FDs per
> process.
>
> vishal
>


if your on solaris you can use pfiles


upendrao@gmail.com

2005-06-03, 3:57 am

Hi Vishal,
Thanks a lot for your support. Yes, I am on Solaris. I couldn't find
a direct command that exactly serves my purpose but I think this piece
of code should... Comments please...

/ ****************************************
*************************************
*Supply the complete path of the file whose open stream to be computed*
*as command line argv[1]
*
****************************************
**************************************/
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
int fd, in, open=0;
struct rlimit rlim;
struct stat statbuf;

if(argc<2)
exit();
if(stat(argv[1],&statbuf)==0)
in=statbuf.st_ino; /* Get the I-NODE number of the required file
(argv[1]) */
getrlimit(RLIMIT_NOFILE, &rlim);
for(open = fd = 0; fd < rlim.rlim_cur; ++fd)
{
if(fstat(fd, &statbuf) == 0)
{
if(in==statbuf.st_ino) /* compare in with all open/valid fds
*/
open += 1;
}
}
printf("Total FDs open for \" %s \" are %d\n",argv[1], open);
}


_upendra

upendrao@gmail.com

2005-06-03, 3:57 am

Hi Mr.Brown,
Thanks for your help.

_upendra

Casper H.S. Dik

2005-06-03, 9:07 am

upendrao@gmail.com writes:

>Hi,
> I have a requirement in which I need to count the number
>of file descriptors associated with a file. To be more clear...A file
>can be opened by several processes say in read only mode. I would like
>to count the number of such file opens on particular file at a given
>instant of time. Any C library function or unix command that can solve
>my problem please...?


On Solaris:

find /proc/*/fd -inum X

where X is the inode number of the file in question.

You then need to do some filtering to make sure it's from the proper
device.

Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
Måns Rullgård

2005-06-03, 9:07 am

Casper H.S. Dik <Casper.Dik@Sun.COM> writes:

> upendrao@gmail.com writes:
>
>
> On Solaris:
>
> find /proc/*/fd -inum X
>
> where X is the inode number of the file in question.


That will work on Linux too, if you add -follow to the find options.

--
Måns Rullgård
mru@inprovide.com
Jens.Toerring@physik.fu-berlin.de

2005-06-03, 9:07 am

upendrao@gmail.com wrote:
> I have a requirement in which I need to count the number
> of file descriptors associated with a file. To be more clear...A file
> can be opened by several processes say in read only mode. I would like
> to count the number of such file opens on particular file at a given
> instant of time. Any C library function or unix command that can solve
> my problem please...?


And later you wrote (reformatted for readability):

> #include <sys/resource.h>
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <stdio.h>


> int main(int argc, char *argv[])
> {
> int fd, in, open=0;
> struct rlimit rlim;
> struct stat statbuf;


> if(argc<2)
> exit();
> if(stat(argv[1],&statbuf)==0)
> in=statbuf.st_ino; /* Get the I-NODE number of the required file
> (argv[1]) */
> getrlimit(RLIMIT_NOFILE, &rlim);
> for(open = fd = 0; fd < rlim.rlim_cur; ++fd)
> {
> if(fstat(fd, &statbuf) == 0)
> {
> if(in==statbuf.st_ino) /* compare in with all open/valid fds */
> open += 1;
> }
> }
> printf("Total FDs open for \" %s \" are %d\n",argv[1], open);
> }


But this definitely does not do what you set out to do (if you correct
the obvious errors in this program). It will count how many times this
program itself has the file opened passed to it as the first command
line argument by name. And since this program doesn't open any files
at all the result will always be 0. It won't tell you how many times
the file is currently opened by other programs.

To do that you would have to get the system to tell you - it's keeping
the file table and thus is the only instance that could give you some
information like that if it stores such information at all. Some systems
may allow you to get at such information, but it's rather likely to be
non-portable. Probably something like lsof is your best bet, it seems
to be avaialable on quite a number of systems. The length of the man
page for that utility can be taken as a hint of how complicated it is
to get at the information....

There is only a loose connection between file names, i-nodes and file
descriptors. E.g., an i-node may be associated to several different
file names via hard links, so it's not a one-to-one relationship. And
a file may have been opened by a process and then unlinked (but the
process keeping the file still open, so it's not deleted yet) and a
file by the same name gets created by another process anew. What's the
count your looking for supposed to be in that case, 1 or 2 (assuming
no other process has one of the versions of that file open)?

You could take all these complications as a hint that what you try to
do might not be too good an idea. So what do you need that information
for? Perhaps there are other ways you can achieve the intended results
without having to determine this bit of information, but, of course,
you must tell what you want to do to give others a chance to come up
with some ideas.
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Gordon Burditt

2005-06-03, 4:00 pm

> Thanks a lot for your support. Yes, I am on Solaris. I couldn't find
>a direct command that exactly serves my purpose but I think this piece
>of code should... Comments please...


I don't know about Solaris, but on other UNIX OSs a file is identified
uniquely by the pair {device,inode}. Just inode is not enough.
For example, on many systems with mounted file systems, /, /usr,
and /home will all have inode 2, because they are at the root of a
filesystem.

Also, your program only computes the number of file descriptors
YOUR PROGRAM has open for that file. If this program was invoked
by another program, that number might not be zero. However,
for normal use invoking it from a shell, all there is to count is
stdin, stdout, and stderr.

Also remember that if you want to count the number of file descriptors
open for a particular file at a particular moment, you won't get
the result until at least two moments later, and ONE moment later someone
might have opened or closed the file, so the info is stale by the
time you get to try to use it.

Gordon L. Burditt
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com