lock
raw OCR
Sets file-sharing locks.
Defined in header <io.h>
Syntax
#include <io.h>
int lock(int handle, long offset, long length);
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ | ■ |
Remarks
lock provides an interface to the DOS 3.x file-sharing mechanism.
| SHARE.EXE | must be loaded before using lock. |
lock can be placed on arbitrary, nonoverlapping regions of any file. A program attempting to read or write into a locked region will retry the operation three times. If all three retries fail, the call fails with an error.
Return value
lock returns 0 on success. On error, lock returns -1 and sets the global variable errno to
EACCES Locking violation
See also
Example
#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <process.h>
#include <share.h>
#include <stdio.h>
int main (void)
{
int handle, status;
long length;
/* must have DOS SHARE.EXE loaded for file locking to function */
handle = sopen("c:\\autoexec.bat" , 0_RDONLY,SH_DENYNO,S_IREAD);
if (handle < 0) {
printf ("sopen failed\n");
exit(l);
}
length = filelength (handle);
status = lock(handle, 0L,length/2);
if (status == 0)
printfC'lock succeeded\n") ;
else
printfC'lock failed\n");
status = unlock (handle,0L,length/2);
if (status == 0)
printf("unlock succeeded\n" );
else
printf("unlock failed\n");
close (handle);
return 0;
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Stable link: /3.1/io.h/lock/
· short form /3.1/lock/