文件描述符的规范 [英] Specification of file descriptors

查看:107
本文介绍了文件描述符的规范的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解文件描述符的标志和模式.

I am trying to understand flags and modes of file descriptors.

手册页

fcntl - manipulate file descriptor

int fcntl(int fd, int cmd);

状态:

文件描述符标志

以下命令操纵与文件关联的标志 描述符.当前,仅定义了一个这样的标志:FD_CLOEXEC,...

The following commands manipulate the flags associated with a file descriptor. Currently, only one such flag is defined: FD_CLOEXEC,...

文件状态标志

每个打开的文件描述都有某些关联的状态标志, 由open(2)初始化... 文件状态标志及其语义在 open(2).

Each open file description has certain associated status flags, initialized by open(2)... The file status flags and their semantics are described in open(2).

鉴于fcntl完全指代文件描述符(不处理流),我想第二个标题应该是文件描述符状态标志". 因此,现在我们有了FD的"标志"和"状态标志". 该手册页还提到,当cmd=F_GETFL时,fcntl的返回值是"文件访问模式文件状态标志". 因此,现在我们还有一种文件访问模式.

Given that fcntl refers entirely to file descriptors (no dealing with streams), I guess the second title should be "File descriptor status flags". So now we have for a FD "flags" and "status flags". This man page also mentions that when cmd=F_GETFL, the return value of fcntl is "the file access mode and the file status flags". So now we have also a file access mode.

现在在open 手册页中 有 flags modes ,就好像它们是两个不同的项目一样. 甚至有一个原型可以清楚地表明差异

Now in the man page for open there are flags and modes, as if they were two different items. There is even a prototype that makes explicit the difference

int open(const char *pathname, int flags, mode_t mode);

现在,对于每个文件描述符,我们都有"标志","状态标志","文件访问模式"和" modes "(我将后两者标识为相同). 首先,
1.我不知道这三个类别之间的区别.

So now we have, for each file descriptor, "flags", "status flags", "file access modes", and "modes" (I would identify the latter two as the same). To begin with,
1. I don't know the difference between these three categories.

遍历两个引用的手册页,我收集了一个实体"列表(下面,按照出现的顺序,有些是重复的).
2.我不知道每个人属于哪个类别.

Traversing both quoted man pages, I collected a list of "entities" (below, in order of appearance, some are repeated).
2. I don't know which category each belongs to.

FD_CLOEXEC, O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC, O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, O_NONBLOCK, O_DSYNC, O_SYNC, O_CLOEXEC
O_CREAT, O_DIRECTORY, O_EXCL, O_NOCTTY, O_NOFOLLOW, O_TMPFILE, O_TRUNC, O_LARGEFILE, O_NDELAY, O_PATH

我找不到一个简单的列表来告诉"X,Y,Z是标志,W,V是模式等". 也许它们是可以互换使用的术语,或者 mode flags 的子集,或者...

I couldn't find a simple list telling "X, Y, Z are flags, W, V are modes, etc." Perhaps they are terms that are used interchangeably, or the mode is a subset of the flags, or...

相关:

文件指针",流"之间的差异;,文件描述符"和...文件"?(答案可能在当前OP中有一个指南,即使不一样).

Difference between "file pointer", "stream", "file descriptor" and ... "file"? (answers there may be a guide in the present OP, even if not the same).

如何理解O_RDONLY = 0?

文件指针",流"之间的差异;,文件描述符"和...文件"?

如何获取文件描述符的模式?

https://www.gnu.org/software /libc/manual/html_node/Access-Modes.html

https://www.gnu.org/software/libc/manual/html_node/File-Status-Flags.html#File-Status-Flags

推荐答案

文件描述符可以重复.例如,当进程fork s时,它将获得其父级不受影响的自己的FD集,并且dup系统调用可用于显式复制单个FD.

File descriptors can be duplicated. For example, when a process forks, it gets its own set of FDs that the parent doesn't affect, and the dup syscall can be used to explicitly duplicate individual FDs.

文件描述符重复时,每个描述符都有其自己的文件描述符标志集,但它们都将共享相同的文件状态标志.例如,考虑以下代码:

When file descriptors get duplicated, every descriptor has its own set of file descriptor flags, but they'll all share the same file status flags. For example, consider this code:

int fdA = open('/tmp/somefile', O_WRONLY);
int fdB = dup(fdA);
fcntl(fdA, F_SETFD, FD_CLOEXEC);
fcntl(fdA, F_SETFL, O_APPEND);

运行它后,fdA将处于执行状态并处于附加模式,而fdB将处于附加模式但处于关闭状态.这是因为close-on-exec是文件描述符标志,而append模式是文件状态标志.

After running it, fdA will be close-on-exec and in append mode, and fdB will be in append mode but not close-on-exec. This is because close-on-exec is a file descriptor flag and append mode is a file status flag.

文件访问模式和文件创建标志与文件状态标志一起被支持.

The file access mode and file creation flags are passed along with the file status flags when they're supported.

open的第三个参数,也令人困惑地称为mode,与到目前为止讨论的所有其他内容均无关.如果文件是通过调用open创建的,则该mode用作新文件的权限.否则,它将无效.

The third parameter to open, also confusingly called mode, is unrelated to everything else discussed so far. If the file is created by the call to open, then that mode is used as the permissions for the new file. Otherwise, it has no effect.

  • FD_CLOEXEC-文件描述符标志
  • O_RDONLY-文件访问模式
  • O_WRONLY-文件访问模式
  • O_RDWR-文件访问模式
  • O_CLOEXEC-文件创建标志
  • O_CREAT-文件创建标志
  • O_DIRECTORY-文件创建标志
  • O_EXCL-文件创建标志
  • O_NOCTTY-文件创建标志
  • O_NOFOLLOW-文件创建标志
  • O_TMPFILE-文件创建标志
  • O_TRUNC-文件创建标志
  • FD_CLOEXEC - file descriptor flag
  • O_RDONLY - file access mode
  • O_WRONLY - file access mode
  • O_RDWR - file access mode
  • O_CLOEXEC - file creation flag
  • O_CREAT - file creation flag
  • O_DIRECTORY - file creation flag
  • O_EXCL - file creation flag
  • O_NOCTTY - file creation flag
  • O_NOFOLLOW - file creation flag
  • O_TMPFILE - file creation flag
  • O_TRUNC - file creation flag

您列出的其余标志是文件状态标志.

The rest of the flags you listed are file status flags.

最后一点说明:O_CLOEXEC仅与新FD相关.对于现有的FD,您将仅使用FD_CLOEXEC.

And one final note: O_CLOEXEC is only relevant for a new FD. For existing FDs, you'll only ever use FD_CLOEXEC.

这篇关于文件描述符的规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆