nr_open , file_max & file_nr区别

1 nr_open

This denotes the maximum number of file-handles a process can allocate.
Default value is 1024*1024 (1048576) which should be
enough for most machines.
Actual limit depends on RLIMIT_NOFILE
resource limit.

  • 单个进程打开文件句柄数上限

2 file-max & file-nr

file-max:
The value in file-max denotes the maximum number of file-
handles that the Linux kernel will allocate.
When you get lots of error messages about running out of file handles,
you might want to increase this limit.
Historically,the kernel was able to allocate file handles
dynamically, but not to free them again.
file-nr:
The three values in file-nr denote the number of allocated file handles,
the number of allocated but unused file handles, and the maximum number of
file handles.
Linux 2.6 always reports 0 as the number of free
file handles – this is not an error, it just means that the
number of allocated file handles exactly matches the number of used file handles.
Attempts to allocate more file descriptors than file-max
are reported with printk, look for “VFS: file-max limit reached”.


  • file-max中指定了系统范围内所有进程可打开的文件句柄的数量限制(系统级别, kernel-level)。
  • file-max一般为内存大小(KB)的10%来计算,如果使用shell,可以这样计算:

grep MemTotal /proc/meminfo | awk ‘{printf(“%d”,$2/10)}’
410556 # 本人主机测算的数据
cat /proc/sys/fs/file-max
406262

  • 我们不需要主动设置这个值,除非这个值确实较小。
    (可能有各种其他原因导致file-max没有设置为内存的10%)

  • 注意:

    If you increase /proc/sys/fs/file-max,
    be sure to increase /proc/sys/fs/inode-max to 3-4
    times the new value of /proc/sys/fs/file-max,
    or you will run out of inodes.
    即修改file-max,最好也要修改inode-max
    inode-max 约等于 3或4 倍file-max。

  • 3 ulimit

    Provides control over the resources available to the shell and to processes started by it,
    on systems that allow such control.

    • 提供对shell及其启动的进程的可用资源(包括文件句柄, 进程数量, core文件大小等)的控制。
    • 这是进程级别的, 也就是说系统中某个session及其启动的每个进程能打开多少个文件描述符,
      能fork出多少个子进程等… 。
    ulimit -n
    1024
    这表示当前用户的每个进程最多允许同时打开1024个文件。
    这1024个文件中还得除去每个进程必然打开的标准输入,
    标准输出,标准错误,服务器监听 socket,进程间通讯的unix域socket等文件,
    那么剩下的可用于客户端socket连接的文件数就只有大概1024-10=1014个左右。
    也就是说缺省情况下,基于Linux的通讯程序最多
    允许同时1014个TCP并发连接。

    参考文献:
    [1] https://www.kernel.org/doc/Documentation/sysctl/fs.txt
    [2] http://blog.csdn.net/guowake/article/details/6615728 博主:guowake
    [3] http://www.cnblogs.com/zengkefu/p/5635153.html 作者:zengkefu

在你的代码中,有几个问题需要注意一下: 1. 在 `read()` 函数中,你需要在每次循环前检查是否已经到达文件末尾,可以使用 `feof(fp)` 函数来判断。 2. 在 `write()` 函数中,你用了一个未初始化的变量 `nr`,应该先给它赋一个初值。 3. 在 `write()` 函数中,你将打开的文件指针命名为 `fp1`,但是在写文件时却使用了未初始化的 `fp` 指针。 4. 在 `main()` 函数中,你的文件读取循环条件错误,应该是 `while(!feof(fp))`。 下面是修改后的代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 100 FILE *fp; //文件指针 void write(char bt[MAX]) { char nr = '\0'; //初始化为 0 FILE *fp1; fp1 = fopen(bt, "a"); //本地文件查找"max+.txt"文件,以追加方式打开 for (int i = 0; i < MAX; i++) { int j = 0; //"="个数 if (nr == '=') j++; else j = 0; fread(&nr, 1, 1, fp); //一次读一个字符 进字符数组 fwrite(&nr, 1, 1, fp1); //读一个,写一个 if (j == 9) { fclose(fp1); break; //连续10个"=",跳出循环 } } } void read(void) { char bt[MAX] = {0}; //临时 标题 for (int i = 0; i < MAX; i++) { if (feof(fp)) //到达文件末尾 { fclose(fp); fp = NULL; break; } fread(&bt[i], sizeof(char), 1, fp); //一次读一个字符 进字符数组 if (bt[i - 1] == '-' && bt[i] == '\0') //判断是否读完标题 { bt[i - 1] = '.'; bt[i] = bt[i + 2] = 't'; bt[i + 1] = 'x'; write(bt); } } } int main(void) { char filename[] = "My Clippings.txt"; if ((fp = fopen(filename, "r")) == NULL) //以只读方式,查看能否打开文件 { printf("can not open the files\n"); system("pause"); return 0; } do { read(); } while (fp != NULL); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值