gpt4 book ai didi

windows - Windows 中的 msync 等效项

转载 作者:行者123 更新时间:2023-12-03 11:13:47 30 4
gpt4 key购买 nike

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

最佳答案

FlushViewOfFile

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

/*
/ Author: Sam Rushing <rushing@nightmare.com>
/ 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 yotamm@mellanox.co.il
/
/ 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 一起使用?

关于windows - Windows 中的 msync 等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/618046/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com