文件描述符

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

Integer value Name <unistd.h> symbolic constant <stdio.h> file stream
0 Standard input STDIN_FILENO stdin
1 Standard output STDOUT_FILENO stdout
2 Standard error STDERR_FILENO stderr

概述

在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.

Linux网络编程--文件描述符相关推荐

  1. Linux C:文件描述符、IO重定向、恢复标准输入输出

    目录 一.文件描述符 二.IO重定向 三.重定向回终端.伪终端 四.恢复标准输入输出 一.文件描述符 在Linux中,文件描述符是一个非负整数的数据类型.是FILE结构体中的一个成员属性. 每打开或者 ...

  2. linux文件描述符有什么用,linux上的文件描述符3有什么特别之处?

    我的工作,那将在Linux和Mac OS X上运行的服务器应用程序它是这样的:linux上的文件描述符3有什么特别之处? 启动主要应用 控制器进程的叉 调用lock_down()在控制过程中 再次叉终 ...

  3. Linux下利用文件描述符恢复的成功失败实验

    数据误删除是作为初级运维人员常常遇到的"低级错误",一些有经验的老手有时也在疲劳.不冷静的情况下"马失前蹄".一旦误删除数据文件,尽快采用影响最小.最迅速的手段 ...

  4. Linux下的文件描述符

    引文 在 Linux 的世界里,一切设备皆文件.对文件的操作都是通过文件描述符(fd)来进行的. Linux 中有7种文件类型: 文件类型 文件类型描述 符号 普通文件 最常使用的一类文件,其特点是不 ...

  5. Linux中的文件描述符与打开文件之间的关系

    1. 概述 在Linux系统中一切皆可以看成是文件,文件又可分为:普通文件.目录文件.链接文件和设备文件.文件描述符(file descriptor)是内核为了高效管理已被打开的文件所创建的索引,其是 ...

  6. Linux中对文件描述符的操作(FD_ZERO、FD_SET、FD_CLR、FD_ISSET

    在Linux中,内核利用文件描述符(File Descriptor)即文件句柄,来访问文件.文件描述符是非负整数.打开现存文件或新建文件时,内核会返回一个文件描述符.读写文件也需要使用文件描述符来指定 ...

  7. linux exec操作文件描述符

    linux每一个打开文件都会关联一个文件描述符,需要的时候我们可以使用exec命令指定一个大于3的数字作为文件 linux默认文件描述符 每打开一个shell就会打开默认的三个文件描述符描0,1,2, ...

  8. Linux——什么是文件描述符

    目录 前文 一,为什么有文件描述符 二,什么是文件描述符 2.1 文件操作接口 2.2 文件描述符 三,文件描述符的原理 四,文件描述符的分配规则 前文 本文主要是详解一下文件描述符,我们从1.为什么 ...

  9. Linux最大打开文件描述符数

    1.    系统最大打开文件描述符数:/proc/sys/fs/file-max a.    查看 $ cat /proc/sys/fs/file-max 186405 2. 设置 a.    临时性 ...

最新文章

  1. 死磕 java集合之TreeMap源码分析(二)- 内含红黑树分析全过程
  2. 怎样利用好单片机上的存储器资源来实现OD的存储与访问
  3. xampp php连接mysql数据库_关于PHP xampp连接数据库之后查询失败,数据库连接成功,表中也有数据...
  4. 第六十二节,html分组元素
  5. Scala程序将字符串转换为整数
  6. 华为手机芯片断供,有没有可能回收旧手机解决目前困境?
  7. mysql 统计 1的数量_利用MySQL统计一列中不同值的数量方法示例
  8. HSmartWindowControl 之 显示图像
  9. (转)如何在MySql中记录SQL日志(例如Sql Server Profiler)
  10. 在github上实现页面托管预览功能
  11. 别嘲笑老同志了!网络诈骗,19岁小鲜肉最容易中招
  12. Scratch3.0学习视频链接
  13. 2022最新Web前端经典面试试题及答案-史上最全前端面试题(含答案)
  14. Android Verified Boot dm-verity 优化和实战2
  15. HE4484E泛海微5V USB 输入双节锂电池串联应用升压充电IC管理芯片
  16. Microsemi Libero系列教程(六)——Identify片上逻辑分析仪的使用
  17. diy一个android手机版下载,居然设计家DIY手机版下载-居然设计家DIY 安卓版v1.3.0.5-PC6安卓网...
  18. Web 动画帧率(FPS)计算
  19. 疫苗预约系统,疫苗接种管理系统,疫苗预约管理系统毕设作品
  20. 华为p10关闭更新_华为p10系统更新,华为emui5.0能否手撕emui4.1

热门文章

  1. gensim提取一个句子的关键词_聊一聊 NLPer 如何做关键词抽取
  2. php 没有libmysql.dll,PHP5.3以上版本没有libmysql.dll,以及由此带来的困扰
  3. vscode编辑python_VSCode+Python开发环境
  4. 地面控制点的定义与作用_什么是地面塌陷
  5. python自定义全局异常_如何在python中进行全局异常捕获
  6. 服务器的虚拟内存怎么调,服务器的虚拟内存怎么调才好
  7. 抗侧力构件弹性位移如何计算_说一说现在很火的装配式建筑怎么计算?
  8. android 截图 listview,Android屏幕及view的截图实例详解
  9. 干货丨总结5类面试官特点和应对方法
  10. python 栈和队列_python 栈和队列的基本实现