1

I'm new to C and Linux and want to know what does it mean to lock file in linux using C functions lockf, flock and fcntl? since I'm comming from windows background where locked files can never be altered by any other process but for linux when ever I lock file for my process, the file simply can be modified by any other process and even deleted by desktop file browser!

I've used 'lslock' program and my files are all locked using Type/POSIX MODE/WRITE START/0 END/0 PATH/my/file/path

Is locking file in linux has different meaning of locking file on windows where on windows locked file is impossible to be altered by any other process.

Also, I read on the internet that NFS 'linux' dosn't provide lock on file unless a range of bytes is dfined and how to do that?

I've ran through examples of this article https://gavv.github.io/blog/file-locks/

  • 4
    Unix systems generally use `advisory locking`, which depends on everyone playing nice and choosing to look and see if the file is locked and abide by the lock. `Mandatory locking`, where an arbiter enforces the locking, can also be done on a Unix system, but is not the default, I believe, because it can be rather slow compared to advisory locking. – Christian Gibbons May 14 '18 at 23:06
  • Bear in mind with these three functions that they may not work well with each other. Also, `flock` is less portable (it's not a POSIX function) and doesn't work with files over NFS on Linux according to the man page, so you might prefer `lockf` or `fcntl`. For more info regarding these three functions, see [this excellent answer](https://stackoverflow.com/a/22411531/539810). –  May 14 '18 at 23:48

1 Answers1

-2

You should read the man page on flock.

https://linux.die.net/man/2/flock

Some more information about linux file locking (Things aren't quite as bleak as the Author makes it sound):

http://0pointer.de/blog/projects/locking.html

Clarus
  • 2,259
  • 16
  • 27