C++ Windows 中打开文件的数量是否有限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/870173/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:40:38  来源:igfitidea点击:

Is there a limit on number of open files in Windows

c++windows

提问by Jimmy J

I'm opening lots of files with fopen() in VC++ but after a while it fails.

我在 VC++ 中用 fopen() 打开了很多文件,但过了一段时间它失败了。

Is there a limit to the number of files you can open simultaneously?

您可以同时打开的文件数量有限制吗?

回答by stack programmer

The C run-time libraries have a 512 limit for the number of files that can be open at any one time. Attempting to open more than the maximum number of file descriptors or file streams causes program failure. Use _setmaxstdioto change this number. More information about this can be read here

C 运行时库对可以在任何时间打开的文件数有 512 个限制。尝试打开超过最大数量的文件描述符或文件流会导致程序失败。使用_setmaxstdio更改此数。可以在此处阅读有关此的更多信息

Also you may have to check if your version of windows supports the upper limit you are trying to set with _setmaxstdio. For more information on _setmaxstdiocheck here

此外,您可能需要检查您的 Windows 版本是否支持您尝试设置的上限_setmaxstdio。有关_setmaxstdio检查这里的更多信息

Information on the subject corresponding to VS 2015 can be found here

可以在此处找到与 VS 2015 对应的主题的信息

回答by Drarok

In case anyone else is unclear as to what the limit applies to, I believe that this is a per-process limit and not system-wide.

如果其他人不清楚限制适用于什么,我相信这是每个​​进程的限制而不是系统范围的限制。

I just wrote a small test program to open files until it fails. It gets to 2045 files before failing (2045 + STDIN + STDOUT + STDERROR = 2048), then I left that open and ran another copy.

我只是写了一个小测试程序来打开文件,直到它失败。它在失败之前达到 2045 个文件(2045 + STDIN + STDOUT + STDERROR = 2048),然后我保持打开状态并运行另一个副本。

The second copy showed the same behaviour, meaning I had at least 4096 files open at once.

第二个副本显示了相同的行为,这意味着我一次至少打开了 4096 个文件。

回答by luiscolorado

If you use the standard C/C++ POSIX libraries with Windows, the answer is "yes", there is a limit.

如果您在 Windows 中使用标准 C/C++ POSIX 库,答案是“是”,这是有限制的。

However, interestingly, the limit is imposed by the kind of C/C++ libraries that you are using.

然而,有趣的是,限制是由您使用的 C/C++ 库的类型强加的。

I came across with the following JIRA thread (http://bugs.mysql.com/bug.php?id=24509) from MySQL. They were dealing with the same problem about the number of open files.

我遇到了以下来自 MySQL 的JIRA 线程 ( http://bugs.mysql.com/bug.php?id=24509)。他们正在处理有关打开文件数量的相同问题。

However, Paul DuBois explained that the problem could effectively be eliminated in Windows by using ...

但是,Paul DuBois 解释说,在 Windows 中可以通过使用...

Win32 API calls (CreateFile(), WriteFile(), and so forth) and the default maximum number of open files has been increased to 16384. The maximum can be increased further by using the --max-open-files=N option at server startup.

Win32 API 调用(CreateFile()、WriteFile() 等)和默认的最大打开文件数已增加到 16384。可以通过使用 --max-open-files=N 选项进一步增加最大值服务器启动。

Naturally, you could have a theoretically large number of open files by using a technique similar to database connections-pooling, but that would have a severe effect on performance.

自然地,通过使用类似于数据库连接池的技术,理论上您可以拥有大量打开的文件,但这会对性能产生严重影响。

Indeed, opening a large number of files could be bad design. However, some situations call require it. For example, if you are building a database server that will be used by thousands of users or applications, the server will necessarily have to open a large number of files (or suffer a performance hit by using file-descriptor pooling techniques).

实际上,打开大量文件可能是糟糕的设计。但是,某些情况下需要它。例如,如果您正在构建一个将被数千个用户或应用程序使用的数据库服务器,则该服务器将必须打开大量文件(或因使用文件描述符池技术而遭受性能损失)。

回答by Malcolm Post

Yes there are limits depending the access level you use when openning the files. You can use _getmaxstdioto find the limits and _setmaxstdioto change the limits.

是的,根据您打开文件时使用的访问级别,存在限制。您可以使用_getmaxstdio查找限制并_setmaxstdio更改限制。

回答by ReinstateMonica Larry Osterman

I don't know where Paulo got that number from.. In windows NT based operating systems the number of file handles opened per process is basically limited by physical memory - it's certainly in the hundreds of thousands.

我不知道 Paulo 从哪里得到这个数字。在基于 Windows NT 的操作系统中,每个进程打开的文件句柄数量基本上受到物理内存的限制——它肯定是数十万。

回答by Paulo Santos

Yes, there is a limit.

是的,有一个限制。

The limit depends on the OS, and memory available.

限制取决于操作系统和可用内存。

In the old D.O.S. the limit was 255 simultaneuously opened files.

在旧的 DOS 中,限制是 255 个同时打开的文件。

In Windows XP, the limit is higher (I believe it's 2,048 as stated by MSDN).

在 Windows XP 中,限制更高(我相信它是MSDN所述的 2,048 )。

回答by Thorsten Sch?ning

Came across the same problem, but using Embarcadero C++-Builder of RAD Studio 10.2. The C-runtime of that thing doesn't seem to provide _getmaxstdioor _setmaxstdio, but some macrosand their default limit is much lower than what is said here for other runtimes:

遇到了同样的问题,但使用 RAD Studio 10.2 的 Embarcadero C++-Builder。那个东西的 C 运行时似乎没有提供_getmaxstdioor _setmaxstdio,但是一些和它们的默认限制比这里对其他运行时所说的要低得多:

stdio.h:

stdio.h:

/* Number of files that can be open simultaneously
*/
#if defined(__STDC__)
#define FOPEN_MAX (_NFILE_)
#else
#define FOPEN_MAX (_NFILE_)
#define SYS_OPEN  (_NFILE_)
#endif

_nfile.h:

_nfile.h:

#if defined(_WIN64)
#define _NFILE_ 512
#else
#define _NFILE_ 50
#endif