开发者

What happens during the shutdown process of an OS?

开发者 https://www.devze.com 2022-12-21 23:40 出处:网络
I hope this is programming-related enough. What exactly happens during the shutdown process of an Operating-System, let\'s take Linux here as its open-source and there may be more knowledge around ab

I hope this is programming-related enough.

What exactly happens during the shutdown process of an Operating-System, let's take Linux here as its open-source and there may be more knowledge around about this.

  • (How) are the kernel threads terminated?
  • Does the power-supply stop supplying power (captain obvious) when the computer is in a "clear" state? What I mean with clear state is that there isn't anything going on in the CPU anymore, etc.

  • Why do most operating systems take so lon开发者_运维百科g to shutdown? I mean hey, they don't need to initialize something or even load resources into memory.

  • Why don't operating systems go like "Hey, nevermind - let's just terminate all the processes end just turn the power off"?


  1. Well, sometimes the OS does just pull the plug. Give 'reboot' the right control arguments, and splat.

  2. What if modifications to a file are in memory and not flushed to disk? You might want to get them out first.

  3. What if some daemon process is partway through some complex update of a file? You might want it to finish.

  4. What if a daemon needs to tell something else on the network that it is going away to arrange an orderly departure?

To deal with all of these, operating systems have orderly shutdown procedures. On linux, you can watch the sausages being unmade by looking in /etc/rcN.d, for N=0,1,2,3,4.

The symbolic links that begin with 'K' are the shutdown process.

One way or the other, at the end of the process, on modern hardware, the operating system turns off the power. What that means depends, of course, on the hardware.


The latest Mac OS X, Show Leopard, actually does something rather interesting: applications are supposed to keep themselves in a 'stable' state where they can be terminated without warning. If that's not the case (during writes to disk), they have to let the OS know. Then, when you ask it to shut down, and all apps are in the 'stable' state, the OS really just pulls the plug (after the obvious hardware-related shutdown is done, such as writing all dirty pages to disk, making sure the hard drive is stopped and parked, etc).

Another reason why most OS sometimes take a long time to shut down, is this: you probably have a lot of apps that weren't used for some time, and are therefore swapped out. To cleanly shut them down, the OS sends them a Terminate message. But in order for them to respond to this and exit safely, they have to be swapped in again. And this happens at the same time that the disk is busy from other processes writing their data to it...


  • Does the power-supply stop supplying power (captain obvious) when the computer is in a "clear" state? What I mean with clear state is that there isn't anything going on in the CPU anymore, etc.

Once all processes have stopped and the file systems are unloaded, the kernel sends an ACPI signal to the BIOS that turns the power supply off.

  • Why do most operating systems take so long to shutdown? I mean hey, they don't need to initialize something or even load resources into memory.

When shutting down, many applications have to store its current state on the file system for future executions, and the file system has to flush the changes from RAM to the hard drive.

  • Why don't operating systems go like "Hey, nevermind - let's just terminate all the processes end just turn the power off"?

Because you run the risk of having applications not storing their data correctly, and even corrupting their data. Even if that wasn't a problem, there could be data incorrectly flushed from the file systems' cache to the hard drive, leaving your system with an inconsistent file system.

You can read about the recent EXT 4/KDE 4 debacle, where EXT 4 was flushing the files' metadata before the files' content to disk and KDE 4 was reading writing tons of small files in bursts. If you happened to hard shutdown during write, all your config files would end up corrupt/FUBAR.


I believe the OS calls ACPI or similar power management functions to do the final turn off command. If the computer doesn't support that, Linux will just halt execution, with all networks disabled.

The reason for not doing a sudden power off is that:

  1. Some hardware wants to be shutdown cleanly - e.g. parking hard disc.
  2. The user might have unsaved data - better to ask all apps to close down.
0

精彩评论

暂无评论...
验证码 换一张
取 消