Linux网络编程--文件描述符

文件描述符

在Unix和Unix-like操作系统中,文件描述符(file descriptor, FD)是一个文件或者像pipe或者network socket等之类的输入/输出源的唯一标识。
文件描述符通常是一个非负整数,负数通常代表无值或者错误。
文件描述符是POSIX API的一部分。每个除deamons之外的进程都有三个标准的POSIX文件描述符,对应三个标准流:

Integer valueName<unistd.h> symbolic constant<stdio.h> file stream
0Standard inputSTDIN_FILENOstdin
1Standard outputSTDOUT_FILENOstdout
2Standard errorSTDERR_FILENOstderr

概述

在Unix传统实现中,文件描述符索引到每个进程文件描述符表中,该表有内核维护,并索引到所有进程打开的系统范围的文件表中。文件表中记录有文件或其他资源打开的模式如读取、写入、追加以及其他可能模式。它还索引到称之为inode表的第三张表,inode表描述了实际的底层文件。
为了执行输入或输出,进程通过系统调用将文件描述符传给内核,内核将代表进程访问文件,进行不能直接访问文件或inode表。
在Linux中,进程打开的文件描述符可以通过/proc/PID/fd/路径来查看,其中PID表示进程id.
在Unix-like系统中,文件描述符可以指文件系统中命名的文件类型。除了常规文件,它还包括目录、块、字符设备、Unix域套接字和命名管道。文件描述符还可以指在文件系统中通常不存在的对象,如匿名管道和网络套接字。
C标准I/O库中的FILE数据结构通常包括类 Unix 系统上所讨论对象的低级文件描述符。整个数据结构提供了额外的抽象,被称为文件句柄。

对文件描述符的操作

下面列出在现代Unix-like系统中对文件描述符的典型操作。绝大多数函数在<unistd.h>头文件中声明,但也有一些在<fcntl.h>中

  • 创建

    • open()
    • creat()
    • socket()
    • accept()
    • socketpair()
    • pipe()
    • epoll_create() (Linux)
    • signalfd() (Linux)
    • eventfd() (Linux)
    • timerfd_create() (Linux)
    • memfd_create() (Linux)
    • userfaultfd() (Linux)
    • fanotify_init() (Linux)
    • inotify_init() (Linux)
    • clone() (with flag CLONE_PIDFD, Linux)
    • pidfd_open() (Linux)
    • open_by_handle_at() (Linux)
  • 导出

    • dirfd()
    • fileno()
  • 单个文件描述的操作

    • read(), write()
    • readv(), writev()
    • pread(), pwrite()
    • recv(), send()
    • recvfrom(), sendto()
    • recvmsg(), sendmsg() (also used for sending FDs to other processes over a Unix domain socket)
    • recvmmsg(), sendmmsg()
    • lseek(), llseek()
    • fstat()
    • fstatvfs()
    • fchmod()
    • fchown()
    • ftruncate()
    • fsync()
    • fdatasync()
    • fdopendir()
    • fgetxattr(), fsetxattr() (Linux)
    • flistxattr(), fremovexattr() (Linux)
    • statx (Linux)
    • setns (Linux)
    • vmsplice() (Linux)
    • pidfd_send_signal() (Linux)
    • waitid() (with P_PIDFD ID type, Linux)
    • fdopen() (stdio function:converts file descriptor to FILE*)
    • dprintf() (stdio function: prints to file descriptor)
  • Operations on multiple file descriptors

    • select(), pselect()
    • poll(), ppoll()
    • epoll_wait(), epoll_pwait(), epoll_pwait2() (Linux, takes a single epoll filedescriptor to wait on many other file descriptors)
    • epoll_ctl() (for Linux)
    • kqueue() (for BSD-based systems).
    • sendfile()
    • splice(), tee() (for Linux)
    • copy_file_range() (for Linux)
    • close_range() (for Linux)
  • Operations on the file descriptor table

    fcntl()函数根据传入的命令参数,可以对文件描述执行不同的操作。这行命令获取/设置文件描述符关联的属性,包括F_GETFD, F_SETFD, F_GETFL and F_SETFL.

    • close()
    • closefrom() (BSD and Solaris only; deletes all file descriptors greater than or equal to specified number)
    • dup() (duplicates an existing file descriptor guaranteeing to be the lowest number available file descriptor)
    • dup2(), dup3() (Close fd1 if necessary, and make file descriptor fd1 point to the open file of fd2)
    • fcntl (F_DUPFD)
  • Operations that modify process state

    • fchdir() (sets the process’s current working directory based on a directory file descriptor)
    • mmap() (maps ranges of a file into the process’s address space)
  • File locking

    • flock()
    • fcntl() (F_GETLK, F_SETLK and F_SETLKW)
    • lockf()
  • Sockets
    See also: Berkeley sockets

    • connect()
    • bind()
    • listen()
    • accept() (creates a new file descriptor for an incoming connection)
    • getsockname()
    • getpeername()
    • getsockopt()
    • setsockopt()
    • shutdown() (shuts down one or both halves of a full duplex connection)
  • Miscellaneous

    • ioctl() (a large collection of miscellaneous operations on a single file descriptor, often associated with a device)

Upcoming operations

A series of new operations on file descriptors has been added to many modern Unix-like systems, as well as numerous C libraries, to be standardized in a future version of POSIX. The at suffix signifies that the function takes an additional first argument supplying a file descriptor from which relative paths are resolved, the forms lacking the at suffix thus becoming equivalent to passing a file descriptor corresponding to the current working directory. The purpose of these new operations is to defend against a certain class of TOCTOU attacks.

  • openat()
  • faccessat()
  • fchmodat()
  • fchownat()
  • fstatat()
  • futimesat()
  • linkat()
  • mkdirat()
  • mknodat()
  • readlinkat()
  • renameat()
  • symlinkat()
  • unlinkat()
  • mkfifoat()
  • fdopendir()

File descriptors as capabilities

Unix file descriptors behave in many ways as capabilities. They can be passed between processes across Unix domain sockets using the sendmsg() system call. Note, however, that what is actually passed is a reference to an “open file description” that has mutable state (the file offset, and the file status and access flags). This complicates the secure use of file descriptors as capabilities, since when programs share access to the same open file description, they can interfere with each other’s use of it by changing its offset or whether it is blocking or non-blocking, for example.In operating systems that are specifically designed as capability systems, there is very rarely any mutable state associated with a capability itself.

A Unix process’ file descriptor table is an example of a C-list.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/508631.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

深信服 linux软件开发面试题整理

1、结构体可以进行比较 int memcmp ( const void * ptr1, const void * ptr2, size_t num ); Compare two blocks of memory Compares the first num bytes of the block of memory pointed by ptr1 to the first num bytes pointed by ptr2, returning zero if they all match…

大端小端模式判断以及数据转换

简介 在计算机系统中&#xff0c;我们是以字节为单位的&#xff0c;每个地址单元都对应着一个字节&#xff0c;一个字节为 8bit。但是在C语言中除了8bit的char之外&#xff0c;还有16bit的short型&#xff0c;32bit的long型&#xff08;要看具体的编译器&#xff09;&#xff…

MSYS2下搭建Qt开发环境

最近随意浏览了一下俺们大省会城市的招聘信息&#xff0c;发现C招聘中涉及Qt经验的要求有不少&#xff0c;为了牛奶和面包&#xff0c;决心深入一下Qt开发。本篇文章由此而出。 Qt 关于Qt的人生经历在这不在累赘&#xff0c;资料随处可得&#xff0c;这里只记录干货。 环境搭…

CentOS7开发环境搭建(1)

文章目录BIOS开启VT支持U盘安装系统(2019-03-11)CentOS DNS配置CentOS网络配置配置静态IP克隆虚拟机网卡名称变更 CentOS6.5时间配置安装VMWare-tools用户管理 (2019-03-15 7.6.1810)给一般账号 root 权限Samba服务配置安装必备软件获取本机公网ipyum源和第三方库源管理配置本地…

ACM 欧拉公式

给出一个数X&#xff0c;求小于X的与X互质的数的个数&#xff0c;使用欧拉公式。 如果x1*x2*...*xnX,则个数nX*(1-1/x1)*(1-/x2)*... 使用这个的题目&#xff0c;超典型 相遇周期(HDOJ)

HDU 1495 非常可乐(BFS)

思路 最难在于想到这道题是BFS&#xff0c;想到之后只有六种情况就很好理解了。 代码 #include<stdio.h> #include<string.h> #include<math.h> #include<queue> using namespace std; int a,b,s; struct shui {int count;int ha,hb,hs; }t,t1; int m…

NBU计算机专业期末考试记录

考试科目&#xff1a;操作系统 软件工程 数据库 计算机网络 JAVA高级应用 汇编 计算机算法设计 操作系统&#xff1a;题目比较简单&#xff0c;这学期的大题有写读写互斥的代码、求平均磁道数、银行家算法、页面调度算法的缺页次数计算。期中考试有参考价值&#xff0c;要看懂…

蚁群算法的若干记录

1、蚁群算法的特点&#xff1a; ① 结合了分布式算法、正反馈机制、贪婪式搜索的算法&#xff1a;正反馈可以快速发现较优解、分布式算法避免早熟收敛、贪婪式搜索有助于早期找出可解决方案&#xff1b; ② 蚁群算法具有很强的并行性&#xff1b; ③ 个体之间通过信息素合作…

蚁群算法之二

1、蚂蚁系统模型的建立 给定G(V,A)&#xff0c;其中V为定点集&#xff0c;A为各顶点互相连接组成的边集,已知各顶点之间的连接距离&#xff0c;要求确定一条长度最短的回路&#xff0c;仅遍历一次所有顶点的回路。引入记号&#xff1a; m&#xff1a;蚁群中蚂蚁的数量&#x…

ns2相关学习——tcl脚本编写(1)

新建一个仿真实例&#xff1a; set ns [new Simulator]为了让nam文件和trace文件有地方可以依托&#xff0c;我们要打开.nam文件进行写入&#xff0c;并且使用句柄nf set nf [open out.nam w] $ns namtrace-all $nf设置拓扑图 1、设置节点的脚本语言&#xff1a;建了两个节点&…

ns2相关学习——TCL脚本编写(2)

下面来学习更加复杂一点的TCL脚本的编写 简述&#xff1a;建立有4个节点的拓扑&#xff0c;其中一个节点作为路由器&#xff0c;用来将两个节点发出的数据包转发到第四个节点上面。 在这里将解释将两个节点的数据流区分开来的方法&#xff0c;展示如何去检测一个队列是否是满…

ns2相关学习——TCL脚本编写(3)

在这里我们将学习动态网络的建立 1、建立拓扑 当节点很多的时候&#xff0c;我们可以使用循环的方式来建立拓扑。 for {set i 0} {$i < 7} {incr i} {set n($i) [$ns node] }这里的数组不需要事先声明。 2、建立链接 这里我们要把7个节点链成一个环儿&#xff0c;同样使用…

NS2相关学习——完成一个新的协议(1)

接下来要进入对我来说老大难的环节了&#xff0c;从表面的TCL慢慢进入到后端的C&#xff0c;一起加油学习吧~ 在本节学习中&#xff0c;将给出一个在ns中实现新的协议的例子。但是可以想见的是&#xff0c;如果由我们自己来完成这个工作&#xff0c;势必要对NS2十分的熟悉并且要…

NS2相关学习——完成一个新协议(2)

在上节中&#xff0c;我们把教程要求的3.1-3.3过了一遍&#xff0c;这一次回到正途上来。看看到底是怎么完成一个新的协议的。 本节中的代码实现了一些简单的“ping”协议&#xff08;灵感来自“ns注释和文档”&#xff08;现在更名为ns手册&#xff09;的第9.6章中的“ping请…

NS2相关学习——完成一个新协议(3)

在前面已经基本学习了怎么完成一个新协议&#xff08;一个神奇的ping协议&#xff0c;然鹅还是有点懵。。。&#xff09; 接下来继续学习相关知识 接着上一部分从1开始 1、必要的修改 如果想要添加添加新的代理程序&#xff0c;就需要修改NS源文件中的内容&#xff0c;特别…

NS2相关学习——创建Xgraph的输出文件

经过前面学习代码的编写&#xff0c;这一部分&#xff0c;我们要学会如何进行分析&#xff0c;一个很直观的方式就是将结果图形化表示出来。 ns-allinone包的一部分是“xgraph”&#xff0c;一个绘图程序&#xff0c;可用于创建模拟结果的图形表示。 在本节中&#xff0c;将向…

NS2相关学习——在ns中模拟无线场景

之前学习的都是有线场景下的NS2相关应用&#xff0c;现在开始&#xff0c;终于要学习无线啦&#xff01;无线是我研究的重点&#xff0c;要好好学习呀&#xff01;在本节中&#xff0c;我们将学习使用ns中提供的移动无线仿真模型。 该部分由两部分组成。 在第一小节中&#xff…

An Energy-Efficient Ant-Based Routing Algorithm for Wireless Sensor Networks (无线传感网中一种基于蚁群算法的能量有效路由)

牙说&#xff1a;这篇论文是研究蚁群算法在能量有效路由协议的过程中必读的一篇文章&#xff0c;原是全英文&#xff0c;在这里按照自己的理解大致翻译成中文&#xff0c;好好学习&#xff0c;与君共勉。 论文题目&#xff1a;An Energy-Efficient Ant-Based Routing Algorith…

活在幻梦中的你我

其实仔细想想,人类和地球上的其它物种有什么不同呢?可能仅有的不同是,人类会去相信那本来并不存在的事情. 并且会为了那种虚幻的东西为止拼搏、努力。比如科技的发展&#xff0c;不就是人类在实现自己想象中的事物么&#xff0c;飞机、轮船、家电、计算机等等&#xff0c;无一…

An Energy-Efficient Ant-Based Routing Algorithm for Wireless Sensor Networks (无线传感网中基于蚁群算法的能量有效路由)2

牙说&#xff1a;接着上一篇继续写。论文标题&#xff1a;An Energy-Efficient Ant-Based Routing Algorithm forWireless Sensor Networks作者&#xff1a;Tiago Camilo, Carlos Carreto, Jorge S Silva, Fernando Boavida正文&#xff1a; 2、相关工作可以考虑无线传感器网络…