每个Unix文件描述是否都有自己的读/写缓冲区? [英] Does each Unix file description have its own read/write buffers?

查看:52
本文介绍了每个Unix文件描述是否都有自己的读/写缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于有关read()和write()的问题,我想知道是否每个打开的文件描述有自己的读取和写入缓冲区,或者一次打开多次时,如果文件只有一个读取和写入缓冲区.我很好奇,因为这会对重叠写入同一文件的情况产生什么影响.也许这在Unix之间有所不同?

In reference to this question about read() and write(), I'm wondering if each open file description has its own read and write buffers or if perhaps there's a single read and write buffer for a file when it has been opened multiple times at once. I'm curious because this would have an effect on what exactly happens with overlapping writes to the same file. Perhaps this is something that varies among Unixes?

(据我所知,文件描述"是指有关打开文件的信息/选项,例如当前标记位置.相比之下,文件描述符"仅指在处理过程中使用的数字来引用说明.)

(To my understanding, "file description" refers to the info/options about an open file, such as the current marker position. "File descriptor", in contrast, refers to just the number used in a process to refer to a description.)

推荐答案

这在某种程度上取决于您是在谈论套接字还是实际文件.

This depends a bit on whether you are talking about sockets or actual files.

严格来说,描述符永远不会有自己的缓冲区.它只是更深层次抽象的句柄.

Strictly speaking, a descriptor never has its own buffers; it's just a handle to a deeper abstraction.

文件系统对象至少在需要时具有其自己的"缓冲区.也就是说,如果程序写入的文件小于文件系统块的大小,则内核别无选择,只能读取FS块并将写入的数据与现有数据合并.

File system objects have their "own" buffers, at least when they are required. That is, if a program writes less than the file system block size, the kernel has no choice but to read a FS block and merge the write with the existing data.

此缓冲区连接到vnode并处于较低级别,可能是inode.它归文件所有,而不是描述符所有.如果有可用内存,它可能会保留很长一段时间.

This buffer is attached to the vnode and at a lower level, possibly an inode. It's owned by the file and not the descriptor. It may be kept around for a long time if memory is available.

在使用套接字的情况下,流(实际上不是单个描述符)实际上具有它拥有的缓冲区.

In the case of a socket, then a stream, but not specifically a single descriptor, does actually have buffers that it owns.

这篇关于每个Unix文件描述是否都有自己的读/写缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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