Re: File Descriptors

From: pr0grmr (chetmarty_at_hotmail.com)
Date: 03/04/05


Date: Fri, 04 Mar 2005 05:29:19 GMT


#include <stdio.h>
#include <limits.h>
#include <sys/resource.h>

int main (int argc, char ** argv)
{
 int rv = 0;
 int i;
 char tmp[128];
 FILE * fd[512];
 struct rlimit mylim;

  if (getrlimit(RLIMIT_NOFILE, &mylim) == -1)
  perror("getrlimit");
 else
 {
  printf("hardlim: %d softlim: %d\n", (int)mylim.rlim_max,
(int)mylim.rlim_cur);
  mylim.rlim_cur = 1024;
    if (setrlimit(RLIMIT_NOFILE, &mylim) == -1)
   perror("setrlimit");
  else if (getrlimit(RLIMIT_NOFILE, &mylim) == -1)
   perror("getrlimit");
  else
   printf("hardlim: %d softlim: %d\n", (int)mylim.rlim_max,
(int)mylim.rlim_cur);
 }

 for (i = 0; i < 512; i++)
 {
  sprintf(tmp, "%3d.dat", i);
  if ((fd[i] = fopen(tmp, "wb")) == NULL)
  {
   perror("fileopen");
   printf("open files: %d\n", i);
   i++; break;
  }
 }

 while(i--)
  fclose(fd[i]);

 return rv;
}

Program run: cshell prompt
hardlim: 65536 softlim: 256
hardlim: 65536 softlim: 1024
fileopen: Too many open files
open files: 253

compilation:
gcc -Wall -O3 -ansi -o gsl fileopen.c

uname -a
SunOS shark 5.9 Generic_117171-12 sun4u sparc SUNW,Ultra-60

Thanks for the interest ...

"David Schwartz" <davids@webmaster.com> wrote in message
news:<d08mm4$nn7$1@nntp.webmaster.com>...
>
> "pr0grmr" <chetmarty@hotmail.com> wrote in message
> news:w1RVd.509$oO4.30@newsread3.news.pas.earthlink.net...
> > Hello All,
> > I am trying to raise the limit of files (file descriptors) that
> > my
> > process can have open and can't get past the OPEN_MAX value.
> > If I getrlimit and setrlimit via RLIMIT_NOFILE to 1024 then a getrlimit
> > again, it shows a soft limit of 1024, originally 256, but if
> > I continue my program opening files, it stops again at 250. The later 6
> > are for STDOUT, STDERR and the like. Can anyone point me in the
> > direction of raising
>
> Post the shortest complete example program that demonstrates your
> problem. Specify what platform you ran it on as well and precisely how you
> invoked it.
>
> DS
>
>

"David Schwartz" <davids@webmaster.com> wrote in message
news:d08mm4$nn7$1@nntp.webmaster.com...
>
> "pr0grmr" <chetmarty@hotmail.com> wrote in message
> news:w1RVd.509$oO4.30@newsread3.news.pas.earthlink.net...
>> Hello All,
>> I am trying to raise the limit of files (file descriptors) that my
>> process can have open and can't get past the OPEN_MAX value.
>> If I getrlimit and setrlimit via RLIMIT_NOFILE to 1024 then a getrlimit
>> again, it shows a soft limit of 1024, originally 256, but if
>> I continue my program opening files, it stops again at 250. The later 6
>> are for STDOUT, STDERR and the like. Can anyone point me in the direction
>> of raising
>
> Post the shortest complete example program that demonstrates your
> problem. Specify what platform you ran it on as well and precisely how you
> invoked it.
>
> DS
>
>



Relevant Pages

  • Re: Where is the bug?!? help
    ... your observations were most helpfull ... ... > It's preferable to specify main's return type as int, ... > Some users may try your program on a platform which doesn't have a ...
    (comp.lang.c)
  • Re: SHOW DEVICE/FILES with FORTRAN?
    ... >> I don't think that locks are involved. ... to to find a list of open files. ... typedef struct {unsigned int len; void *address;} desc; ... printf ("The search_devnam argument accepts the standard wildcard ...
    (comp.os.vms)
  • Re: printf()
    ... >> Out of curiosity, why do programmers ... No, they are not, even leaving aside the lack of a newline in the ... int main ... all open files are closed (hence all output ...
    (comp.lang.c)
  • Re: Too Many Open Files --- error 24
    ... anything with libc or so? ... It is also behaving same way...! ... int main ...
    (comp.unix.programmer)
  • Re: opteron, VS2005 beta, win64: std::streamsize is 4 bytes?
    ... >i need to open files> 2GB. ... > a win32 console application that looks like: ... > int main ...
    (microsoft.public.vc.language)