I'm learning file locking using flock(2)
, and I want to write a little C++ utility class to handle the locking. I've read quite a lot online, but there is one part I don't understand.
In e.g. Optimal lock file method:
1: open the lock file creating it if it doesn't exist
2: ask for an exclusive lock an agreed byte range in the lock file
3: when the lock is granted then
4: <do my processing here>
5: release my lock
6: close the lock file
end
What is this lock file? I read that it is used for lock administration, but I thought that was exactly what flock(2)
is used for? So why do we need to create an additional file? And why isn't this file deleted after I stopped needing the lock? Doesn't this mean that for every file I ever opened (with this locking method enabled) there is a lock file somewhere?
Would be great if someone could explain this or point me to some resource, I've already spent multiple hours on this.