+ -
当前位置:首页 → 问答吧 → 请教fopen能够同时打开的文件数上限

请教fopen能够同时打开的文件数上限

时间:2010-07-30

来源:互联网

我想知道在Linux、HP-Unix、Aix、Solaris等平台下,常见编译器实现的fopen能够同时打开的文件数上限。在相应的平台上,是否有系统设置或者编译器提供的函数可以被用来调整这个上限值。

比如,Windows下VC9实现的fopen默认能够最大打开512个文件,但可以通过_setmaxstdio函数设置其上限达到2048。对于其它平台及其上的编译器,由于没有相关平台可供测试,也没有相关的经验,故请教大家。

下面有一段测试代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>

  4. using namespace std;

  5. int main()
  6. {
  7.     int count = 0;
  8.    
  9.     for (;;)
  10.     {
  11.         char buf[256];
  12.         sprintf(buf, "logs\\log_%d.txt", ++count);
  13.         FILE *pFile = fopen(buf, "a+");
  14.         if (pFile == NULL)
  15.         {
  16.             cout << "Failed to create the file \"" << buf << "\"\n";
  17.             cout << "Error msg: " << strerror(errno) << endl;
  18.             break;
  19.         }
  20.         else
  21.         {
  22.             fprintf(pFile, "a line for test\n");
  23.             cout << "Created a file, No." << count << endl;
  24.         }
  25.     }
  26. }
复制代码

作者: tyc611   发布时间: 2010-07-30

ulimit -n

作者: hellioncu   发布时间: 2010-07-30

回复 tyc611
  1. #include <sys/wait.h>
  2. #include <sys/types.h>
  3. #include <sys/param.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <errno.h>
  9. #ifdef OPEN_MAX
  10. static long openmax = OPEN_MAX;
  11. #else
  12. static long openmax = 0;
  13. #endif
  14. #define OPEN_MAX_GUESS 1024
  15. int main( int argc, char *argv[] )
  16. {
  17.   if( openmax == 0 ) {                // Run sysconf to determine the value
  18.     errno = 0;
  19.     if( ( openmax = sysconf( _SC_OPEN_MAX ) ) < 0 ) {
  20.       if( errno == 0 ) {
  21.         openmax = OPEN_MAX_GUESS;
  22.       } else
  23.         printf( "sysconf error" );
  24.     }
  25.   }
  26.   printf( "%ld\n", openmax );
  27.   return 0;
  28. }
复制代码

作者: churchmice   发布时间: 2010-07-30

相关阅读 更多