int _locking( int handle, int mode, long nbytes );
Routine Required Header Optional Headers Compatibility
_locking <io.h> and <sys/locking.h> <errno.h> Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
_locking returns 0 if successful. A return value of –1 indicates failure, in which case errno is set to one of the following values:
Locking violation (file already locked or unlocked).
Invalid file handle.
Locking violation. Returned when the _LK_LOCK or _LK_RLCK flag is specified and the file cannot be locked after 10 attempts.
An invalid argument was given to _locking.
Locking action to perform
Number of bytes to lock
The _locking function locks or unlocks nbytes bytes of the file specified by handle. Locking bytes in a file prevents access to those bytes by other processes. All locking or unlocking begins at the current position of the file pointer and proceeds for the next nbytes bytes. It is possible to lock bytes past end of file.
mode must be one of the following manifest constants, which are defined in LOCKING.H:
Locks the specified bytes. If the bytes cannot be locked, the program immediately tries again after 1 second. If, after 10 attempts, the bytes cannot be locked, the constant returns an error.
Locks the specified bytes. If the bytes cannot be locked, the constant returns an error.
Same as _LK_NBLCK.
Same as _LK_LOCK.
Unlocks the specified bytes, which must have been previously locked.
Multiple regions of a file that do not overlap can be locked. A region being unlocked must have been previously locked. _locking does not merge adjacent regions; if two locked regions are adjacent, each region must be unlocked separately. Regions should be locked only briefly and should be unlocked before closing a file or exiting the program.
/* LOCKING.C: This program opens a file with sharing. It locks
* some bytes before reading them, then unlocks them. Note that the
* program works correctly only if the file exists.