[prev in list] [next in list] [prev in thread] [next in thread] 

List:       freebsd-hackers
Subject:    Re: get number of open files in a process?
From:       Hooman Fazaeli <hoomanfazaeli () gmail ! com>
Date:       2016-08-27 16:34:24
Message-ID: 57C1BE40.2060902 () gmail ! com
[Download RAW message or body]

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@freebsd.org mailing list
> https://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@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

_______________________________________________
freebsd-hackers@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic