文件附加在 UNIX 中是原子的吗? [英] Is file append atomic in UNIX?

查看:23
本文介绍了文件附加在 UNIX 中是原子的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般来说,当我们在 UNIX 中从多个进程追加到一个文件时,我们可以认为什么是理所当然的?是否可能丢失数据(一个进程覆盖另一个进程的更改)?数据是否有可能被破坏?(例如,每个进程在日志文件中每次追加一行,是否有可能两行被破坏?)如果追加不是上述意义上的原子,那么确保互斥的最佳方法是什么?

In general, what can we take for granted when we append to a file in UNIX from multiple processes? Is it possible to lose data (one process overwriting the other's changes)? Is it possible for data to get mangled? (For example, each process is appending one line per append to a log file, is it possible that two lines get mangled?) If the append is not atomic in the above sense, then what's the best way of ensuring mutual exclusion?

推荐答案

小于 'PIPE_BUF' 大小的写入应该是原子的.这应该至少是 512 字节,尽管它很容易变大(Linux 似乎将它设置为 4096).

A write that's under the size of 'PIPE_BUF' is supposed to be atomic. That should be at least 512 bytes, though it could easily be larger (linux seems to have it set to 4096).

这里假设您说的是所有完全符合 POSIX 的组件.例如,在 NFS 上就不是这样.

This assume that you're talking all fully POSIX-compliant components. For instance, this isn't true on NFS.

但假设您写入以O_APPEND"模式打开的日志文件,并将行(包括换行符)保持在PIPE_BUF"字节以下,那么您应该能够将多个写入器写入日志文件,而不会出现任何损坏问题.任何中断都会在写入之前或之后到达,而不是在中间.如果您希望文件完整性在重启后仍然存在,您还需要在每次写入后调用 fsync(2),但这对性能来说很糟糕.

But assuming you write to a log file you opened in 'O_APPEND' mode and keep your lines (including newline) under 'PIPE_BUF' bytes long, you should be able to have multiple writers to a log file without any corruption issues. Any interrupts will arrive before or after the write, not in the middle. If you want file integrity to survive a reboot you'll also need to call fsync(2) after every write, but that's terrible for performance.

澄清:阅读评论和Oz Solomon 的回答.我不确定 O_APPEND 是否应该具有 PIPE_BUF 大小的原子性.这完全有可能是 Linux 实现 write() 的方式,或者可能是由于底层文件系统的块大小.

Clarification: read the comments and Oz Solomon's answer. I'm not sure that O_APPEND is supposed to have that PIPE_BUF size atomicity. It's entirely possible that it's just how Linux implemented write(), or it may be due to the underlying filesystem's block sizes.

这篇关于文件附加在 UNIX 中是原子的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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