How is the computer able to save things before it crashes?

I was under the impression that when a computer has crashed, something went wrong and it can't perform anymore.

I was wondering how is the computer able to save things before it crashes?

Is the computer really capable of knowing when it's going to crash (and thus inform all processes: "Save ASAP because I'm about to crash") ?

If not, how is it possible that programs like Chrome and Microsoft Word can offer "restoring" services after the computer has crashed?


Solution 1:

When a computer "crashes", it may, eg, be due to an "abrupt" power failure (in which case nothing can be done), it may be due to some sort of nasty internal event (eg, corrupted page tables) that similarly prevents doing anything, or it may be due to some condition that simply prevents "further operations" somehow -- perhaps new processes cannot be started, perhaps the display cannot be updated, perhaps the system has simply run out of storage.

In these latter situations the OS can still function, within limits, and can at least complete disk operations that were ongoing, nicely close files, etc. In addition, if slightly more function is possible, the OS may be able to tell the various application processes to shut themselves down cleanly.

But even if the system goes down "hard", the system as a whole and individual applications may have maintained a system of "journals" and/or "checkpoints" to allow the state of the system as a whole and individual applications to be restored to an "atomic" point, where everything is "internally consistent", and where a minimum of important data has been lost.

This all isn't accomplished with a single mechanism, but with layers of functionality at the system and application level.

With regard to power failures specifically, there may or may not be advance notification, and the "advance" warning may be a fraction of a second or (with UPS or a failing laptop battery) several minutes. What can be done depends on the amount of warning.

In most cases, with a desktop sysyem without UPS, there is at best time to "quiesce" disk operations so that no disk is in the middle of a write when power finally disappears. This prevents generating bad sectors on disk. It used to be, when RAM was much smaller, that there could be enough time (with large capacitors in the power supply, or even, oddly using the energy stored in the disk drive rotor to generate electricity) to write RAM to disk before power dropped, but that possibility pretty much vanished when RAM grew larger than 100M or so.

[And note that, in the olden days, when memories were made with magnetic "cores", RAM was inherently preserved when power was lost (though a particular word being read/written when power failed might be corrupted). This made it easier for those old systems to recover from power failure.]

However, with something resembling a UPS (which may give anywhere from a few minutes to several hours of additional power) there are several more options. One is to simply shut the system down, as if you requested "power off". This causes each application to end itself, and then the system as a whole writes out permanent tables and shuts down. This can take a long time (as I'm sure many of you have noticed). But it's also possible to use a "hibernate" strategy, where RAM is written to disk as a single block, and then the system powered off. In the "hibernate" scenario, on restoring power, the RAM is read back in to exactly where it was written from, a few bits here and there are swizzled, and then execution resumes where it left off.

On some older "big iron" systems a semi-hibernate strategy was used to do an emergency shutdown: Memory would be written out as above, but when power was restored and memory read in then a standard shutdown would occur. This was done because some bits of system state (particularly in I/O controllers) could not be reliably saved/restored to permit continued operation.

Solution 2:

Chrome and Microsoft Word periodically save your current state as you're working. If the application or computer crashes, when the applications relaunch, they look for a saved state on disk, and restore it for you. They don't need to predict the crash; they are constantly saving your state in case something goes wrong.

Solution 3:

EDIT: This only works when an application crashes, not Windows.

Since Windows Vista there is a new API that allows Windows to call a special function in the application when it crashes so it can try to recover any data before it is shut down. It works like this:

  • App starts up
  • App calls Windows function: when I crash please execute RecoverData()
  • ...
  • App crashes
  • Windows executes RecoverData() in the App
  • RecoverData() tries to retrieve data about the currently opened document, in the mean time it keeps pinging Windows to tell it's still busy recovering.
  • When RecoverData() has finished/times out/stops pinging, Windows terminates the App and restarts it.

More info: http://msdn.microsoft.com/en-us/library/windows/desktop/aa373351(v=VS.85).aspx

Solution 4:

Well, they just save the current state before they crash. Like an auto save every X seconds.

I think one really has to differentiate:

  • Hardware crash: Only able to recover with previous checkpoints
  • OS Crash: no way for app to save data, OS can drop something like a core dump
  • Application crash: OS features for application crash can save information