The number of open files in a program in C ++

Is there an easy way to get the number of files opened by a C ++ program.

I would like to do this from my code, ideally in C ++.

I found this blog article that uses a loop through the entire available file descriptor and tests the result of fstat , but I wonder if there is an easier way to do this.



Edit

There seems to be no other solution than to store the number of open files. Thank you all for your help.

Kevin

+4
source share
5 answers

Since the files are FILE * , we could do something like this:

In the header file that gets everywhere:

 #define fopen(x, y) debug_fopen(x, y, __FILE__, __LINE__) #define fclose(x) debug_fclose(x) 

in "debugfile.cpp" (obviously DO NOT use the above #define )

 struct FileInfo { FileInfo(const char *nm, const char fl, int ln) : name(nm), file(fl), line(ln) {} std::string name; const char *file; int line; }; std::map<FILE*, FileInfo> filemap; FILE *debug_fopen(const char *fname, const char *mode, const char *file, int line) { FILE *f = fopen(fname, mode); if (f) { FileInfo inf(fname, file, line); filemap[f] = inf; } } int debug_fclose(FILE *f) { int res = fclose(f); filemap.erase(f); return res; } // Called at some points. void debug_list_openfiles() { for( i : filemap ) { cerr << "File" << (void *) i.first << " opened as " << i.second.name << " at " << i.second.file << ":" << i.second.line << endl; } } 

(I did not compile this code, and it meant showing the concept, it might have small errors, but I think the concept will be executed - while your code, and not some third-party library, is leaking)

+1
source

If you are on Linux, this information is available under /proc/you_pid/fd .

Then use lstat for each file descriptor to save only regular files.

0
source

There is a good practice that the volume of a file opened in the smallest possible open dump, all the necessary information or a buffer in fd, is then closed.

therefore, this normal normal case will have 3 fd std in / out / err and all open files.

keeping track of your open files manually is best if you open files.

enter the global variable fdCounter, increase it after successfully opening the file, decrease it after closing

0
source

If you encapsulated it correctly, it should just add link counts or log in and print them to the console.

One approach to debugging is to override open calls with your own implementation and invoke the real thing from there. Then you can also enter some entries in the system to see if you lose file descriptors. How to open files? Using open() or are you using fopen() ?

Something like this might be:

 #include <fstream> #include <iostream> #include <stdlib.h> #include <fcntl.h> inline int log_open(char *p, int flags, int mode) { int fd = ::open(p, flags, mode); std::cout << "OPEN: " << fd << std::endl; return fd; } inline int log_close(int fd) { int rc = ::close(fd); std::cout << "CLOSE: " << fd << std::endl; return rc; } #define open(p, f, m) log_open(p, f, m) #define close(fd) log_close(fd) int main(int argc, char *argv[]) { int fd = open("tmp.txt", O_RDWR | O_CREAT | O_TRUNC, 0666); std::cout << "FD: " << fd << std::endl; if(fd != -1) close(fd); return 0; } 
0
source

In my experience, by the time you need to count the number of file descriptors, you don’t know where they were opened, which submodule or library. Thus, wrapping open / close is not a viable strategy. Brute force counting seems the only way.

The domain with the blog post orig is no longer resolved in DNS. I copy two sentences from Find the current number of open file descriptors (NOT lsof)

 int j, n = 0; // count open file descriptors for (j = 0; j < FDMAX; ++j) // FDMAX should be retrieved from process limits, // but a constant value of >=4K should be // adequate for most systems { int fd = dup (j); if (fd < 0) continue; ++n; close (fd); } printf ("%d file descriptors open\n", n); 

as well as this:

 #include <stdio.h> #include <sys/types.h> #include <dirent.h> int main (void) { DIR *dp; struct dirent *ep; dp = opendir ("/proc/MYPID/fd/"); if (dp != NULL) { while (ep = readdir (dp)) puts (ep->d_name); (void) closedir (dp); } else perror ("Couldn't open the directory"); return 0; } 
0
source

Source: https://habr.com/ru/post/1486020/


All Articles