0

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
    @iharob (via [suggested edit](http://stackoverflow.com/review/suggested-edits/6511153)): If the title asks a key part of the question and is rewritten inside of the question, please don't remove it. When first looking at the question, it is easy to ignore the title. It's fine to rephrase the start of the question to make it work fine though (IE, just removing the word 'Title.' would make this a lot nicer, but removing the entire first sentence makes the question unclear). – Pokechu22 Dec 17 '14 at 14:50
  • 1
    If the `man`page doesn't explicitly state what happens it should be easy to find out by trying it. – Klas Lindbäck Dec 17 '14 at 14:52
  • @Pokechu22 Ok sorry, I just thought it was superfluous but your argument is good. – Iharob Al Asimi Dec 17 '14 at 14:53
  • 3
    @Klas: Finding out by trying it assumes that it will always behave the same way. People with experience of undefined behaviour will be able to tell you that's a spectacularly bad idea :-) – paxdiablo Dec 17 '14 at 14:55
  • 2
    Should be an `EINVAL`, though test it too (as it's not sufficiently clear). http://man7.org/linux/man-pages/man2/flock.2.html – Deduplicator Dec 17 '14 at 14:56

1 Answers1

1

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().

Dummy00001
  • 16,630
  • 5
  • 41
  • 63