fd最大值和限制

fd的数量决定了fd的最大值

 

在Linux下,系统全部能够打开的fd总数为:

/proc/sys/fs/file-max,取决于内存

The file-max file /proc/sys/fs/file-max sets the maximum number of file-handles that the Linux kernel will allocate. We generally tune this file to improve the number of open files by increasing the value of /proc/sys/fs/file-max to something reasonable like 256 for every 4M of RAM we have: i.e. for a machine with 128 MB of RAM, set it to 8192 - 128/4=32 32*256=8192.

/proc/sys/fs/file-nr 记录系统中fd的使用情况,已分配文件句柄的数目 
已使用文件句柄的数目 
文件句柄的最大数目 ,

单个进程能够打开的最大fd数量为 ulimit -n, 可以通过sysconf(_SC_OPEN_MAX)获取默认的进程fd打开数量。

修改fd限制可以先修改shell的ulimit -n,

或者通过setrlimit函数进行修改:

void modifyfdlimit()
{
 rlimit fdLimit;

 fdLimit.rlim_cur = 30000;
 fdLimit.rlim_max = 30000;

 if (-1 == setrlimit (RLIMIT_NOFILE, &fdLimit))
 {
  printf ("Set max fd open count fai. /nl");

  char cmdBuffer [64];
  sprintf (cmdBuffer, "ulimit -n %d", 30000);

  if (-1 == system (cmdBuffer))
  {
   printf("%s failed. /n", cmdBuffer);

   exit(0);
  }

  if (-1 == getrlimit (RLIMIT_NOFILE, &fdLimit))
  {
   printf("Ulimit fd number failed.");

   exit(0);
  }
 }

 //printf("Hard limit: %d. Soft limit: %d", fdLimit.rlim_max, fdLimit.rlim_cur);
}

 

http://blog.csdn.net/pmunix/article/details/2416498

 

posted @   seasonzone  阅读(1764)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
点击右上角即可分享
微信分享提示