重命名是原子的吗? [英] Is rename() atomic?

查看:146
本文介绍了重命名是原子的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过实验进行检查,也无法从手册页中收集到它.

说我有两个过程,一个是将文件1从目录1移动(重命名)到目录2.假设正在运行的另一个进程同时将directory1和directory2的内容复制到另一个位置.复制是否可能以这种方式发生,即目录1和目录2都将显示文件1-即在第一个进程中,在移动之前复制了目录1,在移动之后复制了目录2.

基本上,rename()是原子系统调用吗?

谢谢

解决方案

是,不是.

rename()是原子的,假定操作系统不会崩溃.不能被任何其他文件系统操作分割.

如果系统崩溃,您可能会看到ln()操作.

还请注意,在网络文件系统上操作时,如果操作成功,您可能会收到ENOENT.本地文件系统无法做到这一点.

I am not being able to check this via experiments and could not gather it from the man pages as well.

Say I have two processes, one moving(rename) file1 from directory1 to directory2. Say the other process running concurrently copies the contents of directory1 and directory2 to another location. Is it possible that the copy happens in such a way that both directory1 and directory2 will show file1 - i.e directory1 is copied before the move and directory2 after the move by the first process.

Basically is rename() is an atomic system call?

Thanks

解决方案

Yes and no.

rename() is atomic assuming the OS does not crash. It cannot be split by any other filesystem op.

If the system crashes you might see a ln() operation instead.

Also note, when operating on a network filesystem, you might get ENOENT when the operation succeeded successfully. Local filesystem can't do that to you.

这篇关于重命名是原子的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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