fopen问题 - 打开的文件太多了

tom*_*myk 4 c++ windows fopen file-descriptor

我有一个在Win XP上运行的多线程应用程序.在某个阶段,其中一个线程无法使用fopen函数打开现有文件._get_errno函数返回EMFILE,这意味着打开的文件太多.没有更多的文件描述符可用.我的平台的FOPEN_MAX是20._getmaxstdio返回512.我用WinDbg检查了这个,我看到大约有100个文件是打开的:

788 Handles
Type            Count
Event           201
Section         12
File            101
Port            3
Directory       3
Mutant          32
WindowStation   2
Semaphore       351
Key             12
Thread          63
Desktop         1
IoCompletion    6
KeyedEvent      1
Run Code Online (Sandbox Code Playgroud)

fopen失败的原因是什么?


编辑:

我写了简单的单线程测试应用程序.这个程序可以打开510文件.我不明白为什么这个应用程序可以打开更多文件,然后多线程应用程序.可能是因为文件句柄泄漏了吗?

#include <cstdio> 
#include <cassert> 
#include <cerrno> 
void main() 
{ 
    int counter(0); 

    while (true) 
    { 
        char buffer[256] = {0}; 
        sprintf(buffer, "C:\\temp\\abc\\abc%d.txt", counter++); 
        FILE* hFile = fopen(buffer, "wb+"); 
        if (0 == hFile) 
        { 
            // check error code 
            int err(0); 
            errno_t ret = _get_errno(&err); 
            assert(0 == ret); 
            int maxAllowed = _getmaxstdio(); 
            assert(hFile); 
        } 
    } 
}
Run Code Online (Sandbox Code Playgroud)

ere*_*eOn 5

我想这是您的操作系统的限制.它可以取决于很多东西:表示文件描述符的方式,它们消耗的内存等等.

而且我想你无能为力.也许有一些参数来调整这个限制.

真正的问题是,你真的需要同时打开那么多文件吗?我的意思是,即使你有100多个线程试图读取100多个不同的文件,它们可能无法同时读取它们,并且你可能得不到比50个线程更好的结果.

由于我们不知道你想要实现什么,因此很难更准确.