问题
Will flock or lockf work on a directory? I there another way to lock a directory in C on a linux machine?
回答1:
Yes, more info about using flock on file/directory can be found here
回答2:
You can't open a directory for writing, so that means you can't get a write lock on it.
Even if you could, please keep in mind that flock
and fcntl
and other kinds of POSIX locks are advisory, so they don't actually prevent software that doesn't respect the lock from doing things.
Maybe you want to look at something like xfs_freeze
which locks an entire filesystem. It's probably not useful for your use case though.
来源:https://stackoverflow.com/questions/8816941/how-can-i-lock-a-directory-in-c-on-a-linux-machine