Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 27 Aug 2016 20:52:24 +0430
From:      Hooman Fazaeli <hoomanfazaeli@gmail.com>
To:        "Batutis, Ed" <Ed.Batutis@netapp.com>
Cc:        "freebsd-hackers@freebsd.org" <freebsd-hackers@freebsd.org>
Subject:   Re: get number of open files in a process?
Message-ID:  <57C1BE40.2060902@gmail.com>
In-Reply-To: <ea9f2ab2d4f543c8ad34fdeb082f8c7a@hioexcmbx08-prd.hq.netapp.com>
References:  <ea9f2ab2d4f543c8ad34fdeb082f8c7a@hioexcmbx08-prd.hq.netapp.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?57C1BE40.2060902>