I encountered a problem recently where a program, which is intended to run continuously, would stop after a few days. After much line-by-line inspection and adding extra code, it turned out to be too many open files caused by a missing close() statement. It turned out to be a socket that wasn't being closed but the effect is the same; the program ends when it runs out of file handles.

The function presented here determines the number of open files for the given process ID. Your main program can call this function to monitor the number of open files and take appropriate action. This function is only intended as a diagnostic tool for use during a program's development and testing phases; it's not something that would be put into production - if you need to keep track of open files, then do so when opening and closing them.

In Linux OSes, a process's open files are listed in the folder /proc/<process-pid>/fd. All that's needed is a way to count the number of file entries in that directory. The function takes a process ID and opens the corresponding directory and counts the number of entries. As you would expect the directories . and .. are also listed so the actual count is 2 fewer. Files, pipes, sockets and other devices, such as those on SPI and I²C buses, all require file handles and will be listed. At a command line if you list the directory with ls -g you'll see the different types.

Exclamation mark Note that you can only examine the open files directory for the current process or its children. If you want to make the function more general then you'll need to modify the executable to run with elevated privileges.

Too many open files is one cause of program failure but integer and float/double overflow is another, though they have different symptoms. Integer overflow causes the value to go negative or return to zero, depending upon whether it is signed or not. Typical cases are using integers with large values for timing purposes, using signed when you should be using unsigned and so on. Floats and doubles on the other hand lose precision so you may find that the program works correctly but the results are not what you expect.