我是否需要冲洗命名管道?

编程入门 行业动态 更新时间:2024-10-28 02:28:41
本文介绍了我是否需要冲洗命名管道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我找不到命名管道是否被缓冲,因此是问题.

I cannot find whether named pipes are buffered, hence the question.

该联机帮助页显示 linux.die/man/3/mkfifo :

FIFO特殊文件类似于管道...任何进程都可以以与普通文件相同的方式打开它以进行读取或写入.

A FIFO special file is similar to a pipe ... any process can open it for reading or writing, in the same way as an ordinary file.

管道没有缓冲,无需冲洗.但是在普通文件中,我会使文件描述符变得模糊(或fsync).

Pipes are not buffered, no need to flush. But in a ordinary file, I would fflush (or fsync) the file descriptor.

命名管道怎么样?

推荐答案

管道没有缓冲,无需冲洗.

Pipes are not buffered, no need to flush.

我实际上反过来说:对于大多数意图和目的,管道只是缓冲.刷新它们没有意义,因为没有底层设备可以接收数据.

I'd actually put that the other way around: for most intents and purposes, pipes are nothing but buffer. It is not meaningful to flush them because there is no underlying device to receive the data.

此外,尽管POSIX并未明确禁止对管道I/O进行额外的缓冲,但它确实提出了足够的行为要求,我认为没有任何办法可以通过观察来确定是否发生这种缓冲,除非可能通过fsync()来确定.成功.换句话说,即使有额外的缓冲,也不必fsync()管道末端.

Moreover, although POSIX does not explicitly forbid additional buffering of pipe I/O, it does place sufficient behavioral requirements that I don't think there's any way to determine from observation whether such buffering occurs, except possibly by whether fsync() succeeds. In other words, even if there were extra buffering, it should not be necessary to fsync() a pipe end.

但是在普通文件中,我 会清空(或fsync)文件描述符.

But in a ordinary file, I would fflush (or fsync) the file descriptor.

好吧,您不会fflush()文件描述符. fflush()对由FILE对象表示的流进行操作,而不对文件描述符进行操作.这是一个至关重要的区别,因为大多数流都在C库级别进行缓冲,而与下面文件的性质无关. fflush()与之交互的就是该库级别的缓冲区.您可以通过setvbuf()函数控制流的库级缓冲模式.

Well no, you would not fflush() a file descriptor. fflush() operates on streams, represented by FILE objects, not on file descriptors. This is a crucial distinction, because most streams are buffered at the C library level, independent of the nature of the file underneath. It is this library-level buffer that fflush() interacts with. You can control the library-level buffering mode of a stream via the setvbuf() function.

在提供它的那些系统上,fsync()在另一个较低的级别上运行.它指示OS确保先前写入指定文件描述符的所有数据均已传递到基础存储设备.换句话说,它将刷新操作系统级别的缓冲区.

On those systems that provide it, fsync() operates at a different, lower level. It instructs the OS to ensure that all data previously written to the specified file descriptor has been delivered to the underlying storage device. In other words, it flushes OS-level buffers.

请注意,您可以通过fdopen()函数将流包装在管道端文件描述符周围.并不需要管道比以前更多的刷新,但是默认情况下流将被缓冲,因此刷新与之相关.

Note well that you can wrap a stream around a pipe-end file descriptor via the fdopen() function. That doesn't make the pipe require flushing any more than it did before, but the stream will be buffered by default, so flushing will be relevant to it.

还请注意,某些存储设备执行其自身的缓冲,因此即使将数据移交给存储设备后,也不能确定它们是否立即持久.

Note, too, that some storage devices perform their own buffering, so that even after the data have been handed off to a storage device, it is not certain that they are immediately persistent.

命名管道怎么样?

How about named pipe?

上面有关流I/O vs 的讨论.基于POSIX描述符的I/O也适用于此.如果您通过流访问命名管道,则其与fflush()的交互将取决于该流的缓冲.

The discussion above about stream I/O vs. POSIX descriptor-based I/O applies here, too. If you access a named pipe via a stream, then its interaction with fflush() will depend on the buffering of that stream.

但是我想您的问题更多是关于OS级缓冲和刷新的. POSIX似乎并没有说什么具体,但是由于您标记了[linux]并在您的问题中参考了Linux手册页,因此我在此提供了答案:

But I suppose your question is more about os-level buffering and flushing. POSIX does not appear to say much concrete, but since you tag [linux] and refer to a Linux manual page in your question, I offer this in response:

管道和FIFO之间的唯一区别是 它们被创建并打开.完成这些任务后, 管道和FIFO上的I/O具有完全相同的语义.

The only difference between pipes and FIFOs is the manner in which they are created and opened. Once these tasks have been accomplished, I/O on pipes and FIFOs has exactly the same semantics.

( =" Linux pipe(7)手册页.)

更多推荐

我是否需要冲洗命名管道?

本文发布于:2023-07-07 16:11:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1065189.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管道

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!