Suppose I have a folder and I want only one instance of my application working on it at a time. I can only synchronize via the filesystem itself. Often times this is a accomplished with something like a .lock_file
where if that's present I know another instance is currently using it. Are there any standard libraries that handle this sort of thing?
Asked
Active
Viewed 221 times
0

marius
- 1,352
- 1
- 13
- 29
-
Which OS and language/framework? If you add these as tags you'll get more views and hopefully an answer. – SilverlightFox Jul 02 '15 at 09:55
1 Answers
1
If you are using C/C++, see fclnt or flock : Locking files in linux with c/c++
If you are using java, see FileChannel lock method and How can I lock a file using java (if possible)
You also can check for the existence of the .lock_file
opening it with
open(pathname, O_CREAT | O_EXCL, 0644)
,
see open man page, it creates and opens the file and returns EEXIST if pathname exists.
In java, calling to File method createNewFile() can be use to create atomically the .lock_file

Community
- 1
- 1

rafalopez79
- 2,046
- 4
- 27
- 23