UID:1268
UID:534
UID:1093
UID:2
Header Fileio.h CategoryInput/output RoutinesPrototypeint lock(int handle, long offset, long length);DescriptionSets file-sharing locks.lock provides an interface to the operating system file-sharing mechanism.A 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 Valuelock returns 0 on success. On error, lock returns -1 and sets the global variable errno toEACCES Locking violation
#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; handle = _sopen("c:\\autoexec.bat", O_RDONLY,SH_DENYNO,S_IREAD); if (handle < 0) { printf("_sopen failed\n"); exit(1); } length = filelength(handle); status = lock(handle,0L,length/2); if (status == 0) printf("lock succeeded\n"); else printf("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;}