www.delorie.com/djgpp/doc/libc/libc_534.html   search  
libc.a reference

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]



#include <unistd.h>

int llockf (int fildes, int function, offset_t size);


The llockf function is a simplified interface to the locking facilities of fcntl (see fcntl, for more detailed information).

The llockf function performs exactly the same functions as lockf (see section lockf), with exactly the same input commands and results, but the input size parameter is of type offset_t instead of off_t, and the fcntl calls are made using functions F_GETLK64, F_SETLK64 and F_SETLKW64, where lockf uses functions F_GETLK, F_SETLK and F_SETLKW.

The llockf function is intended to permit using fcntl functions with size values greater than 2^31 - 1.

fildes is an open file descriptor.

function is a control value which specifies the action to be taken. The permissible values for function are defined in <unistd.h> as follows:

#define   F_ULOCK   0   /* Unlock a previously locked section */
#define   F_LOCK    1   /* Lock a section for exclusive use */
#define   F_TLOCK   2   /* Test and lock a section for exclusive use */
#define   F_TEST    3   /* Test section for other locks */

All other values of function are reserved for future extensions and will result in an error return if not implemented, with errno set to EINVAL.

size is the number of contiguous bytes to be locked or unlocked. The resource to be locked starts at the current offset in the file and extends forward for a positive size and backward for a negative size (the preceding bytes up to but not including the current offset). If size is zero, the section from the current offset through the largest file offset is locked (i.e. from the current offset through the end of file).

The functions defined for llockf are as follows:

This function is used to detect if the specified section is already locked.

F_LOCK and F_TLOCK both lock a section of a file, if the section is available. These two function requests differ only by the action taken if the resource is not available. F_LOCK will cause the calling program to wait until the resource is available. F_TLOCK will cause the function to return a -1 and set errno to EACCES if the section is already locked.

F_ULOCK removes locks from a section of the file. This function will release locked sections controlled by the program.

The llockf function will fail, returning -1 and setting errno to the following error values if the associated condition is true:

Parameter fildes is not a valid open file.

Parameter function is F_TLOCK or F_TEST and the section is already locked.

Parameter function is not one of the implemented functions. Or: An attempt was made to lock a directory, which is not supported.

All lock requests in this implementation are coded as exclusive locks (i.e. all locks use the fcntl request F_WRLCK).

It is therefore wise to code llockf by using function F_TLOCK with all lock requests, and to test the return value to determine if the lock was obtained or not. Using F_TLOCK will cause the implementation to use F_SETLK instead of F_SETLKW, which will return an error if the lock cannot be obtained.

Return Value

On success, zero is returned. On error, -1 is returned, and errno is set appropriately, as described above.




 /* Request a lock on file handle fd from the current position to
    the end of the file */
  errno = 0;
  retval = llockf(fd, F_LOCK, 0);

 /* Request a non-blocking lock on file handle fd */
  errno = 0;
  retval = llockf(fd, F_TLOCK, 0);

 /* Test a lock on file handle fd */
  errno = 0;
  retval = llockf(fd, F_TEST, 0);

 /* Release a lock on file handle fd */
  errno = 0;
  retval = llockf(fd, F_ULOCK, 0);

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2004   by DJ Delorie     Updated Apr 2004