Testing max open files linux

Linux

Some times ulimit -a does not work or does not give the real value. There if you are getting multiple time Too many open files issue and processes are getting hung then its good to check the number of files that can really be kept open in a system. To checck this I usually rely on a small C program which creates continues open files till we get the max count for openfiles in a system.

Make sure the code is executed from a temporary folder once executed successfully as many temp files will be created in the same folder.

 

Fopen.c Content

 
#include <stdlib.h>
#include <stdio.h>
#include <error.h>

int main(int argc, char* argv[]) {
int iOpenFilesCnt = 0;

while (1) {
char szFileName[256];
sprintf(szFileName, "my_file_%d.txt", iOpenFilesCnt);
printf("Opening file name : %s, count is : %d", szFileName, iOpenFilesCnt);
FILE* pFile = fopen(szFileName, "wb");
if (pFile == NULL) {
perror("Error opening file :");
return 1;
}
printf(" - done\n");
iOpenFilesCnt++;


}

return 0;
}

Compile the C Program:

 cc fopen.c -o fopen
Run:./fopen

In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.