我有两个应用程序共享同一个锁文件,我需要知道另一个应用程序何时锁定/解锁该文件。以下代码最初在Linux机器上实现,并被移植到Windows 8、VS12。
我已成功地移植了类中的所有其他代码,并使用LockFile(handle, 0, 0, sizeof(int), 0)和相应的UnlockFile(...)锁定文件。然而,我在以下wait()命令方面遇到了麻烦。
注意:m_iLockFile 曾经是 open() 函数返回的文件描述符,现在已更名为 m_hLockFile,并且是 CreateFile() 函数返回的 HANDLE。
我无法找到 Windows 上等价于 fcntl F_GETLK 命令的函数。 有人知道是否可以: a)使用类似 fcntl 的函数来查询锁定信息,以查明哪个进程已获得了锁定 b)建议如何将上述代码重写适用于 Windows C++。
注意:使用锁文件的服务器应用程序是一个独立的 C++ 可执行文件, 但使用锁文件的客户端是 WinRT Windows 应用程序。因此,任何建议的解决方案都不能破坏客户端的沙箱环境。
谢谢。
我已成功地移植了类中的所有其他代码,并使用LockFile(handle, 0, 0, sizeof(int), 0)和相应的UnlockFile(...)锁定文件。然而,我在以下wait()命令方面遇到了麻烦。
bool devices::comms::CDeviceFileLock::wait(bool locked,
int timeout)
{
// Retrieve the current pid of the process.
pid_t pid = getpid();
// Determine if we are tracking time.
bool tracking = (timeout > 0);
// Retrieve the lock information.
struct flock lock;
if (fcntl(m_iLockFile, F_GETLK, &lock) != 0)
raiseException("Failed to retrieve lock file information");
// Loop until the state changes.
time_t timeNow = time(NULL);
while ((pid == lock.l_pid)
&&
(lock.l_type != (locked ? F_WRLCK : F_UNLCK)))
{
// Retrieve the lock information.
if (fcntl(m_iLockFile, F_GETLK, &lock) != 0)
raiseException("Failed to retrieve lock file information");
// Check for timeout, if we are tracking.
if (tracking)
{
time_t timeCheck = time(NULL);
if (difftime(timeNow, timeCheck) > timeout)
return false;
}
}
// Return success.
return true;
}
注意:m_iLockFile 曾经是 open() 函数返回的文件描述符,现在已更名为 m_hLockFile,并且是 CreateFile() 函数返回的 HANDLE。
我无法找到 Windows 上等价于 fcntl F_GETLK 命令的函数。 有人知道是否可以: a)使用类似 fcntl 的函数来查询锁定信息,以查明哪个进程已获得了锁定 b)建议如何将上述代码重写适用于 Windows C++。
注意:使用锁文件的服务器应用程序是一个独立的 C++ 可执行文件, 但使用锁文件的客户端是 WinRT Windows 应用程序。因此,任何建议的解决方案都不能破坏客户端的沙箱环境。
谢谢。