Is there an easy way to find out the number of open files held by a process (using C or Ruby)?
-Charlie
I think the way I phrased the question above makes the problem sound harder than it is...
I am unittesting a C extension. The extension opens files using open(). I want to make sure that the extension closes all the files it opens (when errors occur or whatever).
So I figured getting a count of open file descriptors before the extension code is called and after it is finished would enable me to do this.
-Charlie
···
On Aug 24, 2004, at 12:41 PM, Charles Mills wrote:
Is there an easy way to find out the number of open files held by a process (using C or Ruby)?
There is a command, lsof, for *nix OSes that can do that.
If you don't have it, you can get it via FTP from
lsof.itap.purdue.edu:/pub/tools/unix/lsof/lsof_4.72.tar.bz2
Kirk Haines
···
On Wed, 25 Aug 2004 04:41:06 +0900, Charles Mills wrote
Is there an easy way to find out the number of open files held by a
process (using C or Ruby)?
*nix only, but
man lsof
man fuser
-a
···
On Wed, 25 Aug 2004, Charles Mills wrote:
Is there an easy way to find out the number of open files held by a
process (using C or Ruby)?
-Charlie
--
EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
A flower falls, even though we love it;
and a weed grows, even though we do not love it. --Dogen
===============================================================================
"Charles Mills" <cmills@freeshell.org> schrieb im Newsbeitrag
news:267B9838-F607-11D8-93A8-000A95A27A10@freeshell.org...
> Is there an easy way to find out the number of open files held by a
> process (using C or Ruby)?
>
I think the way I phrased the question above makes the problem sound
harder than it is...
I am unittesting a C extension. The extension opens files using
open(). I want to make sure that the extension closes all the files it
opens (when errors occur or whatever).So I figured getting a count of open file descriptors before the
extension code is called and after it is finished would enable me to do
this.
IMHO this would not be safe unless you disable threading: another thread
could open or close files in the meantime.
Why don't you just create a customized class that does the bookkeeping?
Alternatively you could hand in open IO objects, if the logic of your
extension allows this.
Kind regards
robert
···
On Aug 24, 2004, at 12:41 PM, Charles Mills wrote:
Thanks for the suggestion.
I guess another way to do it would be to walk all the valid file descriptors and call fstat() on them, checking for EBADF.
I was hoping there would a system call / library function that would do this for me.
-Charlie
···
On Aug 24, 2004, at 1:00 PM, Ara.T.Howard@noaa.gov wrote:
On Wed, 25 Aug 2004, Charles Mills wrote:
Is there an easy way to find out the number of open files held by a
process (using C or Ruby)?
-Charlie*nix only, but
man lsof
man fuser-a
Charles Mills <cmills@freeshell.org> writes:
I am unittesting a C extension. The extension opens files using
open(). I want to make sure that the extension closes all the files it
opens (when errors occur or whatever).
Call me crazy, but what's wrong with adding each file descriptor you
open into a list, and then traversing that list at error time (or
whatever) to make sure they're all closed?
So I figured getting a count of open file descriptors before the
extension code is called and after it is finished would enable me to do
this.
I'm not sure what that would buy you. If your extension code can take
a block, you're completely screwed-- that block may open new
filehandles, or close old ones, and your count-based approach would be
totally out into the weeds. Even if it doesn't, signal handlers and
other asynchronous code may open or close them without your explicit
knowledge.
Besides, knowing how many descriptors are open is a far cry from
knowing *which* descriptors are open, which is necessary to close()
them.
-=Eric
···
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
define an xopen that updates a static var - then you won't have to worry about
some other process/thread/function opening up files and killing your test...
cheerx.
-a
···
On Wed, 25 Aug 2004, Charles Mills wrote:
On Aug 24, 2004, at 12:41 PM, Charles Mills wrote:
Is there an easy way to find out the number of open files held by a
process (using C or Ruby)?I think the way I phrased the question above makes the problem sound
harder than it is...
I am unittesting a C extension. The extension opens files using
open(). I want to make sure that the extension closes all the files it
opens (when errors occur or whatever).So I figured getting a count of open file descriptors before the
extension code is called and after it is finished would enable me to do
this.
--
EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
A flower falls, even though we love it;
and a weed grows, even though we do not love it. --Dogen
===============================================================================
"Charles Mills" <cmills@freeshell.org> schrieb im Newsbeitrag
news:267B9838-F607-11D8-93A8-000A95A27A10@freeshell.org...Is there an easy way to find out the number of open files held by a
process (using C or Ruby)?I think the way I phrased the question above makes the problem sound
harder than it is...
I am unittesting a C extension. The extension opens files using
open(). I want to make sure that the extension closes all the files it
opens (when errors occur or whatever).So I figured getting a count of open file descriptors before the
extension code is called and after it is finished would enable me to do
this.IMHO this would not be safe unless you disable threading: another thread
could open or close files in the meantime.
Good point.
The unittests are pretty simple. Basically more or less testing a C library (not multithreaded) wrapped in Ruby.
There is a point where all Ruby data gets converted to C types/structures and passed to the C library functions I am testing. However, the library functions handle opening and closing files themselves.
-Charlie
···
On Aug 24, 2004, at 1:05 PM, Robert Klemme wrote:
On Aug 24, 2004, at 12:41 PM, Charles Mills wrote:
Why don't you just create a customized class that does the bookkeeping?
Alternatively you could hand in open IO objects, if the logic of your
extension allows this.Kind regards
robert
That is a good idea. ![]()
-Charlie
···
On Aug 24, 2004, at 1:20 PM, Eric Schwartz wrote:
Charles Mills <cmills@freeshell.org> writes:
I am unittesting a C extension. The extension opens files using
open(). I want to make sure that the extension closes all the files it
opens (when errors occur or whatever).Call me crazy, but what's wrong with adding each file descriptor you
open into a list, and then traversing that list at error time (or
whatever) to make sure they're all closed?
Yeah, if your curious that is what I am doing. The library that I am testing already uses functions that wrap open() and close(). So I am adding a global structure to that module to keeps track of the fd's in use. Should be a much more robust test - keeping track of which fd's are created using xopen and which are closed using xclose .... should be 1 to 1.
Thanks for all the help,
Charlie
···
On Aug 24, 2004, at 1:45 PM, Ara.T.Howard@noaa.gov wrote:
On Wed, 25 Aug 2004, Charles Mills wrote:
On Aug 24, 2004, at 12:41 PM, Charles Mills wrote:
Is there an easy way to find out the number of open files held by a
process (using C or Ruby)?I think the way I phrased the question above makes the problem sound
harder than it is...
I am unittesting a C extension. The extension opens files using
open(). I want to make sure that the extension closes all the files it
opens (when errors occur or whatever).So I figured getting a count of open file descriptors before the
extension code is called and after it is finished would enable me to do
this.define an xopen that updates a static var - then you won't have to worry about
some other process/thread/function opening up files and killing your test...cheerx.
-a
"Charles Mills" <cmills@freeshell.org> schrieb im Newsbeitrag
news:A42068F2-F60A-11D8-93A8-000A95A27A10@freeshell.org...
>
> "Charles Mills" <cmills@freeshell.org> schrieb im Newsbeitrag
> news:267B9838-F607-11D8-93A8-000A95A27A10@freeshell.org...
>>
>>> Is there an easy way to find out the number of open files held by a
>>> process (using C or Ruby)?
>>>
>> I think the way I phrased the question above makes the problem sound
>> harder than it is...
>> I am unittesting a C extension. The extension opens files using
>> open(). I want to make sure that the extension closes all the files
>> it
>> opens (when errors occur or whatever).
>>
>> So I figured getting a count of open file descriptors before the
>> extension code is called and after it is finished would enable me to
>> do
>> this.
>
> IMHO this would not be safe unless you disable threading: another
> thread
> could open or close files in the meantime.
>
Good point.
The unittests are pretty simple. Basically more or less testing a C
library (not multithreaded) wrapped in Ruby.
There is a point where all Ruby data gets converted to C
types/structures and passed to the C library functions I am testing.
However, the library functions handle opening and closing files
themselves.
Yeah, but then how about my suggestion:
> Why don't you just create a customized class that does the bookkeeping?
> Alternatively you could hand in open IO objects, if the logic of your
> extension allows this.
You can get the file descriptor by using IO#fileno.
If you'd uncover a bit more of your extension's functionality, we might come
up with better solutions...
Kind regards
robert
···
On Aug 24, 2004, at 1:05 PM, Robert Klemme wrote:
>> On Aug 24, 2004, at 12:41 PM, Charles Mills wrote: