Finding open file descriptors for a process linux ( C code )?

后端 未结 6 1247
遇见更好的自我
遇见更好的自我 2020-12-08 01:02

I wanted to find all fds opened for a process in linux.

Can I do it with glib library functions ?

6条回答
  •  离开以前
    2020-12-08 01:32

    Sometimes C++ is an option, Donal's solution using boost::filesystem:

    #include 
    #include 
    #include 
    #include 
    
    namespace fs = boost::filesystem;
    
    int main()
    {
        std::string path = "/proc/" + std::to_string(::getpid()) + "/fd/";
        unsigned count = std::distance(fs::directory_iterator(path),
                                       fs::directory_iterator());
        std::cout << "Number of opened FDs: " << count << std::endl;
    }
    

提交回复
热议问题