Windows 中的 msync 等效项

发布于 2024-07-14 19:24:59 字数 249 浏览 7 评论 0原文

Windows 中的 msync [unix sys call] 相当于什么? 我正在寻找 C、C++ 空间中的 MSDN api。 有关 msync 的更多信息,请访问 http://opengroup.org/onlinepubs/007908799/ xsh/msync.html

what is the equivalent of msync [unix sys call] in windows? I am looking for MSDN api in c,C++ space.
More info on msync can be found at http://opengroup.org/onlinepubs/007908799/xsh/msync.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦魇绽荼蘼 2024-07-21 19:24:59

FlushViewOfFile

查看 Python 2.6 mmapmodule.c 以获取使用中的 FlushViewOfFile 和 msync 的示例:

/*
 /  Author: Sam Rushing <[email protected]>
 /  Hacked for Unix by AMK
 /  $Id: mmapmodule.c 65859 2008-08-19 17:47:13Z thomas.heller $

 / Modified to support mmap with offset - to map a 'window' of a file
 /   Author:  Yotam Medini  [email protected]
 /
 / mmapmodule.cpp -- map a view of a file into memory
 /
 / todo: need permission flags, perhaps a 'chsize' analog
 /   not all functions check range yet!!!
 /
 /
 / This version of mmapmodule.c has been changed significantly
 / from the original mmapfile.c on which it was based.
 / The original version of mmapfile is maintained by Sam at
 / ftp://squirl.nightmare.com/pub/python/python-ext.
*/

static PyObject *
mmap_flush_method(mmap_object *self, PyObject *args)
{
    Py_ssize_t offset = 0;
    Py_ssize_t size = self->size;
    CHECK_VALID(NULL);
    if (!PyArg_ParseTuple(args, "|nn:flush", &offset, &size))
        return NULL;
    if ((size_t)(offset + size) > self->size) {
        PyErr_SetString(PyExc_ValueError, "flush values out of range");
        return NULL;
    }
#ifdef MS_WINDOWS
    return PyInt_FromLong((long) FlushViewOfFile(self->data+offset, size));
#elif defined(UNIX)
    /* XXX semantics of return value? */
    /* XXX flags for msync? */
    if (-1 == msync(self->data + offset, size, MS_SYNC)) {
        PyErr_SetFromErrno(mmap_module_error);
        return NULL;
    }
    return PyInt_FromLong(0);
#else
    PyErr_SetString(PyExc_ValueError, "flush not supported on this system");
    return NULL;
#endif
}

更新:
我认为您不会在 win32 映射文件 API 中找到完全同等的情况。 FlushViewOfFile API 没有同步风格(可能是因为缓存管理器的可能影响)。 如果需要精确控制数据何时写入磁盘,也许您可​​以在创建映射文件的句柄时将 FILE_FLAG_NO_BUFFERINGFILE_FLAG_WRITE_THROUGH 标志与 CreateFile API 结合使用?

FlushViewOfFile

Checkout the Python 2.6 mmapmodule.c for an example of FlushViewOfFile and msync in use:

/*
 /  Author: Sam Rushing <[email protected]>
 /  Hacked for Unix by AMK
 /  $Id: mmapmodule.c 65859 2008-08-19 17:47:13Z thomas.heller $

 / Modified to support mmap with offset - to map a 'window' of a file
 /   Author:  Yotam Medini  [email protected]
 /
 / mmapmodule.cpp -- map a view of a file into memory
 /
 / todo: need permission flags, perhaps a 'chsize' analog
 /   not all functions check range yet!!!
 /
 /
 / This version of mmapmodule.c has been changed significantly
 / from the original mmapfile.c on which it was based.
 / The original version of mmapfile is maintained by Sam at
 / ftp://squirl.nightmare.com/pub/python/python-ext.
*/

static PyObject *
mmap_flush_method(mmap_object *self, PyObject *args)
{
    Py_ssize_t offset = 0;
    Py_ssize_t size = self->size;
    CHECK_VALID(NULL);
    if (!PyArg_ParseTuple(args, "|nn:flush", &offset, &size))
        return NULL;
    if ((size_t)(offset + size) > self->size) {
        PyErr_SetString(PyExc_ValueError, "flush values out of range");
        return NULL;
    }
#ifdef MS_WINDOWS
    return PyInt_FromLong((long) FlushViewOfFile(self->data+offset, size));
#elif defined(UNIX)
    /* XXX semantics of return value? */
    /* XXX flags for msync? */
    if (-1 == msync(self->data + offset, size, MS_SYNC)) {
        PyErr_SetFromErrno(mmap_module_error);
        return NULL;
    }
    return PyInt_FromLong(0);
#else
    PyErr_SetString(PyExc_ValueError, "flush not supported on this system");
    return NULL;
#endif
}

UPDATE:
I don't think you are going to find complete parity in the win32 mapped file APIs. The FlushViewOfFile API doesn't have a synchronous flavor (probably because of the possible impact of the cache manager). If precise control over when data is written to disk is required perhaps you can use the FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH flags with the CreateFile API when you create the handle to your mapped file?

若有似无的小暗淡 2024-07-21 19:24:59

用于刷新所有文件映射的 Windows 等效项是

void FlushToHardDrive(LPVOID fileMapAddress,HANDLE hFile)
{
    FlushViewOfFile(fileMappAddress,0); //Async flush of dirty pages
    FlushFileBuffers(hFiles); // flush metadata and wait 
}

对于刷新部分文件映射

void FlushToHardDrive(LPVOID address,DWORD size, HANDLE hFile)
{
    FlushViewOfFile(address,size); //Async flush of region
    FlushFileBuffers(hFiles); // flush metadata and wait 
}

这在 MSDN 这里

标记FILE_FLAG_NO_BUFFERING实际上对内存映射文件没有任何作用(描述此处),也甚至使用这些标志创建文件句柄,元数据文件可以被缓存而不是被刷新,所以如果你想完全确保所有数据(包括文件访问时间)都被保存,FlushFileBuffers对于IO和MM总是需要的。 此处描述了此行为

PS 一些现实世界的示例:SQLite 使用 MM 文件几乎仅用于 阅读,因此当您将 MM 文件用于编写/更新您需要了解此场景的所有副作用

Windows equivalent for flushing all filemapping is

void FlushToHardDrive(LPVOID fileMapAddress,HANDLE hFile)
{
    FlushViewOfFile(fileMappAddress,0); //Async flush of dirty pages
    FlushFileBuffers(hFiles); // flush metadata and wait 
}

And for flushing part of filemapping

void FlushToHardDrive(LPVOID address,DWORD size, HANDLE hFile)
{
    FlushViewOfFile(address,size); //Async flush of region
    FlushFileBuffers(hFiles); // flush metadata and wait 
}

This is described in MSDN here

Flag FILE_FLAG_NO_BUFFERING actually do nothing for memory mapped files (described here), also even file handle is created with these flags, metadata of file can be cached and not flushed, so FlushFileBuffers is always required for IO and MM, if you want to be completely sure that all data (including file access time) is saved. This behavior is described here

P.S. Some real world example: SQLite use MM-files almost only for reading, so when you are using MM-files for write/update you need understand all side effects for this scenario

白云悠悠 2024-07-21 19:24:59

我怀疑 FlushViewOfFile 实际上是正确的。 当我阅读 msync 手册页时,我不会认为它是实际上刷新磁盘缓存(磁盘单元中的缓存,而不是主内存中的系统缓存)。

在磁盘堆栈完成写入之前,FlushViewOfFile 不会返回; 与 msync 文档一样,它没有提及磁盘缓存中发生的情况。 我们或许应该在文档中更清楚地说明这一点。

I suspect FlushViewOfFile actually is the right thing. When I read the man page for msync, I would not assume that it is actually flushing the disk cache (the cache in the disk unit, as opposed to the system cache in main memory).

FlushViewOfFile won't return until the disk stack has completed the writes; like the msync documentation, it says nothing about what happens in the disk cache. We should probably take a look at making that more clear in the documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文