logoalt Hacker News

Things Unix can do atomically (2010)

119 pointsby onurkanbkrctoday at 5:29 AM44 commentsview on HN

Comments

amstantoday at 7:52 AM

Missing (probably because of the date of the article): `mv --exchange` aka renameat2+RENAME_EXCHANGE. It atomically swaps 2 file paths.

show 2 replies
0xbadcafebeetoday at 5:52 AM

You can use `ln` atomicity for a simple, portable(ish) locking system: https://gist.github.com/pwillis-els/b01b22f1b967a228c31db3cf...

show 1 reply
Igromtoday at 9:44 AM

>fcntl(fd, F_GETLK, &lock), fcntl(fd, F_SETLK, &lock), and fcntl(fd, F_SETLKW, &lock)

There's also `flock`, the CLI utility in util-linux, that allows using flocks in shell scripts.

show 1 reply
ncrucestoday at 9:55 AM

I use several of these to implement alternative SQLite locking protocols.

POSIX file locking semantics really are broken beyond repair: https://news.ycombinator.com/item?id=46542247

zzo38computertoday at 7:38 AM

Even though it can do some things atomically, it only does with one file at a time, and race conditions are still possible because it only does one operation at a time (even if you are only need one file). Some of these are helpful anyways, such as O_EXCL, but it is still only one thing at a time which can cause problems in some cases.

What else it does not do is a transaction with multiple objects. That is why, I would design a operating system, that you can do a transaction with multiple objects.

show 2 replies
MintPawtoday at 7:01 AM

Not much apparently, although I didn't know about changing symlinks, that could be very useful.

sega_saitoday at 6:41 AM

rename() is certainly the easiest to use for any sort of file-system based synchronization.

andrewstuarttoday at 9:54 AM

Anywhere there is atomic capability you can build a queuing application.

ta8903today at 7:11 AM

Not technically related to atomicity, but I was looking for a way to do arbitrary filesystem operations based on some condition (like adding a file to a directory, and having some operation be performed on it). The usual recommendation for this is to use inotify/watchman, but something about it seems clunky to me. I want to write a virtual filesystem, where you pass it a trigger condition and a function, and it applies the function to all files based on the trigger condition. Does something like this exist?

show 3 replies
maximgeorgetoday at 8:09 AM

[dead]

exactoday at 6:29 AM

Sorry, there is zero chance I will ever deploy new code by changing a symlink to point to the new directory.

show 5 replies
klempnertoday at 9:00 AM

This document being from 2010 is, of course, missing the C11/C++11 atomics that replaced the need for compiler intrinsics or non portable inline asm when "operating on virtual memory".

With that said, at least for C and C++, the behavior of (std::)atomic when dealing with interprocess interactions is slightly outside the scope of the standard, but in practice (and at least recommended by the C++ standard) (atomic_)is_lock_free() atomics are generally usable between processes.