• C

Max number of file descriptors

In c, how do I get the maximum number of file descriptors allowed by the operating system?
jameswaltAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

avizitCommented:
0
jameswaltAuthor Commented:
I'm talking about c, not the shell

is there a command or external variable that provides this value ?
0
baboo_Commented:

FILE* f = fopen("/proc/sys/fs/file-max", "r");
int* max_fd;
fread( (void*) max_fd, sizeof( int ), 1, f );
close( f );

printf("The max number of file descriptors is %d\n", *max_fd);

Will this do it for you?

baboo_
0
Get Cisco Certified in IT Security

There’s a high demand for IT security experts and network administrators who can safeguard the data that individuals, corporations, and governments rely on every day. Pursue your B.S. in Network Operations and Security and gain the credentials you need for this high-growth field.

efnCommented:
Standard C does not deal with file descriptors, so any answer will be platform-dependent.  If baboo_'s solution does not work for you, I suggest you specify the operating system(s) of interest to you.
0
cupCommented:
Do you mean file descriptors (the int returned when you open) or the file handles (the FILE* returned when you fopen)?  Note that the number of descriptors can be far greater than the number of handles.
0
PaulCaswellCommented:
In an MSDOS environment, use _nfile.

If you want to change it, let me know, I have some code that will do it. You cant just change it yourself as there are some global allocations that use this figure as a limit.

Paul
0
grg99Commented:
something like this:

 char FName[20];  int Num = 0;

while( open( sprintf( FName, "%d", Num ), O_CREAT ) )  Num++;

printf("The OS and C I/O library let  you open %d file descriptors\n", Num ):
0
jameswaltAuthor Commented:
Im working with sun solaris
0
efnCommented:
Use the getrlimit function with a parameter of RLIMIT_NOFILE.

http://docs.sun.com/app/docs/doc/817-0691/6mgfmmdp0?q=getrlimit&a=view

--efn
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
van_dyCommented:
i believe u can find that out on solaris with the following

#include <limits.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
        int max = sysconf(_SC_OPEN_MAX);
        if(max < 0)
                printf("sysconf error\n");
        else
                printf("Maximum no of allowed descriptors = %d\n", max);
        return 0;
}
0
efnCommented:
I believe van_dy's answer is also correct.  I suggest splitting the points.
0
efnCommented:
My expertise on this is not deep, but I guessed from the sysconf man page that both were process limits, since it says "A call to setrlimit() can cause the value of OPEN_MAX to change."  This wouldn't make sense if OPEN_MAX were a system-level limit.

http://docs.sun.com/app/docs/doc/817-0692/6mgfnkuse?q=sysconf&a=view

But, jmcg, you may well know more about this than I.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.