简体   繁体   English

在Linux上移动文件时的竞争条件

[英]Race condition while moving files on Linux

Suppose I have two scripts. 假设我有两个脚本。 The first one puts (with mv command) some files into a directory, the second one checks the directory once in a while and processes the files. 第一个将(使用mv命令)一些文件放入目录,第二个文件偶尔检查一次目录并处理文件。 The situation I'm concerned about is when the second script starts processing of the file which is only partly moved at the moment. 我关注的情况是当第二个脚本开始处理文件时,此时仅部分移动。 Can this happen in real life on XFS file system? 这可能发生在XFS文件系统的现实生活中吗?

It depends on where you're moving the files from. 这取决于您从哪里移动文件。 mv WITHIN a single filesystem is atomic, otherwise it must do a copy which is not atomic (followed by a delete of the original file), and is prone to the kind of race condition you mention. mv WITHIN单个文件系统是原子的,否则它必须做一个非原子的复制(后面是原始文件的删除),并且容易出现你所提到的那种竞争条件。

FWIW, this is normal POSIX semantics, nothing particular to XFS. FWIW,这是正常的POSIX语义,对XFS没有什么特别之处。

Race condition would not occure in your case in XFS file system. 在XFS文件系统中,您的情况不会出现竞争条件。 However XFS allows multiple processes to read and write a file at once by using flexible locking scheme in contrast to Unix file systems single threaded inode lock. 但是,与Unix文件系统单线程inode锁相比,XFS允许多个进程使用灵活的锁定方案一次读取和写入文件。 XFS tack care of serializing the writes on the same region by multiple processes . XFS负责通过多个进程序列化同一区域的写入。

XFS uses direct I/O for accessing the file.Direct I/O allows an application to specify that its data not to be cached in the buffer cache. XFS使用直接I / O来访问文件。直接I / O允许应用程序指定其数据不缓存在缓冲区缓存中。

When using normal, buffered I/O, multiple readers can access the file concurrently, but only a single writer is allowed access to the file at a time. 使用普通的缓冲I / O时,多个读取器可以同时访问文件,但一次只允许一个编写器访问该文件。 When using direct I/O, multiple readers and writers can access the file simultaneously. 使用直接I / O时,多个读取器和写入器可以同时访问该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM