Linux 从 C 代码获取当前使用的文件描述符的计数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7976769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting count of current used file descriptors from C code
提问by Vivek Goel
Is there a C API to get the:
是否有 C API 来获取:
- Current used file descriptors system wide
- Current used file descriptors of the current process
- 当前使用的文件描述符系统范围
- 当前进程的当前使用的文件描述符
采纳答案by R.. GitHub STOP HELPING ICE
For the current process count, you can use getrlimit
to get the file descriptor limit, then iterate over all integers from 0 to that limit and try calling fcntl
with the F_GETFD
command. It will succeed only on the file descriptors which are actually open, letting you count them.
对于当前进程计数,您可以使用getrlimit
获取文件描述符限制,然后遍历从 0 到该限制的所有整数,并尝试fcntl
使用F_GETFD
命令进行调用。它只会在实际打开的文件描述符上成功,让您计算它们。
Edit:I now have a better way to do it. After getting the rlimit
, make a large array of struct pollfd
(as large as the limit if possible; otherwise you can break it down into multiple runs/calls) with each fd in the range and the events
member set to 0. Call poll
on the array with 0 timeout, and look for the POLLNVAL
flag in the revents
for each member. This will tell you which among a potentially-huge set of fds are invalid with a single syscall, rather than one syscall per fd.
编辑:我现在有更好的方法来做到这一点。获得 后,使用范围内的每个 fd 和成员设置为 0rlimit
制作一个大数组struct pollfd
(如果可能,尽可能大;否则您可以将其分解为多次运行/调用)。events
调用poll
数组时超时为 0 ,并POLLNVAL
在revents
每个成员的 中查找标志。这将告诉您在单个系统调用中,潜在的大量 fd 中哪些无效,而不是每个 fd 一个系统调用。
回答by Some programmer dude
Since you say you are on Linux, you can open the folder /proc/self/fd/
which should contain symbolic links to all open file descriptors.
既然你说你在 Linux 上,你可以打开文件夹/proc/self/fd/
,该文件夹应该包含所有打开的文件描述符的符号链接。
回答by Polynomial
You can read /proc/sys/fs/file-nr
to find the total number of allocated and free file system handles as well as the maximum allowed.
您可以阅读/proc/sys/fs/file-nr
以查找已分配和可用文件系统句柄的总数以及允许的最大值。
[root@box proc]# cat /proc/sys/fs/file-nr
3853 908 53182
| | |
| | |
| | max: maximum open file descriptors
| free: total free allocated file descriptors
allocated: total allocated file descriptors since boot
To calculate the number that are currently in use, just do allocated - free
. You could also calculate a percentage of used descriptors by doing ((allocated - free) / max) * 100
要计算当前使用的数量,只需执行allocated - free
。您还可以通过执行以下操作来计算已使用描述符的百分比((allocated - free) / max) * 100
As for per-process, I'm not sure of any programmatic way you can do it.
至于每个进程,我不确定您可以使用任何编程方式。
Here's a tutorial on how to do it with lsof
anyway: http://linuxshellaccount.blogspot.com/2008/06/finding-number-of-open-file-descriptors.html
这是一个关于如何使用的教程lsof
:http: //linuxshellaccount.blogspot.com/2008/06/finding-number-of-open-file-descriptors.html
回答by MD XF
I'm not positive about file descriptors, but you can easily check <stdio.h>
files.
我还不能肯定有关文件的描述,但你可以很容易地检查<stdio.h>
文件。
In stdio.h
, __sF
, is a file array that stores every FILE
. (googling __sF
shows many stdio
s with a matching keyword).
在stdio.h
, __sF
, 是一个文件数组,它存储每个FILE
. (谷歌搜索__sF
显示许多stdio
具有匹配关键字的 s)。
If a FILE
's flags are empty, the file is not in use. Therefore, we can simply walk through __sF
, checking the flag of each FILE
in the array.
如果 aFILE
的标志为空,则该文件未在使用中。因此,我们可以简单地遍历__sF
,检查FILE
数组中每个的标志。
#include <stdio.h>
int getOpenFileCount(void)
{
int fileCount;
for (fileCount = 0; __sF[fileCount]._flags != 0; fileCount++)
continue;
return fileCount;
}