get number of open files in a process?
Hooman Fazaeli
hoomanfazaeli at gmail.com
Sat Aug 27 16:22:29 UTC 2016
On 2016-01-21 17:04, Batutis, Ed wrote:
> Hi,
>
> I need to determine the number of open files in my process - all types of file handles total - sockets, files, everything.
>
> Does this work reliably for that purpose?
>
> int num_open = 0; /* number of open files? */
> kinfo_file *inf = kinfo_getfile(getpid(), &num_open);
> if ( inf ) {
> free(inf);
> }
>
>
> Thanks,
>
> =Ed
>
>
> _______________________________________________
> freebsd-hackers at freebsd.org mailing list
> https://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "freebsd-hackers-unsubscribe at freebsd.org"
My reply may be late and you may already solved the problem.
Anyway, one possible solution is to do (slow) fd counting in another
thread and update an atomic integer which the main thread can read
and act upon:
volatile int num_open = 0;
fdcount_thread_proc(void *arg){
while (!exit_flag) {
num_open = get_the_number_of_open_files_in_whatever_way_you_prefer();
usleep(1000);
}
}
Of course, the main thread does not get the __exact__ number of
open files when it reads num_open, butif you just need to
know when your are approaching the limit, a close estimation would
be enough.
--
Best regards
Hooman Fazaeli
More information about the freebsd-hackers
mailing list