[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Max number of file descriptors

Posted on 2004-11-28
16
Medium Priority
?
523 Views
Last Modified: 2008-01-09
In c, how do I get the maximum number of file descriptors allowed by the operating system?
0
Comment
Question by:jameswalt
12 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 12692662
0
 

Author Comment

by:jameswalt
ID: 12692672
I'm talking about c, not the shell

is there a command or external variable that provides this value ?
0
 
LVL 3

Expert Comment

by:baboo_
ID: 12693205

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Expert Comment

by:efn
ID: 12693613
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
 
LVL 11

Expert Comment

by:cup
ID: 12694231
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12694270
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
 
LVL 22

Expert Comment

by:grg99
ID: 12695352
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
 

Author Comment

by:jameswalt
ID: 12695654
Im working with sun solaris
0
 
LVL 15

Accepted Solution

by:
efn earned 80 total points
ID: 12699530
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
 
LVL 5

Expert Comment

by:van_dy
ID: 12699561
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
 
LVL 15

Expert Comment

by:efn
ID: 12890393
I believe van_dy's answer is also correct.  I suggest splitting the points.
0
 
LVL 15

Expert Comment

by:efn
ID: 12890487
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question