views:

450

answers:

11

Hi,

Is it possible to 'hibernate' a process in linux? Just like 'hibernate' in laptop, I would to write all the memory used by a process to disk, free up the RAM. And then later on, I can 'resume the process', i.e, reading all the data from memory and put it back to RAM and I can continue with my process?

Thank you.

A: 

There's ctrl+z in linux, but i'm not sure it offers the features you specified. I suspect you asked this question since it doesn't

Simon Walker
+3  A: 

The short answer is "yes." You might start by looking at this for some ideas: ELF executable reconstruction from a core image (http://vx.netlux.org/lib/vsc03.html)

fullreset
Interesting link; but the link does point out it doesn't work reliably
Will
I did say this was a /starting/ point. :)
fullreset
A: 

ctrl+z to hibernate

$fg to wake up

Aito
A: 

Ctrl-Z increases the chances the process's pages will be swapped, but it doesn't free the process's resources completely. The problem with freeing a process's resources completely is that things like file handles, sockets are kernel resources the process gets to use, but doesn't know how to persist on its own. So Ctrl-Z is as good as it gets.

Tobu
+4  A: 

The issue is restoring the streams - files and sockets - that the program has open.

When your whole OS hibernates, the local files and such can obviously be restored. Network connections don't, but then the code that accesses the internet is typically more error checking and such and survives the error conditions (or ought to).

If you did per-program hibernation (without application support), how would you handle open files? What if another process accesses those files in the interim? etc?

Maintaining state when the program is not loaded is going to be difficult.

Simply suspending the threads and letting it get swapped to disk would have much the same effect?

Or run the program in a virtual machine and let the VM handle suspension.

Will
A: 

There was some research on checkpoint/restore for Linux back in 2.2 and 2.4 days, but it never made it past prototype. It is possible (with the caveats described in the other answers) for certain values of possible - I you can write a kernel module to do it, it is possible. But for the common value of possible (can I do it from the shell on a commercial Linux distribution), it is not yet possible.

florin
+3  A: 

Short answer is "yes, but not always reliably". Check out CryoPID:

http://cryopid.berlios.de/

Open files will indeed be the most common problem. CryoPID states explicitly:

Open files and offsets are restored. Temporary files that have been unlinked and are not accessible on the filesystem are always saved in the image. Other files that do not exist on resume are not yet restored. Support for saving file contents for such situations is planned.

The same issues will also affect TCP connections, though CryoPID supports tcpcp for connection resuming.

Ulisses Montenegro
After hitting the submit button I now realize this reads a lot like spam/advertisement for CryoPID. It is not -- I am simply a satisfied user of the utility, really.
Ulisses Montenegro
A: 

This is sort of the ultimate goal of clustered operating system. Mathew Dillon puts a lot of effort to implement something like this in his Dragonfly BSD project.

Nikolai N Fetissov
+1  A: 

As others have noted, it's difficult for the OS to provide this functionality, because the application needs to have some error checking builtin to handle broken streams.

However, on a side note, some programming languages and tools that use virtual machines explicitly support this functionality, such as the Self programming language.

Chris S
+4  A: 

I used to maintain CryoPID, which is a program that does exactly what you are talking about. It writes the contents of a program's address space, VDSO, file descriptor references and states to a file that can later be reconstructed. CryoPID started when there were no usable hooks in Linux itself and worked entirely from userspace (actually, it still does work, depending on your distro / kernel / security settings).

Problems were (indeed) sockets, pending RT signals, numerous X11 issues, the glibc caching getpid() implementation amongst many others. Randomization (especially VDSO) turned out to be insurmountable for the few of us working on it after Bernard walked away from it. However, it was fun and became the topic of several masters thesis.

If you are just contemplating a program that can save its running state and re-start directly into that state, its far .. far .. easier to just save that information from within the program itself, perhaps when servicing a signal.

Tim Post
A: 

The answers mentioning ctrl-z are really talking about stopping the process with a signal, in this case SIGTSTP. You can issue a stop signal with kill:

kill -STOP <pid>

That will suspend execution of the process. It won't immediately free the memory used by it, but as memory is required for other processes the memory used by the stopped process will be gradually swapped out.

When you want to wake it up again, use

kill -CONT <pid>

The more complicated solutions, like CryoPID, are really only needed if you want the stopped process to be able to survive a system shutdown/restart - it doesn't sound like you need that.

caf