Unix是否以原子方式对单个文件进行读写操作? [英] Are Unix reads and writes to a single file atomically serialized?

查看:79
本文介绍了Unix是否以原子方式对单个文件进行读写操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道对单个文件的写操作是否原子完成,以使对同一文件的write("bla bla")和随后的write("herp derp")永远不会导致交错,例如"bla herp bla derp".假设这些写操作发生在不同的进程或线程中,什么决定了哪个先完成?

I'd like to know if the writes upon a single file get done atomically such that write("bla bla") and a subsequent write("herp derp") to the same file never results in interleaving, e.g. "bla herp bla derp". Assuming these writes happen in different processes or threads, what governs which gets done first?

此外,read()是否总是在完全完成所有先前写入的状态下返回反映文件的数据(无论该数据实际上是否已写入磁盘)?例如,在write("herp derp")之后,所有后续读取将始终反映写入文件的完整数据,或者后续读取有时仅反映"herp",而不反映"derp"(或有时不反映任何数据)完全没有)?如果读写发生在不同的进程/线程中怎么办?

Also, does a read() always return data reflecting the file in a state of all previous writes fully completed (whether the data has been actually written to disk or not)? For example, after write("herp derp"), will all subsequent reads always reflect the full data written to the file, or will a subsequent read sometimes reflect only "herp" but not "derp" (or sometimes reflect none of the data at all)? What if the reads and writes occur in different processes/threads?

我对并发文件访问策略不感兴趣.我只想知道读写操作到底能干什么.

I'm not interested in concurrent file access strategies. I just want to know what read and write do exactly.

推荐答案

单独的write()调用被单独处理,而不是作为单个原子写入事务处理,并且当多个进程/线程正在向同一个文件写入时,完全可以进行交织.实际写入的顺序由调度程序(两个内核进程调度程序,对于绿色"线程,则由线程库的调度程序)确定.

Separate write() calls are processed separately, not as a single atomic write transaction, and interleaving is entirely possible when multiple processes/threads are writing to the same file. The order of the actual writes is determined by the schedulers (both kernel process scheduler, and for "green" threads the thread library's scheduler).

除非另行指定(O_DIRECT open标志或类似的,如果支持),read()write()在内核缓冲区上运行,并且read()将优先使用已加载的缓冲区来优先读取磁盘.

Unless you specify otherwise (O_DIRECT open flag or similar, if supported), read() and write() operate on kernel buffers and read() will use a loaded buffer in preference to reading the disk again.

请注意,这可能会由于本地文件缓冲而变得复杂;例如,stdioiostreams会在独立于内核缓冲区的进程中按块将文件数据读取到缓冲区中,因此从其他位置到已经在stdio中缓冲的数据的write()不会被见过.同样,使用输出缓冲之前,在刷新输出缓冲区之前,不会自动生成任何实际的内核级输出,要么是由于缓冲区已满而自动刷新,要么是由于fflush()或C ++的endl(隐式刷新输出缓冲区)而手动生成的

Note that this may be complicated by local file buffering; for example, stdio and iostreams will read file data by blocks into a buffer in the process which is independent of kernel buffers, so a write() from elsewhere to data that are already buffered in stdio won't be seen. Likewise, with output buffering there won't be any actual kernel-level output until the output buffer is flushed, either automatically because it has filled up or manually due to fflush() or C++'s endl (which implicitly flushes the output buffer).

这篇关于Unix是否以原子方式对单个文件进行读写操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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