简体   繁体   English

保存进程的内存以备后用?

[英]Save a process' memory for later use?

Is it possible to pause a process, save the memory contents to a file, and then later reload the file so you can continue the program?是否可以暂停进程,将内存内容保存到文件中,然后重新加载文件,以便继续执行程序?

Edit I've been reading about this:编辑我一直在阅读有关此内容的信息:

http://en.wikipedia.org/wiki/Setcontext http://en.wikipedia.org/wiki/Setcontext

Is it possible to dump the contents of the struct, and somehow force malloc to allocate the same memory regions?是否可以转储结构的内容,并以某种方式强制 malloc 分配相同的内存区域?

Technically it is possible, but it would require saving all the system-allocated resources state too - like file descriptors for example and then restoring them.从技术上讲这是可能的,但它也需要保存所有系统分配的资源状态 - 例如文件描述符,然后恢复它们。 So it's a challenging task.所以这是一项具有挑战性的任务。

The easiest way to achieve what you want is to use a virtual machine like VMWare.实现您想要的最简单方法是使用像 VMWare 这样的虚拟机。 When you pause it you actually save the whole machine state together with all programs running.当您暂停它时,您实际上将整个机器状态与所有正在运行的程序一起保存。

This is usually called a persistent continuation .这通常称为持久延续 Some languages like SmallTalk and SBCL have first class support for persistent continuations.一些语言,如 SmallTalk 和 SBCL,对持久性延续提供一流的支持。 Most languages don't.大多数语言都没有。

Depending on your requirements and OS you could try forcing a core dump根据您的要求和操作系统,您可以尝试强制进行核心转储

I have never tried actually loading a core dumped program back up other than in gdb.除了在 gdb 中,我从未尝试过实际加载核心转储程序备份。 It seems like any files you have open or any other state that is not in your programs memory would be lost as sharptooth pointed out.正如sharptooth指出的那样,您打开的任何文件或不在程序内存中的任何其他状态似乎都将丢失。

Another approach would be simply serializing the state you need to disk in your program.另一种方法是简单地序列化您需要在程序中写入磁盘的状态。 It sucks but it is probably the most reliable way unless you are content with suspending execution of the program.这很糟糕,但它可能是最可靠的方法,除非您满足于暂停程序的执行。 That could be done with your operating system's thread library.这可以通过操作系统的线程库来完成。 Or as one poster pointed out with your shell.或者正如一张海报用你的外壳指出的那样。

Well java has serialization and it comes somewhere near to it.好吧,java 有序列化,它接近它。 Though you can't do it to the lowest level like CPU registers memory address etc since this will require os to be in same state that was when you 'paused' the process.尽管您不能像 CPU 寄存器内存地址等那样将其执行到最低级别,因为这将要求 os 处于“暂停”进程时的相同状态。

This can be a good project as a linux kernel module :-)作为 linux 内核模块,这可能是一个很好的项目:-)

Quote from " Persist (hibernate!) a process state to disk for quiker loading " (sic):引用自“ Persist (hibernate!) a process state to disk for quiker loading ”(原文如此):

Q. Can you please explain more on how this swap works so that process state will be saved in disk and reuse when required?"问:您能否详细解释一下这种交换是如何工作的,以便将进程状态保存在磁盘中并在需要时重用?”

A. It's very simple.答:非常简单。 The Page file is a special place on a disk where inactive processes are stored in highly optimized way.页面文件是磁盘上的一个特殊位置,非活动进程以高度优化的方式存储在其中。 When such process resumes running, the system automagically reads it back to memory and it just continues from where it was.当此类进程恢复运行时,系统会自动将其读回内存,并从原来的位置继续运行。 It is sort of how programs run on iPad :)这是程序在 iPad 上运行的方式 :)

All this functionality is already built into Windows.所有这些功能都已内置于 Windows 中。 While your process keeps running, the system ensures that it is either in the memory or in the page file (there are few exceptons though, which you can ignore).当您的进程继续运行时,系统会确保它位于内存或页面文件中(尽管有一些例外,您可以忽略)。

In other words, Windows already has the ability to hibernate a process to the page file.换句话说,Windows 已经具有将进程休眠到页面文件的能力。 @MSalters quote from Raymond Chen " explaining why its impossible " is simply wrong. @MSalters 引用 Raymond Chen 的“ 解释为什么不可能”是完全错误的。

It's messy to the point of being impossible when dealing with native code, as sharptooth mentions.正如sharptooth 提到的那样,在处理本机代码时,它变得混乱到无法实现的地步。

However, some programs (iirc emacs, for instance) have used "dump my own memory" tricks to preserve configuration, instead of dealing with config files.但是,某些程序(例如 iirc emacs)使用“转储我自己的内存”技巧来保留配置,而不是处理配置文件。 This doesn't work on Windows, though, since executables are run in deny-write share mode.但是,这在 Windows 上不起作用,因为可执行文件在拒绝写入共享模式下运行。 But it's a cute (albeit dangerous) trick on linux or DOS :)但它在 linux 或 DOS 上是一个可爱的(虽然很危险)技巧:)

.NET 3.0 及更高版本中的 Workflow Foundation 允许停止和重新启动工作流。

Raymond Chen explains why it's impossible. Raymond Chen 解释了为什么这是不可能的。 Obviously, not all Microsoft engineers read this, because the Visual Studio compiler does this when precompiling headers.显然,并非所有 Microsoft 工程师都会阅读此内容,因为 Visual Studio 编译器在预编译头文件时会这样做。 Its dumps its own state after compiling the headers for the first time, and restores itself to continue.它在第一次编译头文件后转储自己的状态,并恢复自己以继续。

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

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