文件系统如何处理并发读/写? [英] How do filesystems handle concurrent read/write?

查看:400
本文介绍了文件系统如何处理并发读/写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户A要求系统读取文件 foo ,同时用户B要将他或她的数据保存到同一个文件中。这种情况是如何在文件系统级别上处理的?大多数文件系统(但不是全部)使用锁定来防止对文件系统级别的并发访问相同的文件。锁可以是独占的,所以第一个获得锁的用户可以访问 - 后续的用户会得到拒绝访问的错误。在您的示例场景中,用户A将能够读取文件并获取文件锁定,但用户B将无法在用户A正在读取时进行写入。



一些文件系统(例如NTFS)允许指定锁定级别,以允许例如并发读取器,但不允许作者。字节范围的锁也是可能的。

与数据库不同,文件系统通常不是事务性的,不是原子性的,来自不同用户的变化不是孤立的(如果更改甚至可以看到 - )

使用全文件锁是一种粗粒度的方法,但它会防止不一致的更新。并不是所有的文件系统都支持全文锁,所以通常使用一个锁文件 - 一个典型的空文件,它的存在表明它的关联文件正在被使用。 (创建一个文件是在大多数文件系统上的一个原子操作。)


User A asks the system to read file foo and at the same time user B wants to save his or her data onto the same file. How is this situation handled on the filesystem level?

解决方案

Most filesystems (but not all) use locking to guard concurrent access to the same file. The lock can be exclusive, so the first user to get the lock gets access - subsequent users get a "access denied" error. In your example scenario, user A will be able to read the file and gets the file lock, but user B will not be able to write while user A is reading.

Some filesystems (e.g. NTFS) allow the level of locking to be specified, to allow for example concurrent readers, but no writers. Byte-range locks are also possible.

Unlike databases, filesystems typically are not transactional, not atomic and changes from different users are not isolated (if changes can even be seen - locking may prohibit this.)

Using whole-file locks is a coarse grained approach, but it will guard against inconsistent updates. Not all filesystems support whole-file locks, and so it is common practice to use a lock file - a typically empty file whose presence indicates that its associated file is in use. (Creating a file is an atomic operation on most file systems.)

这篇关于文件系统如何处理并发读/写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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