Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-11-2005, 11:17 AM   #1
Registered: Dec 2004
Location: India
Distribution: Fedora Core 4
Posts: 145

Rep: Reputation: 15
How to find out the number of open file descriptors?

Is there any way to find out the number of open file descriptors in a program?
Old 12-11-2005, 12:38 PM   #2
Registered: Dec 2003
Distribution: Debian, FreeBSD
Posts: 310

Rep: Reputation: 35
Pinky:/var/log# ls /proc/14685/fd | wc -l

Where the number between proc/ and /fd is the pid number of the application.
0 members found this post helpful.
Old 12-11-2005, 10:23 PM   #3
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
You could try with getdtablesize() first to know the maximum number of file descriptors, then apply fcntl(2) on each one trying to get information, for example: the close-on-exec flag, discarding anyone that gives errors (errno should be EBADF).

The number that getdtablesize() gives is runtime, so it is the same as getrlimit(RLIMIT_NOFILE) or sysconf(_SC_OPEN_MAX). I managed to open file descriptors > 100 and then lower the limit with setrlimit() and keep them open, so it's best to grab a maximum value (which may be OPEN_MAX, usually present in <limits.h>).

I will share with you my code that puts all open file descriptors with the close-on-exec flag set on a select set. Changing it to return the number of open file descriptors is easy. Here's I'm using FD_SETSIZE as the upper limit.

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/select.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

int get_ncloexec(fd_set *cloexec)
    fd_set closet;
    int flags;
    int fd;
    int n = 0;


    for (fd = 0; fd < (int) FD_SETSIZE; fd++) {
        errno = 0;
        flags = fcntl(fd, F_GETFD, 0);
        if (flags == -1 && errno) {
            if (errno != EBADF) {
#ifdef DEBUG
                return -1;
        if (flags & FD_CLOEXEC) {
            FD_SET(fd, &closet);

    if (cloexec)
        *cloexec = closet;

    return n;
1 members found this post helpful.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Increasing File Descriptors Geoffrey_Firmin Slackware 5 04-21-2008 09:48 AM
file descriptors node047 Linux - Newbie 1 03-29-2005 05:47 AM
unix file descriptors versus c FILE pointers nodger Programming 9 11-25-2004 07:02 AM
Expect script throws "open(slave pty): bad file number" error KNut Linux - Newbie 0 02-24-2004 12:24 PM
File descriptors odd question ferreter Linux - General 2 02-13-2003 10:21 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:24 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration