Blog dedicated to Oracle Applications (E-Business Suite) Technology; covers Apps Architecture, Administration and third party bolt-ons to Apps

Thursday, August 13, 2009

C program to test open file descriptors

Sun has given this program to test the number of open file descriptors here.


vi fopentestcase.c

#include
#include


#define NoOfFILES 65536

int main()
{
char filename[10];
FILE *fds[NoOfFILES];
int i;

for (i = 0; i < NoOfFILES; ++i)
{
sprintf (filename, "/tmp/%d.log", i);
fds[i] = fopen(filename, "w");

if (fds[i] == NULL)
{
printf("\nNumber of open files = %d. " \
"fopen() failed with error: ", i);
perror("");
exit(1);
}
else
{
fprintf (fds[i], "some string");
}
}
return (0);
}

/usr/local/bin/gcc -o fopentestcase fopentestcase.c

$ ./fopentestcase

Number of open files = 253. fopen() failed with error: Too many open files
$

It errors out on the 254th file. This is because it is a 32 bit program.

No comments: