简体   繁体   English

如果计算机在保留内存映射文件时挂起,会发生什么情况?

[英]What happens if computer hangs while persisting a memory-mapped file?

I'm very interested in using managed memory-mapped files available since .NET 4.0 . 我对使用自.NET 4.0之后可用的托管内存映射文件非常感兴趣。

Check the following statement extracted from MSDN: 检查从MSDN提取的以下语句:

Persisted files are memory-mapped files that are associated with a source file on a disk. 持久文件是与磁盘上的源文件关联的内存映射文件。 When the last process has finished working with the file, the data is saved to the source file on the disk. 当最后一个过程处理完文件后,数据将保存到磁盘上的源文件中。 These memory-mapped files are suitable for working with extremely large source files. 这些内存映射文件适用于处理非常大的源文件。

My question is: what happens if computer hangs while persisting a memory-mapped file? 我的问题是: 如果计算机在保留内存映射文件的同时挂起,会发生什么情况?

I mean, since memory-mapped files are stored in virtual memory (I understand that this is in the page file), maybe a file can be restored from virtual memory and try to store it again to the source file after restarting Windows. 我的意思是,由于内存映射文件存储在虚拟内存中(我知道这在页面文件中),因此也许可以从虚拟内存中还原文件,然后在重新启动Windows之后尝试将其再次存储到源文件中。

The data pages that underlie a memory mapped file reside in the OS cache (file cache). 内存映射文件基础的数据页位于OS缓存(文件缓存)中。 Whenever you shutdown Windows it writes all modified cache pages to the file system. 每当您关闭Windows时,它都会将所有已修改的缓存页面写入文件系统。

The pages in the cache are either ordinary file data (from processes doing reads/writes from/to files) or memory mapped pages which are read/written by the paging system. 高速缓存中的页面要么是普通文件数据(来自对文件进行读/写的过程),要么是由分页系统读取/写入的内存映射页面。

If Windows is unable (eg crashes or freezes) to flush cache contents to disk then that data is lost. 如果Windows无法(例如崩溃或死机)将缓存内容刷新到磁盘,则该数据将丢失。

If enable persistence , memory map file not remove after reboot . 如果启用持久性,则重新启动后不会删除内存映射文件。

you can use atomic action process with a flag that show data is valid or not if valid you can restore else data lost 您可以使用带有标志的原子动作过程来显示数据是否有效(如果有效),您可以恢复其他数据丢失

If your os support (kernel or filesystem lifetime) like unix you can use share memory with synchronisation that is more faster than map file 如果您的操作系统支持(内核或文件系统生存期),例如unix,则可以使用共享内存进行同步,其同步速度比映射文件更快

Modern Operating Systems 3e (2007) book memory map file: Shared libraries are really a special case of a more general facility called memory-mapped files. 现代操作系统3e(2007)预订了内存映射文件:共享库实际上是称为内存映射文件的更通用工具的特例。 The idea here is that a process can issue a system call to map a file onto a portion of its virtual address space. 这里的想法是,进程可以发出系统调用以将文件映射到其虚拟地址空间的一部分。 In most implementations, no pages are brought in at the time of the mapping, but as pages are touched, they are demand paged in one at a time, using the disk file as the backing store. 在大多数实现中,映射时不会插入任何页面,但是在触摸页面时,需要使用磁盘文件作为后备存储一次将它们分页。 When the process exits, or explicitly unmaps the file, all the modified pages are written back to the file. 当进程退出或显式取消映射文件时,所有修改后的页面都将写回到文件中。 Mapped files provide an alternative model for I/O. 映射文件为I / O提供了替代模型。 Instead of doing reads and writes, the file can be accessed as a big character array in memory. 无需进行读写操作,文件可以作为内存中的大字符数组进行访问。 In some situations, programmers find this model more convenient. 在某些情况下,程序员发现此模型更方便。 If two or more processes map onto the same file at the same time, they can communicate over shared memory. 如果两个或多个进程同时映射到同一文件,则它们可以通过共享内存进行通信。 Writes done by one process to the shared memory are immediately visible when the other one reads from the part of its virtual address spaced mapped onto the file. 当另一进程从其虚拟地址中映射到文件的部分虚拟地址读取内容时,即可立即看到该进程对共享内存的写操作。 This mechanism thus provides a high bandwidth channel between processes and is often used as such (even to the extent of mapping a scratch file). 因此,此机制在进程之间提供了高带宽通道,并且经常被使用(甚至在映射暂存文件的范围内)。 Now it should be clear that if memory-mapped files are available, shared libraries can use this mechanism 现在应该清楚,如果有内存映射文件可用,则共享库可以使用此机制

In http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2044.html http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2044.html

Shared Memory 共享内存

POSIX defines a shared memory object as "An object that represents memory that can be mapped concurrently into the address space of more than one process." POSIX将共享内存对象定义为“一个表示可以同时映射到多个进程的地址空间中的内存的对象”。 Shared memory is similar to file mapping, and the user can map several regions of a shared memory object, just like with memory mapped files. 共享内存类似于文件映射,并且用户可以映射共享内存对象的多个区域,就像使用内存映射文件一样。 In some operating systems, like Windows, shared memory is an special case of file mapping, where the file mapping object accesses memory backed by the system paging file. 在某些操作系统(如Windows)中,共享内存是文件映射的特例,其中文件映射对象访问由系统页面文件支持的内存。 However, in Windows, the lifetime of this memory ends when the last process connected to the shared memory object closes connection or the application exits, so there is no data persistence. 但是,在Windows中,当连接到共享内存对象的最后一个进程关闭连接或应用程序退出时,该内存的生存期结束,因此没有数据持久性。 If an application creates shared memory, fills it with data and exits, the data is lost. 如果应用程序创建共享内存,将其填充数据并退出,则数据将丢失。 This lifetime is known as process lifetime In POSIX operating systems the shared memory lifetime is different since for semaphores, shared memory, and message queues it's mandatory that the object and its state (including data, if any) is preserved after the object is no longer referenced by any process. 此生存期称为进程生存期。在POSIX操作系统中,共享内存的生存期有所不同,因为对于信号量,共享内存和消息队列,必须在不再使用对象后保留对象及其状态(包括数据,如果有的话)由任何过程引用。 Persistence of an object does not imply that the state of the object is preserved after a system crash or reboot, but this can be achieved since shared memory objects can actually be implemented as mapped files of a permanent file system. 对象的持久性并不意味着在系统崩溃或重新启动后就可以保留对象的状态,但这可以实现,因为共享内存对象实际上可以实现为永久文件系统的映射文件。 The shared memory destruction happens with an explicit call to unlink(), which is similar to the file destruction mechanism. 显式调用unlink()会发生共享内存破坏,这类似于文件破坏机制。 POSIX shared memory is required to have kernel lifetime (the object is explicitly destroyed or it's destroyed when the operating system reboots) or filesystem persistence (the shared memory object has the same lifetime as a file). POSIX共享内存必须具有内核生存期(对象被显式销毁或在操作系统重新启动时销毁)或文件系统持久性(共享内存对象与文件具有相同的生存期)。 This lifetime difference is important to achieve portability. 此生命周期差异对于实现可移植性很重要。 Many portable runtimes have tried to achieve perfect portability between Windows and POSIX shared memory but the author of this paper has not seen any satisfactory effort. 许多可移植的运行时都试图在Windows和POSIX共享内存之间实现完美的可移植性,但是本文的作者并未看到任何令人满意的成果。 Adding a reference count to POSIX shared memory is effective only as long as a process does not crash, something that it's very usual. 仅在进程不崩溃的情况下,将引用计数添加到POSIX共享内存中才有效,这是很常见的事情。 Emulating POSIX behaviour in Windows using native shared memory is not possible since we could try to dump shared memory to a file to obtain persistence, but a process crash would avoid persistence. 在Windows中无法使用本机共享内存模拟POSIX行为,因为我们可以尝试将共享内存转储到文件中以获得持久性,但是进程崩溃将避免持久性。 The only viable alternative is to use memory mapped files in Windows simulating shared memory, but avoiding file-memory synchronization as much as possible. 唯一可行的选择是在Windows中使用内存映射文件模拟共享内存,但要尽可能避免文件内存同步。 Many other named synchronization primitives (like named mutexes or semaphores) suffer the same lifetime portability problem. 许多其他命名的同步原语(例如命名的互斥体或信号量)也遭受相同的生存期可移植性问题。 Automatic shared memory cleanup is useful in many contexts, like shared libraries or DLL-s communicating with other DLL-s or processes. 自动共享内存清理在许多情况下都很有用,例如共享库或DLL与其他DLL或进程进行通信。 Even when there is a crash, resources are automatically cleaned up by the operating systems. 即使发生崩溃,操作系统也会自动清理资源。 POSIX persistence is also useful when a launcher program can create and fill shared memory that another process can read or modify. 当启动程序可以创建和填充其他进程可以读取或修改的共享内存时,POSIX持久性也很有用。 Persistence also allows data recovery if a server process crashes. 如果服务器进程崩溃,持久性还允许恢复数据。 All the data is still in the shared memory, and the server can recover its state. 所有数据仍保留在共享内存中,服务器可以恢复其状态。 This paper proposes POSIX lifetime (kernel or filesystem lifetime) as a more portable solution, but has no strong opinion about this. 本文提出了POSIX生存期(内核或文件系统生存期)作为更可移植的解决方案,但对此没有强烈的意见。 The C++ committee should take into account the use cases of both approaches to decide which behaviour is better or if both options should be available, forcing the modification of both POSIX and Windows systems. C ++委员会应考虑这两种方法的用例,以确定哪种行为更好,或者两种选项是否都可用,从而迫使同时修改POSIX和Windows系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM