File LockingAn important issue in any system with multiple processes is coordination and synchronization of access to shared objects, such as files. Windows can lock files, in whole or in part, so that no other process (running program) can access the locked file region. File locks can be read-only (shared) or read-write (exclusive). Most important, the locks belong to the process. Any attempt to access part of a file (using ReadFile or WriteFile) in violation of an existing lock will fail because the locks are mandatory at the process level. Any attempt to obtain a conflicting lock will also fail even if the process already owns the lock. File locking is a limited form of synchronization between concurrent processes and threads; synchronization is covered in much more general terms starting in Chapter 8. The most general function is LockFileEx. The less general function, LockFile, can be used on Windows 9x. LockFileEx is a member of the extended I/O class of functions, so the overlapped structure, used earlier to specify file position to ReadFile and WriteFile, is required to specify the 64-bit file position and range of the file region that is to be locked.
LockFileEx locks a byte range in an open file for either shared (multiple readers) or exclusive (one reader-writer) access. ParametershFile is the handle of an open file. The handle must have GENERIC_READ or both GENERIC_READ and GENERIC_WRITE file access. dwFlags determines the lock mode and whether to wait for the lock to become available. LOCKFILE_EXCLUSIVE_LOCK, if set, indicates a request for an exclusive, read-write lock. Otherwise, it requests a shared (read-only) lock. LOCKFILE_FAIL_IMMEDIATELY, if set, specifies that the function should return immediately with FALSE if the lock cannot be acquired. Otherwise, the call blocks until the lock becomes available. dwReserved must be 0. The two parameters with the length of the byte range are self-explanatory. lpOverlapped points to an OVERLAPPED data structure containing the start of the byte range. The overlapped structure contains three data members that must be set (the others are ignored); the first two determine the start location for the locked region.
A file lock is removed using a corresponding UnlockFileEx call; all the same parameters are used except dwFlags.
You should consider several factors when using file locks.
Table 3-1 shows the lock logic when all or part of a range already has a lock. This logic applies even if the lock is owned by the same process that is making the new request.
Table 3-2 shows the logic when a process attempts a read or write operation on a file region with one or more locks, owned by a separate process, on all or part of the read-write region. A failed read or write may take the form of a partially completed operation if only a portion of the read or write record is locked.
Read and write operations are normally in the form of ReadFile and WriteFile calls or their extended versions, ReadFileEx and WriteFileEx. Diagnosing a read or write failure requires calling GetLastError. Accessing memory that is mapped to a file is another form of file I/O, as will be discussed in Chapter 5. Lock conflicts are not detected at the time of memory reference; rather, they are detected at the time that the MapViewOfFile function is called. This function makes a part of the file available to the process, so the lock must be checked at that time. The LockFile function is a limited, special case and is a form of advisory locking. It can be used on Windows 9x, which does not support LockFileEx. Only exclusive access is available, and LockFile returns immediately. That is, LockFile does not block. Test the return value to determine whether you obtained the lock. Releasing File LocksEvery successful LockFileEx call must be followed by a single matching call to UnlockFileEx (the same is true for LockFile and UnlockFile). If a program fails to release a lock or holds the lock longer than necessary, other programs may not be able to proceed, or, at the very least, their performance will be negatively impacted. Therefore, programs should be carefully designed and implemented so that locks are released as soon as possible, and logic that might cause the program to skip the unlock should be avoided. Termination handlers (Chapter 4) are a useful way to ensure that the unlock is performed. Lock Logic ConsequencesAlthough the file lock logic shown in Tables 3-1 and 3-2 is natural, it has consequences that may be unexpected and cause unintended program defects. Here are some examples.
Using File LocksFile locking examples are deferred until Chapter 6, which covers process management. Program 4-2, 6-4, 6-5, and 6-6 use locks to ensure that only one process at a time can modify a file.
|
Saturday, November 7, 2009
File Locking
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment