文件IO-文件描述符

文件描述符

1.文件描述符的描述

a  file descriptor, a small, nonnegative integer for use in 
subsequent system calls (read(2), write(2), lseek(2), fcntl(2), etc.)           
 --from man 2.6.3

关键词:
nonnegative integer 非负整数
use in system calls 系统调用使用

当打开或者创建一个新的文件时,内核向进程返回一个文件描述符
用此文件描述符就能读写文件
linux一切皆文件,拿到文件描述符,就拿到了linux的钥匙

2.标准文件描述符

标准输出文件描述符、标准输入文件描述符、标准错误文件描述符

/* Standard file descriptors.  */
#define STDIN_FILENO    0       /* Standard input.  */
#define STDOUT_FILENO   1       /* Standard output.  */
#define STDERR_FILENO   2       /* Standard error output.  */

---from <unistd.h>

3.文件描述符的限制

使用ulimit -n 进行文件描述符设置当前进程

[para ~]$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7271
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 65535           --> 文件描述符限制 65535个
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 4096
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

设置永久生效最大文件描述符限制

修改 /etc/security/limits.conf

#End of file
root soft nofile 65535
root hard nofile 65535
para soft nofile 1024
para hard nofile 1024
* soft nofile 65535
* hard nofile 65535

[para ~]$ ulimit -n
1024

4.文件描述符唯一性

进程PCB中维护一个文件描述符表,该表的索引值从0开始
每个进程都有自己的PCB块,当然也有属于自己的文件描述符表
在这里插入图片描述
此图由unix环境高级编程书籍而来,网上说 linux系统只使用i节点,而不使用v节点 ,知道方法再细研究此问题。

(1)进程拿到文件描述符fd --> 通过文件描述符表拿到对应文件描述符指针
(2)通过指针找到文件表的偏移量,再通过文件偏移找到当前文件指针的位置
(3)在通过i节点信息去文件系统进行具体操作

5.进程结构图

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值