8.3 fcntl: Locks and Other File Operations

The fcntl system call is the access point for several advanced operations on file descriptors. The first argument to fcntl is an open file descriptor, and the second is a value that indicates which operation is to be performed. For some operations, fcntl takes an additional argument. We'll describe here one of the most useful fcntl operations, file locking. See the fcntl man page for information about the others.

The fcntl system call allows a program to place a read lock or a write lock on a file, somewhat analogous to the mutex locks discussed in Chapter 5, "Interprocess Communication." A read lock is placed on a readable file descriptor, and a write lock is placed on a writable file descriptor. More than one process may hold a read lock on the same file at the same time, but only one process may hold a write lock, and the same file may not be both locked for read and locked for write. Note that placing a lock does not actually prevent other processes from opening the file, reading from it, or writing to it, unless they acquire locks with fcntl as well.

To place a lock on a file, first create and zero out a struct flock variable. Set the l_type field of the structure to F_RDLCK for a read lock or F_WRLCK for a write lock. Then call fcntl, passing a file descriptor to the file, the F_SETLCKW operation code, and a pointer to the struct flock variable. If another process holds a lock that prevents a new lock from being acquired, fcntl blocks until that lock is released.

The program in Listing 8.2 opens a file for writing whose name is provided on the command line, and then places a write lock on it. The program waits for the user to hit Enter and then unlocks and closes the file.

Listing 8.2 (lock-file.c) Create a Write Lock with fcntl
#include <fcntl.h> 
#include <stdio.h> 
#include <string.h> 
#include <unistd.h> 
int main (int argc, char* argv[]) 
  char* file = argv[1]; 
  int fd; 
  struct flock lock; 
  printf ("opening %s\n", file); 
  /* Open a file descriptor to the file.  */ 
  fd = open (file, O_WRONLY); 
  printf ("locking\n"); 
  /* Initialize the flock structure.  */ 
  memset (&lock, 0, sizeof(lock)); 
  lock.l_type = F_WRLCK; 
  /* Place a write lock on the file.  */ 
  fcntl (fd, F_SETLKW, &lock); 
  printf ("locked; hit Enter to unlock... "); 
  /* Wait for the user to hit Enter. */ 
  getchar (); 
  printf ("unlocking\n"); 
  /* Release the lock. */ 
  lock.l_type = F_UNLCK; 
  fcntl (fd, F_SETLKW, &lock); 
  close (fd); 
  return 0; 

Compile and run the program on a test file—say, /tmp/test-file—like this:

% cc -o lock-file lock-file.c 
% touch /tmp/test-file 
% ./lock-file /tmp/test-file 
opening /tmp/test-file 
locked; hit Enter to unlock... 

Now, in another window, try running it again on the same file.

% ./lock-file /tmp/test-file 
opening /tmp/test-file 

Note that the second instance is blocked while attempting to lock the file. Go back to the first window and press Enter:


The program running in the second window immediately acquires the lock.

If you prefer fcntl not to block if the call cannot get the lock you requested, use F_SETLK instead of F_SETLKW. If the lock cannot be acquired, fcntl returns -1 immediately.

Linux provides another implementation of file locking with the flock call. The fcntl version has a major advantage: It works with files on NFS [3] file systems (as long as the NFS server is reasonably recent and correctly configured). So, if you have access to two machines that both mount the same file system via NFS, you can repeat the previous example using two different machines. Run lock-file on one machine, specifying a file on an NFS file system, and then run it again on another machine, specifying the same file. NFS wakes up the second program when the lock is released by the first program.

[3] Network File System (NFS) is a common network file sharing technology, comparable to Windows' shares and network drives.