原子写入文件与不写入文件之间的区别

时间:2021-07-29 02:37:47

What is the difference in writing to files atomically on the iPhone in objective-c and not, is there any performance difference between the two?

在objective-c中以原子方式在iPhone上写文件有什么区别而不是,两者之间是否有任何性能差异?

Thanks in advance!

提前致谢!

3 个解决方案

#1


49  

Atomic in general means the operation cannot be interrupted will complete or have no effect. When writing files, that is accomplished by writing to a temporary file then replacing the original with the temporary when the write completes.

原子一般意味着操作不能中断完成或无效。在编写文件时,可以通过写入临时文件来完成,然后在写入完成时将原始文件替换为临时文件。

A crash while writing an atomic file means the original is not modified and there is a garbage file that can be deleted. A crash while writing normally would mean an expected good file is corrupt.

写入原子文件时发生崩溃意味着原始文件未被修改,并且存在可以删除的垃圾文件。正常写入时崩溃意味着预期的好文件已损坏。

Performance wise the cost is minimal. During the write you will have two copies of a file. The file replace is a very simple operation at the file system level.

性能方面,成本极低。在写入期间,您将拥有两个文件副本。文件替换是文件系统级别的非常简单的操作。

Edit: thanks zneak

编辑:谢谢zneak

#2


0  

Filesystems don't have to resort to write/rename cycles for atomic writes. Filesystems that have locking semantics allow you to 'lock' portions or all of a file, or in some cases even do things like appends to a file, to help with atomicity.

文件系统不必为原子写入使用写/重命名循环。具有锁定语义的文件系统允许您“锁定”部分或全部文件,或者在某些情况下甚至可以执行附加到文件之类的操作来帮助实现原子性。

@Randy, both your assumptions about fragmentation are likely to be wrong. On most filesystems, writing an entire file and closing it will result in a less fragmented file, and writing a large file in a single write will definitely result in better usage of large blocks. If you meant the file blocks were more likely to 'creep' across the disk, that depends on the layout preferences in your filesystem. If you're writing to flash, you probably want the filesystem to creep across the available storage as a sort of self-levelling of the writes.

@Randy,你对碎片化的假设都可能是错误的。在大多数文件系统上,写入整个文件并关闭它将导致文件碎片更少,并且在单次写入中写入大文件肯定会导致更好地使用大块。如果您的意思是文件块更容易在磁盘上“蠕变”,那取决于文件系统中的布局首选项。如果您正在写入闪存,您可能希望文件系统在可用存储中蔓延,作为写入的一种自我调平。

#3


0  

Writing atomically takes more steps - additionally auxiliary file is created. NSString Class Reference explains:

原子写入需要更多步骤 - 另外创建辅助文件。 NSString类参考说明:

If YES, the receiver is written to an auxiliary file, and then the auxiliary file is renamed to path. If NO, the receiver is written directly to path. The YES option guarantees that path, if it exists at all, won’t be corrupted even if the system should crash during writing.

如果是,则将接收器写入辅助文件,然后将辅助文件重命名为路径。如果为NO,则将接收器直接写入路径。 YES选项保证路径(如果存在的话)即使系统在写入期间崩溃也不会被破坏。

Here is the example in the case of pLists:

以下是pLists的示例:

[array writeToFile:path atomically:YES];

when "YES", then pList is updated just once even if you run the code several times in XCode,

当“是”时,即使您在XCode中多次运行代码,pList也只更新一次,

[array writeToFile:path atomically:NO];

when "NO" it is updated as many as you run the same code (repeated update).

当“NO”时,它会像运行相同代码一样多次更新(重复更新)。

#1


49  

Atomic in general means the operation cannot be interrupted will complete or have no effect. When writing files, that is accomplished by writing to a temporary file then replacing the original with the temporary when the write completes.

原子一般意味着操作不能中断完成或无效。在编写文件时,可以通过写入临时文件来完成,然后在写入完成时将原始文件替换为临时文件。

A crash while writing an atomic file means the original is not modified and there is a garbage file that can be deleted. A crash while writing normally would mean an expected good file is corrupt.

写入原子文件时发生崩溃意味着原始文件未被修改,并且存在可以删除的垃圾文件。正常写入时崩溃意味着预期的好文件已损坏。

Performance wise the cost is minimal. During the write you will have two copies of a file. The file replace is a very simple operation at the file system level.

性能方面,成本极低。在写入期间,您将拥有两个文件副本。文件替换是文件系统级别的非常简单的操作。

Edit: thanks zneak

编辑:谢谢zneak

#2


0  

Filesystems don't have to resort to write/rename cycles for atomic writes. Filesystems that have locking semantics allow you to 'lock' portions or all of a file, or in some cases even do things like appends to a file, to help with atomicity.

文件系统不必为原子写入使用写/重命名循环。具有锁定语义的文件系统允许您“锁定”部分或全部文件,或者在某些情况下甚至可以执行附加到文件之类的操作来帮助实现原子性。

@Randy, both your assumptions about fragmentation are likely to be wrong. On most filesystems, writing an entire file and closing it will result in a less fragmented file, and writing a large file in a single write will definitely result in better usage of large blocks. If you meant the file blocks were more likely to 'creep' across the disk, that depends on the layout preferences in your filesystem. If you're writing to flash, you probably want the filesystem to creep across the available storage as a sort of self-levelling of the writes.

@Randy,你对碎片化的假设都可能是错误的。在大多数文件系统上,写入整个文件并关闭它将导致文件碎片更少,并且在单次写入中写入大文件肯定会导致更好地使用大块。如果您的意思是文件块更容易在磁盘上“蠕变”,那取决于文件系统中的布局首选项。如果您正在写入闪存,您可能希望文件系统在可用存储中蔓延,作为写入的一种自我调平。

#3


0  

Writing atomically takes more steps - additionally auxiliary file is created. NSString Class Reference explains:

原子写入需要更多步骤 - 另外创建辅助文件。 NSString类参考说明:

If YES, the receiver is written to an auxiliary file, and then the auxiliary file is renamed to path. If NO, the receiver is written directly to path. The YES option guarantees that path, if it exists at all, won’t be corrupted even if the system should crash during writing.

如果是,则将接收器写入辅助文件,然后将辅助文件重命名为路径。如果为NO,则将接收器直接写入路径。 YES选项保证路径(如果存在的话)即使系统在写入期间崩溃也不会被破坏。

Here is the example in the case of pLists:

以下是pLists的示例:

[array writeToFile:path atomically:YES];

when "YES", then pList is updated just once even if you run the code several times in XCode,

当“是”时,即使您在XCode中多次运行代码,pList也只更新一次,

[array writeToFile:path atomically:NO];

when "NO" it is updated as many as you run the same code (repeated update).

当“NO”时,它会像运行相同代码一样多次更新(重复更新)。