简体   繁体   English

获取打开文件限制

[英]Getting the open file limit

Is it possible to portably access the maximum number of files that can be simultaneously open within the current process, in C++ (eg the result of ulimit -n on unix systems)? 是否可以在C ++中可移植地访问当前进程中可以同时打开的最大文件数(例如,Unix系统上ulimit -n的结果)? I'm writing an application in C++ that potentially requires hundreds of open files, often more than the systems default maximum. 我正在用C ++编写一个应用程序,该应用程序可能需要数百个打开的文件,通常超过系统默认的最大值。 Going over the maximum open file limit generally causes difficult to diagnose crashes, especially as most users are not aware of the limit. 超过最大打开文件限制通常会导致崩溃难以诊断,尤其是当大多数用户不知道该限制时。 If I knew the limit at startup, I could potentially warn the user of potential issues, or give a better error message when things go wrong. 如果我知道启动时的限制,则可能会警告用户潜在的问题,或者在出现问题时给出更好的错误消息。

In POSIX-compatible systems (which I think includes Windows): 在兼容POSIX的系统(我认为包括Windows)中:

#include <sys/resource.h>

struct rlimit lim;
getrlimit(RLIMIT_NOFILE, &lim);
rlim_t max_files = lim.rlim_cur;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM