in reply to Re^2: Read in hostfile, modify, output
in thread Read in hostfile, modify, output
To digress a bit, Wiki Linearizability contains a good discussion about what "atomic" means. I'll just quote part: Atomicity is a guarantee of isolation from concurrent processes. Additionally, atomic operations commonly have a succeed-or-fail definition, they either successfully change the state of the system, or have no apparent effect.
In the case of a single rename(), as long as the system software and hardware work as expected, a single rename can be considered atomic. There are ways (like system reset (hardware,software or human induced) or power supply transient) that can mess things up.
To replace an important file with a new version, first get the new file onto the system in its entirety. Now a quick two step dance happens. At a minimum, rename the existing file to something else, probably some sort of .bak. Then as soon as possible, rename the new file to what the existing file's name was. With everything working perfectly, there is a brief interval where "File" does not exist - that happens between the 2 renames. Any open() attempted during that very short interval will fail.
At the lowest level (disk writing hardware), a disk write operation is not "atomic" and can fail this test: either successfully change the state of the system, or have no apparent effect. When writing a sector on the HD, the sequence: a) Old data, b)No data(the write is occuring), c)New data occurs. If something goes wrong during step (b), then we are left with unknown garbage. Even the very fast rename function goes through these 3 steps.
Summary: Rename is the fastest and best function to use for replacing files. Get the new file completely ready in advance. Do not mess around with writing the new file line by line - that is too slow and opens up corruption issues.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Read in hostfile, modify, output
by Anonymous Monk on Dec 20, 2016 at 23:14 UTC | |
by Marshall (Canon) on Dec 21, 2016 at 00:41 UTC | |
by eyepopslikeamosquito (Archbishop) on Dec 21, 2016 at 19:19 UTC | |
by Marshall (Canon) on Dec 22, 2016 at 20:25 UTC | |
by eyepopslikeamosquito (Archbishop) on Dec 23, 2016 at 23:04 UTC | |
by Anonymous Monk on Dec 21, 2016 at 01:13 UTC | |
by Marshall (Canon) on Dec 21, 2016 at 01:27 UTC | |
by Anonymous Monk on Dec 21, 2016 at 01:53 UTC | |
| |
by Anonymous Monk on Dec 21, 2016 at 10:27 UTC |