开发者

Question about C fopen function

开发者 https://www.devze.com 2022-12-19 16:49 出处:网络
While I was reading the reference about fopen function, I found out that FOPEN_MAX is a value that the \"minimum nu开发者_StackOverflow社区mber of streams that the implementation guarantees can be ope

While I was reading the reference about fopen function, I found out that FOPEN_MAX is a value that the "minimum nu开发者_StackOverflow社区mber of streams that the implementation guarantees can be open simultaneously".

Why it is the minimum number of streams? Doesn't it have to be "the maximum number of streams...." ?


Minimum number of streams that can be opened = guarantee that at least so many can be opened

Maximum number of streams that can be opened = guarantee that opening any more will certainly fail

What the wording means that if you have less than FOPEN_MAX streams open, it is guaranteed that at least one more can be opened, and the system does not necessarily provide any hard maximum


It sounds counterintuitive that something "MAX" is the minimum number that can be opened simultaneously. But if you're writing portable code, that number is the effective maximum that you can use safely.

Consider this code:

if (num_open >= FOPEN_MIN) {
    // close some old ones
}

Looks odd, right? So calling it FOPEN_MAX makes sense.


Poor wording in my opinion. It would be better to just say that the implementation guarantees that at least FOPEN_MAX streams may be open simultaneously. Note that if you try to open more streams, you may succeed, but it is not guaranteed in advance.


on linux you can adjust the maximum number of allowed open file handles.
See the command ulimit for more information:

For example on my box running as my user right now any process would have a max number of 256 file handles opened at any given time.

$ ulimit -n
256

see the man page for ulimit for more information( here is a web version): http://ss64.com/bash/ulimit.html

However, I imagine you can't lower it below FOPEN_MAX

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号