Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

count of file descriptors
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...?


Report this thread to moderator Post Follow-up to this message
Old Post
upendrao@gmail.com
06-02-05 02:02 PM


Re: count of file descriptors
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


Report this thread to moderator Post Follow-up to this message
Old Post
vishal
06-02-05 02:02 PM


Re: count of file descriptors
"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



Report this thread to moderator Post Follow-up to this message
Old Post
Rodrick Brown
06-03-05 08:57 AM


Re: count of file descriptors
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


Report this thread to moderator Post Follow-up to this message
Old Post
upendrao@gmail.com
06-03-05 08:57 AM


Re: count of file descriptors
Hi Mr.Brown,
Thanks for your help.

_upendra


Report this thread to moderator Post Follow-up to this message
Old Post
upendrao@gmail.com
06-03-05 08:57 AM


Re: count of file descriptors
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Casper H.S. Dik
06-03-05 02:07 PM


Re: count of file descriptors
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

Report this thread to moderator Post Follow-up to this message
Old Post
Måns Rullgård
06-03-05 02:07 PM


Re: count of file descriptors
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

Report this thread to moderator Post Follow-up to this message
Old Post
Jens.Toerring@physik.fu-berlin.de
06-03-05 02:07 PM


Re: count of file descriptors
>   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

Report this thread to moderator Post Follow-up to this message
Old Post
Gordon Burditt
06-03-05 09:00 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:43 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.