LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 12-11-2005, 11:17 AM   #1
skie_knite007
Member
 
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
fur
Member
 
Registered: Dec 2003
Distribution: Debian, FreeBSD
Posts: 310

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

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
primo
Member
 
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.

Code:
#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;

    FD_ZERO(&closet);

    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
                perror("fcntl(F_GETFD)");
#endif
                return -1;
            }
            else
                continue;
        }
        if (flags & FD_CLOEXEC) {
            FD_SET(fd, &closet);
            n++;
        }
    }

    if (cloexec)
        *cloexec = closet;

    return n;
}
 
1 members found this post helpful.
  


Reply


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


All times are GMT -5. The time now is 11:30 PM.

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