多个进程同时写入同一个文件是否安全?[CentOs 6,ext4] [英] Safe to have multiple processes writing to the same file at the same time? [CentOs 6, ext4]

查看:19
本文介绍了多个进程同时写入同一个文件是否安全?[CentOs 6,ext4]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个系统,其中多个从属进程通过 unix 域套接字进行通信,并且它们同时写入 同一个文件.我从来没有研究过文件系统或这个特定的文件系统(ext4),但感觉这里可能存在一些危险.

I'm building a system where multiple slave processes are communicating via unix domain sockets, and they are writing to the same file at the same time. I have never studied filesystems or this specific filesystem (ext4), but it feels like there might be some danger here.

每个进程写入输出文件的不相交子集(即,正在写入的块中没有重叠).例如,P1 仅写入文件的前 50%,而 P2 仅写入后 50%.或者可能 P1 只写入奇数块,而 P2 写入偶数块.

Each process writes to a disjoint subset of the output file (ie, there is no overlap in the blocks being written). For example, P1 writes to only the first 50% of the file and P2 writes only to the second 50%. Or perhaps P1 writes only the odd-numbered blocks while P2 writes the even-numbered blocks.

P1P2(在不同的线程上同时运行)在不使用任何锁定的情况下写入同一个文件是否安全?换句话说,文件系统是否隐式地施加了某种锁定?

Is it safe to have P1 and P2 (running simultaneously on separate threads) writing to the same file without using any locking? In other words, does the filesystem impose some kind of locking implicitly?

注意:很遗憾,我不能随意输出多个文件并在以后加入它们.

Note: I'm unfortunately not at liberty to output multiple files and join them later.

注意:我发布此问题后的阅读内容与下面唯一发布的答案不符.我读到的所有内容都表明我想做的事情很好,而下面的受访者坚持认为我正在做的事情不安全,但我无法辨别所描述的危险.

Note: My reading since posting this question does not agree with the only posted answer below. Everything I've read suggests that what I want to do is fine, whereas the respondent below insists what I am doing is unsafe, but I am unable to discern the described danger.

推荐答案

如果您使用的是 POSIX原始"IO 系统调用,例如 read()、write()、lseek() 等等.

What you're doing seems perfectly OK, provided you're using the POSIX "raw" IO syscalls such as read(), write(), lseek() and so forth.

如果您使用 C stdio(fread()、fwrite() 和朋友)或其他一些具有自己的用户空间缓冲的语言运行时库,那么Tilo"的答案是相关的,因为缓冲,在某种程度上超出您的控制范围,不同的进程可能会覆盖彼此的数据.

If you use C stdio (fread(), fwrite() and friends) or some other language runtime library which has its own userspace buffering, then the answer by "Tilo" is relevant, in that due to the buffering, which is to some extent outside your control, the different processes might overwrite each other's data.

Wrt OS 锁定,而 POSIX 声明写入或读取小于 PIPE_BUF 的大小对于某些特殊文件(管道和 FIFO)是原子的,但对于常规文件没有这样的保证.在实践中,我认为页面内的 IO 很可能是原子的,但没有这样的保证.操作系统仅在保护其内部数据结构所需的范围内进行内部锁定.可以使用文件锁或其他一些进程间通信机制来序列化对文件的访问.但是,所有这些仅与您有多个进程对文件的同一区域执行 IO 相关.在您的情况下,由于您的进程正在对文件的不相交部分执行 IO,因此这些都不重要,您应该没问题.

Wrt OS locking, while POSIX states that writes or reads less than of size PIPE_BUF are atomic for some special files (pipes and FIFO's), there is no such guarantee for regular files. In practice, I think it's likely that IO's within a page are atomic, but there is no such guarantee. The OS only does locking internally to the extent that is necessary to protect its own internal data structures. One can use file locks, or some other interprocess communication mechanism, to serialize access to files. But, all this is relevant only of you have several processes doing IO to the same region of a file. In your case, as your processes are doing IO to disjoint sections of the file, none of this matters, and you should be fine.

这篇关于多个进程同时写入同一个文件是否安全?[CentOs 6,ext4]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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