linux fd

1,设置最大的fd数量

2,设置单个进程的最大fd数量


1fd的数量决定了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.itpub.net/90618/viewspace-772571/


2,单个进程的最大fd

limit -n可以查看当前的单个进程最大fd


ulimit -HSn 65536用来设置临时值


/etc/security/limits.conf文件里面添加

* soft nofile 8192
* hard nofile 8192

重启生效。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值