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

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

问题描述

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

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

可以安全地在 P1 P2 )写入相同的文件,而不使用任何锁定?换句话说,文件系统是否会隐含地实施某种锁定?



注意:不幸的是,我无法输出多个文件,加入他们后。



注意:自发布此问题后,我的阅读不符合下面唯一发布的答案。我读过的所有内容都表明,我想要做的事情是好的,而下面的答复者坚持我所做的事情是不安全的,但是我无法辨别所描述的危险。



Wrt OS锁定,而POSIX指出对于一些特殊文件(管道和FIFO)来说,写入或读取小于PIPE_BUF的大小是原子的,对于普通文件是没有这种保证的。实际上,我认为页面内的IO可能是原子性的,但是没有这样的保证。操作系统只在内部进行锁定,以保护自己的内部数据结构。可以使用文件锁定或其他进程间通信机制来序列化对文件的访问。但是,所有这一切只与你有几个进程IO在文件的相同区域相关。在你的情况,因为你的进程正在做IO来分离文件的部分,这些都不重要,你应该没问题。

I'm building a system where multiple slave processes are communicating via unix domain sockets, and at the same time 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.

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.

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.

解决方案

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

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 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天全站免登陆