What happens if we try to unlock an already unlocked file using flock? C lang

257 Views Asked by At

Title. What happens if we try to unlock an already unlocked file using flock? That is to say, when we already used a flock(file, LOCK_UN) and we try to use it again!

This in C. Is the behavior unexpected? Does it give an error? Does it do nothing?

Thanks!

1

There are 1 best solutions below

0
On BEST ANSWER

The flock() is part of BSD, otherwise not standardized, and specified only as good as it is specified in the BSD documentation.

Yet, it is a real kernel syscall and as such is capable of detecting all types of misbehavior on part of the user-space applications. Including the double unlock.

If you want a well defined, yet not widely supported function, check the POSIX' lockf().