I use F_SETLKW to lock a file or keep waiting. How can I interrupt the blocking lock after waiting for a period? I want to add an argument to let user set waiting time. Maybe I can use non-blocking lock(F_SETLK) and a loop to check it. But it may consume too many resources. Any idea?
That what I'm trying now. But it's weird. I wrote a signal handler function to judge if set lock successfully or wait timeout. There are A, B ,C three processes trying to get file lock. A starts to lock file first, then B starts waiting, then C starts waitng. (alarm after ten seconds). Now A releases its lock but C gets file lock instead of B. I don't know how could it happen. Since I use "F_SETLKW", it sholud wait sequentially.
A, B and C are all independent processes. There is no rule that says that he who starts waiting for a lock first is the first to get it, but there is a rule that says only one can have the lock at any time.