• 1. unix网络编程中的fd是什么

    • 1.1 fd全称是file descriptor,是进程独有的文件描述符表的索引
    • 1.2. 对文件描述符执行close操作时,仅仅是关闭了该进程对某文件的访问,其他进程依然能访问
  • 2. 访问文件需要通过系统调用,进程不能直接访问文件
  • 3. 可以通过/proc/PID/fd/查看PID对应的进程打开了哪些文件
  • 4. 类Unix系统中,文件描述符还可以指向其他io设备,比如网络连接套接字、管道等

1. unix网络编程中的fd是什么

1.1 fd全称是file descriptor,是进程独有的文件描述符表的索引

维基百科这样描述:

In the traditional implementation of Unix, file descriptors index into a per-process file descriptor table maintained by the kernel, that in turn indexes into a system-wide table of files opened by all processes, called the file table. This table records the mode with which the file (or other resource) has been opened: for reading, writing, appending, and possibly other modes. It also indexes into a third table called the inode table that describes the actual underlying files.[3]

图片原始连接

简单的说,就是内核为每个进程维护了一个file descriptor table,file descriptor是file descriptor table的索引,file descriptor table的表项又转而可以索引到系统级的file table,file table又可以索引到系统级的inode table,而这个inode table则真正的描述了底层的文件。系统级的file table还记录了每个文件被打开的方式:读、写、追加…。file descriptor table每个进程都有一个,所以fork的会被拷贝。

1.2. 对文件描述符执行close操作时,仅仅是关闭了该进程对某文件的访问,其他进程依然能访问

close的文档如下描述:

If fd is the last file descriptor referring to the underlying open file description (see open(2)), the resources associated with the open file description are freed; if the descriptor was the last reference to a file which has been removed using unlink(2) the file is deleted.

可以看到,只有该fd是指向某文件的最后一个fd时,文件对应资源才会被释放。

2. 访问文件需要通过系统调用,进程不能直接访问文件

To perform input or output, the process passes the file descriptor to the kernel through a system call, and the kernel will access the file on behalf of the process. The process does not have direct access to the file or inode tables.

3. 可以通过/proc/PID/fd/查看PID对应的进程打开了哪些文件

On Linux, the set of file descriptors open in a process can be accessed under the path /proc/PID/fd/, where PID is the process identifier.

4. 类Unix系统中,文件描述符还可以指向其他io设备,比如网络连接套接字、管道等

In Unix-like systems, file descriptors can refer to any Unix file type named in a file system. As well as regular files, this includes directories, block and character devices (also called “special files”), Unix domain sockets, and named pipes. File descriptors can also refer to other objects that do not normally exist in the file system, such as anonymous pipes and network sockets.

unix网络编程中的fd是什么相关推荐

  1. linux下网络编程设置非阻塞,UNIX网络编程 非阻塞connect的实现

    一.<UNIX网络编程>-非阻塞connect 在一个TCP套接口被设置为非阻塞之后调用connect,connect会立即返回EINPROGRESS错误,表示连接操作正在进行中,但是仍未 ...

  2. Unix网络编程unp.h问题以及Linux中的库

    Stevens的<UNIX网络编程 卷1:套接字连网API>是一本很著名的UNIX网络编程书籍.其中使用了一个unp.h的引用,如果没有设置相应的库的话,即使引入了头文件也是没用的.所以首 ...

  3. 网络编程中的关键问题总结

    网络编程中的关键问题总结 总结下网络编程中关键的细节问题,包含连接建立.连接断开.消息到达.发送消息等等: 连接建立 包括服务端接受 (accept) 新连接和客户端成功发起 (connect) 连接 ...

  4. UNIX网络编程学习笔记(代码超详细解析)(持续更新)

    1. 其他函数准备 1. TCP 回射服务器程序: str_echo 函数 #include "unp.h"void str_echo(int sockfd) {ssize_t n ...

  5. unix网络编程 str_cli epoll 非阻塞版本

    unix网络编程 str_cli epoll 非阻塞版本 unix网络编程str_cli使用epoll实现讲了使用epoll配合阻塞io来实现str_cli,这个版本是配合非阻塞io. 可以看到采用非 ...

  6. unix 网络编程总结

    from:http://middleware123.com/tuxedo/intro/303.html 1.处理SIGCHLD信号 当编写fork子进程处理连接的服务器程序时,子进程退出会给父进程产生 ...

  7. 深入分析网络编程中踩过的坑

    网络编程中经常会遇到一些异常的情况,定位问题需要了解协议栈的实现,以下是工作中遇到的一些常见问题的深入分析和解决思路. 问题1:server端业务进程响应心跳超时被监控进程kill,导致数据或者逻辑异 ...

  8. 《Unix网络编程卷1-套接字联网API》第一个例子编译 不通过问题解决

    <Unix网络编程卷1-套接字联网API>是本好书. 但是第一个例子不是很好编译. 需要如下步骤: 本人机器CentOS 5.4 1.下载源码 unpv13e解压到任意目录 然后按其rea ...

  9. Linux网络编程中的几组类似功能的区别

    1.bzero与memset char buff[1024]; memset(buff,0,sizeof(buff));bzero(buff, sizeof(buff)); struct sockad ...

  10. UNIX网络编程之旅-配置unp.h头文件环境

    最近在学习Unix网络编程(UNP),书中steven在处理网络编程时只用了一个#include "unp.h"  相当有个性并且也很便捷 于是我把第三版的源代码编译实现了这个过程 ...

最新文章

  1. Firefox做默认浏览器,点击QQ面板连接(QQ邮箱,空间),延迟很久很久才打开网页(Firefox 浏览器 延迟 打开 点击没反应)...
  2. C++继承时的名字遮蔽(二)
  3. oracle连接本地数据库
  4. 关于_vmvare workstation装32ubuntu的问题
  5. Linux设置RSA密钥登录
  6. 解决idea修改html、js、css后,浏览器不能同步加载
  7. 一步一步学Silverlight 2系列(8):使用样式封装控件观感
  8. 感知器模型为什么不能解决异或问题
  9. 可燃气体浓度多少合格_科普:气体容积单位LEL%、VOL%、TLV(ppm)之间如何换算?...
  10. 关于项目估算的微博讨论
  11. 客户贷款逾期预测[5] - 特征工程
  12. 中国跨境平台出海,产业带依然是最大优势
  13. 《Java黑皮书基础篇第10版》 第3章【习题】
  14. 艾伟:WCF从理论到实践(13):事务投票
  15. c++代替goto语句的方法
  16. python网络爬虫权威指南(第2版)pdf_用Python写网络爬虫(第2版) PDF 下载
  17. Mysql rbo和cbo_CBO和RBO介绍(好)
  18. synchronize底层原理
  19. 第八次作业—— 缺陷管理
  20. 媒体服务器保持唤醒状态,[已解决]向大家请教一下,正常的硬盘休眠日志是什么样的?...

热门文章

  1. termux python 打开摄像头_python+opencv 电脑调用手机的摄像头
  2. FinClip11月产品更新:FIDE 插件开发功能优化;文档中心捉“虫”竞赛火热开启
  3. springboot 整合 邮件发送
  4. Windows server 2008 r2 基于LLS的Web和FTP服务搭建(基本)
  5. Python IDLE的下载,安装和使用
  6. linux c 端口复用,Linux C++ 网络编程学习系列(1)——端口复用实现
  7. ESP32的SDK开发之获取SNTP网络时间
  8. 保险核保、理赔|门诊住院发票识别||医疗单据医疗票据识别技术
  9. java 反应堆模式_Java NIO 反应堆模式
  10. 智慧灯杆解决方案之智慧景区(园区)建设