After a research I made, I'm still not sure if there are APIs that allows you to open file exclusively, meaning, any other process would not be able to right to the file.
Please can someone give me a good reference/guide/note/manual that covers this topic?
Thanks a lot!
Edit: Advisory Locking is good enough.
There are three primary systems for file locking between processes:
fcntl()
lockf()
flock()
Some Unix-like systems might not have
flock()
; they might havelockf()
instead, for example, or they might only havefcntl()
locking (though most will have bothlockf()
andfcntl()
and many will haveflock()
too). The current version of POSIX mandatesfcntl()
locking andlockf()
locking for file-level inter-process locking. (POSIX also definesflockfile()
,funlockfile()
andftrylockfile()
— which are used for controlling locking between threads in an application).AFAIK, you can implement both
lockf()
andflock()
locking usingfcntl()
locking.Note that the locking functions work on file descriptors or file streams. Each program will be able to open the file, but will then apply advisory locking calls to check whether it has exclusive access to the file.
Note that some systems support mandatory file locking (indicated by setting the setgid bit on a file while the corresponding group execute bit is not set — so 2644 mode, for example). However, Mac OS X does not support mandatory locking (10.10 Yosemite tested, but prior versions also have this limitation). POSIX does not require mandatory locking support. It was provided on SVR4 systems.