在 *NIX 系统上,有没有办法找出当前正在运行的进程中有多少个打开的文件句柄?
我正在从正在运行的相关进程中寻找用于 C 语言的 API 或公式。
最佳答案
在某些系统上(见下文),您可以在/proc/[pid]/fd 中对它们进行计数。如果不在其中之一,请参阅下面的内容:wallyk's answer .
在c中,可以列出目录并统计总数,也可以列出目录内容:
#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;
}
在 bash 中,类似于:
ls -l /proc/[pid]/fd/ | wc -l
Operating systems that support the proc filesystem include, but are not limited to:
Solaris
IRIX
Tru64 UNIX
BSD
Linux (which extends it to non-process-related data)
IBM AIX (which bases its implementation on Linux to improve compatibility)
QNX
Plan 9 from Bell Labs
关于c++ - 查找当前打开的文件句柄数(不是 lsof),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8163757/