get number of open files in a process?

Batutis, Ed Ed.Batutis at netapp.com
Thu Jan 21 19:00:24 UTC 2016


> > > Use getdtablesize() to get the current largest known fd. If you
> > > want, you can afterwards loop and use fcntl with F_GETFD to check if
> > > the descriptor is still open.

Since I need an open handle count I would have to do 2048 fcntl() calls in my case. I'm afraid this doesn't look like a good idea, since it might happen a lot under certain circumstances.

Looking at the source for kinfo_getfile, it seems like using this is not a good idea because, according to a comment in bug 197695:

https://www.mail-archive.com/freebsd-bugs@freebsd.org/msg21569.html

the function uses a sysctl with KERN_PROC_FILEDESC which "might be slow". This function also does two memory allocations for data I don't need to look at.

So, I guess the answer is, for me anyway, that there is no sufficiently efficient way to get the number of open file handles from the system for my user-space application.

But, if anyone has any more ideas, I'd be happy to hear them!

Thanks,

=Ed




More information about the freebsd-hackers mailing list