views:

1205

answers:

5

It just hit me the other day. What actually happens when I tell the computer to go into Stand-By or to Hibernate?

More spesifically, what implications, if any, does it have on code that is running? For example if an application is compressing some files, encoding video files, checking email, running a database query, generating reports or just processing lots of data or doing complicated math stuff. What happens? Can you end up with a bug in your video? Can the database query fail? Can data processing end up containing errors?

I'm asking this both out of general curiosity, but also because I started to wonder if this is something I should think about when I program myself.

+1  A: 

Stand-By keeps your "state" alive by keeping it in RAM. As a consequence if you lose power you'll lose your stored "state".

But it makes it quicker to achieve.

Hibernation stores your "state" in virtual RAM on the hard disk, so if you lose power you can still come back three days later. But it's slower.

I guess a limitation with Stand-By is how much RAM you've got, but I'm sure virtual RAM must be employed by Stand-By when it runs out of standard RAM. I'll look that up though and get back!

joshcomley
We the computer hibernates, it simply writes the content of the RAM on the disk. Next time you boot, it restores the content of the RAM from the disk. When the OS runs out of standard RAM, it writes stuff to disk, this is called paging :)
Philippe
And, AFAIK, paging happens when the pc is running normally as well (if you run out of RAM that is)
Svish
@Philippe - that's what I'm saying; hibernate takes the content of your RAM and plonks it on the HDD
joshcomley
+5  A: 

You should remember that the OS (scheduler) freezes your program about a gazillion times each second. This means that your program can already function pretty well when the operating system freezes it. There isn't much difference, from your point of view, between stand-by, hibernate and context switching.

What is different is that you'll be frozen for a long time. And this is the only thing you need to think about. In most cases, this shouldn't be a problem.

If you have a network connection you'll probably need to re-establish it, and similar issues. But this just means checking for errors in all IO operations, which I'm sure you're already doing... :-)

Gilad Naor
That is a very good point. Never thought about that!
Svish
Also note that while in prinicple standby/hibernate should restore *all* hardware to its old state (RAM, CPU, graphics etc.) this may not work for all hardware. So some hardware may not "wake up" properly. Normally this only happens with bad drivers, but it's something to look out for, especially with unusual hardware (e.g. special interface cards).
sleske
+1  A: 

The Wikipedia article on ACPI contains the details about the different power savings modes which are present in modern PCs.

Here's the basic idea, from how I understand things:

The basic idea is to keep the current state of the system persisted, so when the machine is brought back into operation, it can resume at the state it was before the machine was put into sleep/standby/hibernation, etc. Think of it as serialization for your PC.

In standby, the computer will keep feeding power to the RAM, as the main memory is volatile memory that needs constant refreshing to hold on to its state. This means that the hard drives, CPU, and other components can be turned off, as long as there is enough power to keep the DRAM refreshed to keep its contents from disappearing.

In hibernation, the main memory will also be turned off, so the contents must be copied to permanent storage, such as a hard drive, before the system power is turned off. Other than that, the basic premise of hiberation is no different from standby -- to store the current state of the machine to restore at a later time.

With that in mind, it's probably not too likely that going into standby or hibernate will cause problems with tasks that are executing at the moment. However, it may not be a good idea to allow network activity to stop in the middle of execution, as depending on the protocol, your network connection could timeout and be unable to resume upon returning the system to its running state.

Also, there may be some machines that just have flaky power-savings drivers which may cause it to go to standby and never come back, but that's completely a different issue.

coobird
+3  A: 

My initial thought is that as long as your program and its eco-system is contained within the pc that is going on stand - by or hibernation, then, upon resume your program should not be affected.

However, if you are say updating a record in some database hosted on a separate machine then hibernation / stand - by will be treated as a timeout.

If your program is dependent on such a change in "power status" you can listen to WM_POWERBROADCAST Message as mentioned on msdn

Prashast
Thanks for noting the WM_POWERBROADCAST message on msdn! Could be very helpful if an application needs to reestablish a network connection or something =)
Svish
A: 

There are some implications for your code. Hibernation is more than just a context switch from the scheduler. Network connections will be closed, network drives or removable media might be disconnected during the hibernation, ...

I dont think your application can be notified of hibernation (but I might be wrong). What you should do is handle error scenarios (loss of network connectivity for example) as gracefully as possible. And note that those error scenario can occur during normal operation as well, not only when going into hibernation ...

Guillaume