Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to use mkdir as file-based locking on linux?

Tags:

linux

locking

I wanted to quickly implement some sort of locking in perl program on linux, which would be shareable between different processes.

So I used mkdir as an atomic operation, which returns 1 if the directory doesn't exist and 0 if it does. I remove the directory right after the critical section.

Now, it was pointed to me that it's not a good practice in general (independently on the language). I think it's quite OK, but I would like to ask your opinion.

edit: to show an example, my code looked something like this:

while (!mkdir "lock_dir") {wait some time}
critical section
rmdir "lock_dir"
like image 574
Karel Bílek Avatar asked Aug 26 '11 17:08

Karel Bílek


People also ask

Can you lock a file in Linux?

To enable mandatory file locking in Linux, two requirements must be satisfied: We must mount the file system with the mand option (mount -o mand FILESYSTEM MOUNT_POINT). We must turn on the set-group-ID bit and turn off the group-execute bit for the files we are about to lock (chmod g+s,g-x FILE).

Can mkdir fail?

The mkdir() function shall fail if: [EACCES] Search permission is denied on a component of the path prefix, or write permission is denied on the parent directory of the directory to be created. [EEXIST]

What is lock directory in Linux?

1. Purpose. Lock files should be stored within the /var/lock directory structure. Lock files for devices and other resources shared by multiple applications, such as the serial device lock files that were originally found in either /usr/spool/locks or /usr/spool/uucp , must now be stored in /var/lock .

Is mkdir Atomic?

mkdir is not an atomic operation.


2 Answers

IMHO this is a very bad practice. What if the perl script which created the lock directory somehow got killed during the critical section? Another perl script waiting for the lock dir to be removed will wait forever, because it won't get removed by the script which originally created it. To use safe locking, use flock() on a lock file (see perldoc -f flock).

like image 85
Friek Avatar answered Oct 19 '22 06:10

Friek


This is fine until an unexpected failure (e.g. program crash, power failure) happens while the directory exists.

After this, the program will never run because the lock is locked forever (assuming the directory is on a persistent filesystem).

Normally I'd use flock with LOCK_EXCL instead.

Open a file for reading+writing, creating it if it doesn't exist. Then take the exclusive lock, if that fails (if you use LOCK_NB) then some other process has it locked.

After you've got the lock, you need to keep the file open.

The advantage of this approach is, if the process dies unexpected (for example, crash, is killed or the machine fails), the lock is automatically released.

like image 37
MarkR Avatar answered Oct 19 '22 05:10

MarkR