Live data from Hacker News

Everything you never wanted to know about file locking (2010)

apenwarr.ca

11–18 of 18 posts

Re: Everything you never wanted to know about file locking (2010)

#11
post #4

One sure way to get a lock is to make a directory. #!/bin/sh if mkdir /your/lockdir then trap "rmdir /your/lockdir" EXIT INT ABRT TERM ...code goes here... else echo somebody else has the lock fi No matter how many processes attempt to make the directory, only one will succeed. That works for my scripting, but I have never used it in C.

Is this guaranteed to be atomic on all filesystems?

Re: Everything you never wanted to know about file locking (2010)

#12
post #11
post #4

One sure way to get a lock is to make a directory. #!/bin/sh if mkdir /your/lockdir then trap "rmdir /your/lockdir" EXIT INT ABRT TERM ...code goes here... else echo somebody else has the lock fi No matter how many processes attempt to make the directory, only one will succeed. That works for my scripting, but I have never used it in C.

Is this guaranteed to be atomic on all filesystems?

For POSIX, yes.

https://rcrowley.org/2010/01/06/things-unix-can-do-atomicall...

Windows has a deep well of POSIX in the kernel (plus hard file locks), and it appears to hold there.

https://en.wikipedia.org/wiki/Microsoft_POSIX_subsystem

Re: Everything you never wanted to know about file locking (2010)

#13
post #4

One sure way to get a lock is to make a directory. #!/bin/sh if mkdir /your/lockdir then trap "rmdir /your/lockdir" EXIT INT ABRT TERM ...code goes here... else echo somebody else has the lock fi No matter how many processes attempt to make the directory, only one will succeed. That works for my scripting, but I have never used it in C.

this is great thanks, was just wondering, could something else remove the dir in between the if and then, before trap? Just wondering about the atomicity.

The problem with lock files and lock directories is that if the lock holder dies without cleaning up you now need to do something to clean up.

Re: Everything you never wanted to know about file locking (2010)

#14

Earlier quoted context omitted.

this is great thanks, was just wondering, could something else remove the dir in between the if and then, before trap? Just wondering about the atomicity.

The problem with lock files and lock directories is that if the lock holder dies without cleaning up you now need to do something to clean up.

On Linux, this is why I always turn to using abstract sockets when I only need local locking. Only one process can bind and the kernel cleans up automatically on process exit.

You could do the same thing with TCP/UDP, but abstract sockets give you more flexibility in naming with 108 characters vs. being forced to use a 16-bit integer. Also it means you aren't using up a port that could otherwise be used for actual network communication.

Abstract sockets also make for a nice process existence monitoring mechanism since any processes connected to the bound socket are guaranteed to be immediately notified when the process dies.

Re: Everything you never wanted to know about file locking (2010)

#17
post #11
post #4

One sure way to get a lock is to make a directory. #!/bin/sh if mkdir /your/lockdir then trap "rmdir /your/lockdir" EXIT INT ABRT TERM ...code goes here... else echo somebody else has the lock fi No matter how many processes attempt to make the directory, only one will succeed. That works for my scripting, but I have never used it in C.

Is this guaranteed to be atomic on all filesystems?

It’s even atomic on NFS. In fact, it’s probably the only reliable locking mechanism on NFS.

Re: Everything you never wanted to know about file locking (2010)

#18

Another good read is the SQLite locking module, https://www.sqlite.org/src/artifact/0240c5b547b4cf585c8cac35... , since these guys have to deal with the insanity of locking across different systems in real life. You know things are bad when the least awful implementation of OS-level locking is the one from Microsoft.

POSIX locks are insane enough that when I reimplemented the SQLite file system API, I gave up on them: https://github.com/ncruces/go-sqlite3/tree/main/vfs#file-loc...
Post reply on HN