How to use lsof(List Opened Files) in a C/C++ application?

后端 未结 4 500
死守一世寂寞
死守一世寂寞 2021-01-05 08:20

Is there any way to get all opened sockets using c++? I know the lsof command and this is what I\'m looking for, but how to use it in a c++ application?

4条回答
  •  萌比男神i
    2021-01-05 08:58

    The lsof command is prepared specifically such that it can be used from other programs including C, see the section: OUTPUT FOR OTHER PROGRAMS of man lsof for more information. For example you can invoke lsof with -F p and it will output the pid of the processes prefixed with 'p':

    $ lsof -F p /some/file
    p1234
    p4321
    

    you can then use popen to execute this commmand in a child process and read from its standard output.

提交回复
热议问题