21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Locking files on Windows<br />

Where Unix falls flat on its face with respect to supporting file locking, Windows<br />

gets it right. Windows supports only mandatory file locks, and it fully enforces them.<br />

If a process has a lock on a file or a portion of a file, another process cannot mistakenly<br />

or maliciously steal that lock.<br />

Windows provides four functions for locking and unlocking files. Two functions,<br />

LockFile( ) and LockFileEx( ), are provided for engaging locks, and two functions,<br />

UnlockFile( ) and UnlockFileEx( ), are provided for removing them.<br />

Neither LockFile( ) nor UnlockFile( ) will return until the lock can be successfully<br />

obtained or released, respectively. LockFileEx( ) and UnlockFileEx( ), however, can<br />

be called in such a way that they will always return immediately, either returning failure<br />

or signalling an event object when the requested operation completes.<br />

Locks can be placed on a file in its entirety or on a portion of a file. A single file may<br />

have multiple locks owned by multiple processes so long as none of the locks overlap.<br />

When removing a lock, you must specify the exact portion of the file that was<br />

locked. For example, two locks covering contiguous portions of a file may not be<br />

removed with a single unlock operation that spans the two locks.<br />

When a lock is held on a file, closing the file does not necessarily<br />

remove the lock. The behavior is actually undefined and may vary<br />

across different filesystems and versions of Windows. Always make<br />

sure to remove any locks on a file before closing it.<br />

There are two types of locks on Windows:<br />

Shared lock<br />

This type of lock allows other processes to read from the locked portion of the<br />

file, while denying all processes—including the process that obtained the lock—<br />

permission to write to the locked portion of the file.<br />

Exclusive lock<br />

This type of lock denies other processes both read and write access to the locked<br />

portion of the file, while allowing the locking process to read or write to the<br />

locked portion of the file.<br />

Using LockFile( ) to obtain a lock always obtains an exclusive lock. However,<br />

LockFileEx( ) obtains a shared lock unless the flag LOCKFILE_EXCLUSIVE_LOCK is specified.<br />

Here are the signatures for LockFile and UnlockFile( ):<br />

BOOL LockFile(HANDLE hFile, DWORD dwFileOffsetLow,<br />

DWORD dwFileOffsetHigh, DWORD nNumberOfBytesToLockLow,<br />

DWORD nNumberOfBytesToLockHigh);<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

Locking Files | 59

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!