如何增加和QUOT的限制;最大打开文件"在Mac OS XÇ [英] How to increase the limit of "maximum open files" in C on Mac OS X

查看:191
本文介绍了如何增加和QUOT的限制;最大打开文件"在Mac OS XÇ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mac OS X上的最大打开文件的缺省限制为256(-n的ulimit)和我的应用程序需要约400文件处理程序。

The default limit for the max open files on Mac OS X is 256 (ulimit -n) and my application needs about 400 file handlers.

我试图改变通过setrlimit(上限),但即使函数正确执行,我仍然限制为256个。

I tried to change the limit with setrlimit() but even if the function executes correctly, i'm still limited to 256.

下面是测试程序使用:

#include <stdio.h>
#include <sys/resource.h>

main()
{
  struct rlimit rlp;

  FILE *fp[10000];
  int i;

  getrlimit(RLIMIT_NOFILE, &rlp);
  printf("before %d %d\n", rlp.rlim_cur, rlp.rlim_max);

  rlp.rlim_cur = 10000;
  setrlimit(RLIMIT_NOFILE, &rlp);

  getrlimit(RLIMIT_NOFILE, &rlp);
  printf("after %d %d\n", rlp.rlim_cur, rlp.rlim_max);

  for(i=0;i<10000;i++) {
    fp[i] = fopen("a.out", "r");
    if(fp[i]==0) { printf("failed after %d\n", i); break; }
  }

}

和的输出是:

before 256 -1
after 10000 -1
failed after 253

我不能要求谁用我的应用程序到的/ etc文件或里面的东西捅人。我需要的应用程序本身来做到这一点。

I cannot ask the people who use my application to poke inside a /etc file or something. I need the application to do it by itself.

推荐答案

etresoft发现了苹果答案讨论板

etresoft found the answer on the apple discussion board:

整个问题这里是你的
  printf()函数。当您打电话
  printf()的,要初始化
  内部数据结构在一定
  尺寸。然后,您调用了setrlimit()来
  尝试调整这些大小。那
  因为你有函数失败
  已经使用那些内部
  与结构的printf()。如果你
  使用两个RLIMIT结构(一个用于
  前一后进行),也不要
  打印,直到打完电话后
  了setrlimit,你会发现,你可以
  改变当前的限制
  即使在一个命令行处理
  程序。最大值为10240。

The whole problem here is your printf() function. When you call printf(), you are initializing internal data structures to a certain size. Then, you call setrlimit() to try to adjust those sizes. That function fails because you have already been using those internal structures with your printf(). If you use two rlimit structures (one for before and one for after), and don't print them until after calling setrlimit, you will find that you can change the limits of the current process even in a command line program. The maximum value is 10240.

这篇关于如何增加和QUOT的限制;最大打开文件&QUOT;在Mac OS XÇ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆