Has anyone successfully used fcntl() to lock a file?
As far as I know, it works.
The following code open’s and locks a file
then checks to see if it is locked, but
fl.l_pid is never assigned. Whats wrong
with this code.
You have a bug in your creat() call.
// open and lock the file
fd = creat( “/fs/fd/test.txt”, O_WRONLY );
This should not be O_WRONLY, it should be a file permission/mode.
O_WRONLY == 1 == S_IXOTH … your file is created with only
execute-other permissions, so your second creat() call should
fail (unless you’re root) with EACCES.
// open another file desc. and check for lock
fd2 = creat( “/fs/fd/test.txt”, O_WRONLY );
Checking the return code of all system calls is good practice.