0

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?

marius
  • 1,352
  • 1
  • 13
  • 29

1 Answers1

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