阿帕奇是否为他们提供服务前,读锁文件? [英] Does Apache read-lock files before serving them?
问题描述
我有一个移动应用,读取存储在Apache服务器上的JSON文件。该JSON文件的内容被再生(使用PHP脚本)如果事情是通过GUI改变。
I have an mobile app that reads a JSON file that is stored on an Apache server. The contents of that JSON file are regenerated (using a PHP script) if something is changed via a GUI.
我很担心,试图覆盖在它被阿帕奇服务的中间JSON文件可能会导致问题。
I am worried that trying to overwrite the JSON file in the middle of it being served by Apache might cause problems.
请问Apache获得任职文件之前读锁?如果不是这样,如果我尝试它在它提供服务的同时?
Does Apache obtain a read lock before serving files? If not, what will happen if I try to write it at the same time it is being served?
推荐答案
没有。在POSIX兼容的系统,所有的锁都咨询反正,所以即使阿帕奇会得到一个读锁,其他进程可以只写文件。
No. On POSIX-compatible systems, all locks are advisory anyways, so even if apache would get a read lock, the other process could just write the file.
您可以确定与 strace的
:
[pid 7246] open("/var/www/file.json", O_RDONLY|O_CLOEXEC) = 11
[pid 7246] fcntl(11, F_GETFD) = 0x1 (flags FD_CLOEXEC)
[pid 7246] mmap(NULL, 20, PROT_READ, MAP_SHARED, 11, 0) = 0x7f53f93da000
[pid 7246] munmap(0x7f53f93da000, 20) = 0
[pid 7246] writev(10, [{"HTTP/1.1 200 OK\r\nDate: Thu, 26 J"}, ...) = 365
[pid 7246] close(11) = 0
因此,可能发生的是你的JSON文件仅部分写入。为了避免这个问题,写你的JSON文件在同一文件系统的临时文件,并使用原子改名
来覆盖该文件。
这样的话,如果打开
成功,Apache将继续为旧文件。如果说之前的改名
完成了打开
,Apache会得到新的,完成的文件。
That way, if the open
has succeeded, apache will continue serving the old file. If the rename
finishes before the open
, apache will get the new, completed file.
如果你担心的一致性(在断电左右的情况下),你可能还需要调用 FSYNC
在关闭前写的JSON文件的应用程序。
If you worry about consistency (in the case of a power failure or so), you may also want to call fsync
in the application that writes the JSON file before closing it.
这篇关于阿帕奇是否为他们提供服务前,读锁文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!