求教读写资料加锁
求教读写文件加锁
大家好,现在我要写到一个读写文件的需求,为了保证数据的统一性,要对读写文件加锁,一个进程可以读写,另一个只能读,我对读写文件加锁这一块不了解,请教下大家,要怎么用,有好的例子吗 谢谢啦
------解决方案--------------------
参考http://msdn.microsoft.com/zh-cn/library/w804hcwf(v=vs.80).aspx
------解决方案--------------------
_locking
Locks or unlocks bytes of a file.
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.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
_locking returns 0 if successful. A return value of –1 indicates failure, in which case errno is set to one of the following values:
EACCES
Locking violation (file already locked or unlocked).
EBADF
Invalid file handle.
EDEADLOCK
Locking violation. Returned when the _LK_LOCK or _LK_RLCK flag is specified and the file cannot be locked after 10 attempts.
EINVAL
An invalid argument was given to _locking.
Parameters
handle
File handle
mode
Locking action to perform
nbytes
Number of bytes to lock
Remarks
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.
大家好,现在我要写到一个读写文件的需求,为了保证数据的统一性,要对读写文件加锁,一个进程可以读写,另一个只能读,我对读写文件加锁这一块不了解,请教下大家,要怎么用,有好的例子吗 谢谢啦
------解决方案--------------------
参考http://msdn.microsoft.com/zh-cn/library/w804hcwf(v=vs.80).aspx
------解决方案--------------------
_locking
Locks or unlocks bytes of a file.
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.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
_locking returns 0 if successful. A return value of –1 indicates failure, in which case errno is set to one of the following values:
EACCES
Locking violation (file already locked or unlocked).
EBADF
Invalid file handle.
EDEADLOCK
Locking violation. Returned when the _LK_LOCK or _LK_RLCK flag is specified and the file cannot be locked after 10 attempts.
EINVAL
An invalid argument was given to _locking.
Parameters
handle
File handle
mode
Locking action to perform
nbytes
Number of bytes to lock
Remarks
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.