I have a console programm that runs different commands. I want to be able to run multiple instances of the programm in parallel however, I want to prevent specific commands from beeing processed in parallel.
I thought of a simple file lock system.
Given command “foo” is not allowed to be run in parallel, will that work?
- before running foo check if file .lock_foo_* exists (if so, abort)
- if not, create file .lock_foo_GUID
- check if .lock_foo_GUID exists and if no other .lock_foo_* file exists
if there is another .lock_foo_* file, that means that another instance created one at the simultanously and we abort and remove our lock file.
I wonder if there are any race conditions and if that’s the correct way to handle it?
UPDATE
I think it’s actually easier than initially expected (at least on windows). I think the answer from thb set me on the right track.
I think something like this should work
public bool CreateLock(string fileName){
try{
File.Open(string.Format(".lock_{0}",fileName), FileMode.Create, FileAccess.ReadWrite, FileShare.None);
}
catch (Exception exception){
return false;
}
return true;
}
The OS will make sure that the file will be only created if it doesn’t exist yet and will otherwise raise an exception. So this method should be safe. Unfortunatly this will not work on UNIX though.
Your logic looks pretty good. However, if it interests you, there already exist more than one command-line utility that do what you want, for example
lockfileandflock.Try
man 1 lockfile. I suspect that that manpage will succinctly answer your question.On the other hand, if you need or prefer to implement this, yourself, then the approach you suggest is almost right but must sporadically, very occasionally fail. This is frustrating, but is inherent to the kind of thing you are trying to do. The reason is that a brief interval interposes between (1) checking whether another process has locked the resource and (2) locking the resource. What happens if, during this brief interval, another process should lock the resource?
What is needed is an atomic operation, which somehow checks and locks as a single, uninterruptible step. Fortunately, modern CPUs provide such atomic operations and modern operating-system kernels expose them, precisely so you can indeed reliably do what you want.
If this interests you, then you can consult Microsoft’s development docs for LockFile and LockFileEx.