In case I have a program that needs some basic configuration files that cannot be modified when it is running (during start-up), what is the proper way to implement a lock in its directory environment?
Details:
The scenario I'm thinking is: only the software itself is able to modify the files. So, I need to protect the environment from other instances of the software. The software also might be composed of several different programs. So, I need to find an interface that is straightforward to share among processes.
Idea 1:
One way would be acquiring lock (flock) for each configuration file. This is nearly impractical to implement and maintain.
Idea 2:
The second way I've been thinking is to create a .lock file in my directory during start-up and delete it when the config files are safe to be modified. I've seen many commercial softwares using some similar approaches. However, I suspect that the lack of atomicity of these operations might be an issue. Is this method safe when several applications might concurrently try to create/poll/delete the lock file?