UNIX命令mv和rm如何使用打开的文件? [英] How do the UNIX commands mv and rm work with open files?

查看:270
本文介绍了UNIX命令mv和rm如何使用打开的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我正在读取存储在NTFS文件系统上的文件,并且正在读取文件时尝试移动/重命名该文件,则无法执行此操作。如果我在UNIX文件系统(如EXT3)上尝试这种方法,则会成功,读取过程不受影响。我甚至可以读取文件,读取过程不受影响。这个怎么用?有人可以向我解释为什么在UNIX文件系统下支持这种行为,但不支持NTFS?我有一个模糊的感觉,它与硬链接和inode有关,但我将不胜感激一个很好的解释。 解决方案

使用引用计数和两层体系结构来查找文件。

文件名引用了一个名为

<多个文件名可以引用相同的inode;然后他们被称为
硬链接。另外,文件描述符(fd)是指inode。 fd是打开文件时进程获得的对象的类型。

Unix文件系统中的文件只有在最后一次引用时才会消失,所以当没有更多的名字(硬链接)或fd引用它。所以, rm 实际上并没有删除一个文件;它删除了文件引用。



这个文件系统设置可能看起来很混乱,有时会带来问题(尤其是NFS)有许多应用程序不需要锁定的好处。许多Unix程序也通过打开临时文件并立即删除它们来利用这种情况。只要他们终止,即使他们崩溃,临时文件已经消失。


If I am reading a file stored on an NTFS filesystem, and I try to move/rename that file while it is still being read, I am prevented from doing so. If I try this on a UNIX filesystem such as EXT3, it succeeds, and the process doing the reading is unaffected. I can even rm the file and reading processes are unaffected. How does this work? Could somebody explain to me why this behaviour is supported under UNIX filesystems but not NTFS? I have a vague feeling it has to do with hard links and inodes, but I would appreciate a good explanation.

解决方案

Unix filesystems use reference counting and a two-layer architecture for finding files.

The filename refers to something called an inode, for information node or index node. The inode stores (a pointer to) the file contents as well as some metadata, such as the file's type (ordinary, directory, device, etc.) and who owns it.

Multiple filenames can refer to the same inode; they are then called hard links. In addition, a file descriptor (fd) refers to an inode. An fd is the type of object a process gets when it opens a file.

A file in a Unix filesystem only disappears when the last reference to it is gone, so when there are no more names (hard links) or fd's referencing it. So, rm does not actually remove a file; it removes a reference to a file.

This filesystem setup may seem confusing and it sometimes poses problems (esp. with NFS), but it has the benefit that locking is not necessary for a lot of applications. Many Unix programs also use the situation to their advantage by opening a temporary file and deleting it immediately after. As soon as they terminate, even if they crash, the temporary file is gone.

这篇关于UNIX命令mv和rm如何使用打开的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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