Unknown
Unknown

Reputation: 46791

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

Is it possible to dump the contents of the struct, and somehow force malloc to allocate the same memory regions?

Upvotes: 36

Views: 19309

Answers (9)

OctaveL
OctaveL

Reputation: 1045

Existing answers to this rather old thread range from "impossible" to "extremely hard", and admittedly it might not be an option on Windows (I think?), but these days you can certainly do it on Linux: https://github.com/checkpoint-restore/criu

Upvotes: 0

sharptooth
sharptooth

Reputation: 170489

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. When you pause it you actually save the whole machine state together with all programs running.

Upvotes: 29

Guillaume
Guillaume

Reputation: 18865

This is usually called a persistent continuation. Some languages like SmallTalk and SBCL have first class support for persistent continuations. Most languages don't.

Upvotes: 9

Contango
Contango

Reputation: 80282

Quote from "Persist (hibernate!) a process state to disk for quiker loading" (sic):

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 :)

All this functionality is already built into 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. @MSalters quote from Raymond Chen "explaining why its impossible" is simply wrong.

Upvotes: 2

MSalters
MSalters

Reputation: 179927

Raymond Chen explains why it's impossible. Obviously, not all Microsoft engineers read this, because the Visual Studio compiler does this when precompiling headers. Its dumps its own state after compiling the headers for the first time, and restores itself to continue.

Upvotes: 0

fuzzy-waffle
fuzzy-waffle

Reputation: 830

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. 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.

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.

Upvotes: 5

snemarch
snemarch

Reputation: 5008

It's messy to the point of being impossible when dealing with native code, as sharptooth mentions.

However, some programs (iirc emacs, for instance) have used "dump my own memory" tricks to preserve configuration, instead of dealing with config files. This doesn't work on Windows, though, since executables are run in deny-write share mode. But it's a cute (albeit dangerous) trick on linux or DOS :)

Upvotes: 2

Xolve
Xolve

Reputation: 23200

Well java has serialization and it comes somewhere near to it. 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.

This can be a good project as a linux kernel module :-)

Upvotes: 2

Jonathan Parker
Jonathan Parker

Reputation: 6795

Workflow Foundation in .NET 3.0 and higher allows for workflows to be stopped and restarted.

Upvotes: 0

Related Questions