This article is from: http://blog.csdn.net/sad_4978/article/details/3776887thank you!
For power management, this function is frequently used in desktop software. Especially when you burn a CD, if you set a power-saving mode, it is likely that the CD may fail to be burned. Therefore, it is necessary to have a necessary understanding of this part. Power management is not just a function of the operating system. The operating system must have a motherboard that supports power management.
ACPI
The power management method of the operating system cannot help but introduce ACPI. It is a prerequisite for the operating system to implement power management.
ACPI (Advanced Configuration Management) is a new type of power management specification proposed by Intel/Microsoft/toshba in 1997. It aims to allow the system, rather than the bios, to fully control power management and make the system more energy-efficient.
Its main features include:
L provides the immediate start function, that is, it can be immediately restored to the status of the last shutdown.
L peripherals (such as optical drive) will automatically turn off the power when not in use, and provide power when used.
L supports hot swapping.
Based on the above features, there are three ways to reduce power consumption:
L cut off the power of the monitor to keep the host powered on. After entering the suspend state, you can wake up your computer with a keyboard or mouse.
L The operating system stores the current information in the memory. Only the memory and other key components are powered on, and the computer is in a high power-saving state. After entering the suspend state, you can wake up your computer with a keyboard or mouse. After waking up, the operating system reads data from the memory and restores the data to the status before suspension. This method is also called to suspend to memory (save
To ram or suspend to ram ).
L The operating system stores the current data on the hard disk and shuts down automatically. After you enter the suspend state, you must use the power key to wake up a computer. After waking up, the operating system reads data from the hard disk and restores it to the status before suspension. This method is also called to suspend to the hard disk (save
To disk or suspend to disk ).
The following describes the basic content of ACPI. You can use ACPI to implement the following functions:
L you can set the switch time of the peripherals.
L The laptop can enter a low-power state at a low voltage to ensure that important applications run properly.
L The operating system can reduce the clock frequency when the application requires less time.
L The operating system can allocate energy to peripherals and boards based on their specific needs.
L enables the computer to sleep when no one is using the computer, but ensures that some devices are working.
L The plug-and-play device can be controlled by ACPI during insertion.
In actual application, ACPI is divided into six states. These six States accurately divide power management. They are:
S0 ----- all devices are powered normally.
S1 ----- disable the CPU through the CPU clock controller. Other parts are still working normally. This status is also called pos (Power
On suspend)
S2 ----- the CPU is stopped, and the bus clock is also disabled. The remaining devices are still running.
S3 ----- the operating system stores the current information in the memory. Only the memory and other key components are powered on.
S4 ----- the operating system stores the current information in the hard disk, and the system's main power is off. However, the hard disk is still charged and can be awakened.
S5 ----- shut down, all devices are powered off.
OS message
Pending operation
In the "initial understanding of ACPI", I had a basic understanding of power management. I have learned about ACPI in software development. Here, we will introduce how the Windows operating system manages the power supply and notify the application through messages.
If you carefully observe the Windows system, you will find that there are two options: standby and sleep. Sleep and Hibernate are supported in the English operating system. You may also see stand-. What if sleep, stand-by, and Hibernate are different? I checked msdn and found no better explanation. I accidentally saw an early article in msdn and explained the three names as follows:
For this discussion, the term "Sleep" means that the system is on standby or is in hibernation. To an application, standby and hibernation are the same. The difference occurs in how the operating
System determines what gets powered down. The application does not need to provide any additional feedback for the operating system to make this determination.
I understand that sleep should be collectively referred to as stand-by and hibernate, but why is sleep displayed on the operating system?
I don't know. Okay. Ignore them for the moment. To continue here, stand-by is called standby, and Hibernate is called sleep. In Windows
In the system, the standby mode corresponds to the S3 state, and the hibernation mode corresponds to the S4 state.
Many things have been clarified here. Next, let's take a look at the windows system if the power management event is notified to the application.
A message such as wm_powerbroadcast is found in msdn. Description of the message:
Notifies applications that a power-management event has occurred. A Window provided es this message through its windowproc function.
Lresult Callback windowproc (hwndHwnd, // Handle to window
UintUmsg,//Wm_powerbroadcast
Wparam Wparam, // Power-management event
Lparam); // function-specific data
The description of msdn clearly shows that the application can obtain power management messages by reloading the windowsproc function. When wm_powerbroadcast is received, it also brings an event and a set of data. By analyzing the event and data, you can find out the current status of the operating system. Note that this message must be obtained through the windowsproc function. The overloaded pretranslatemessage function is invalid for this message.
It is described based on Windows XP. When there is a difference between Vista and Windows XP, the difference is extracted for description.
When the operating system is ready to enter S3 and S4 states, the wm_powerbroadcast message is first broadcast, and the pbt_apmquerysuspend event is carried to the currently running application. If the application does not process this event, it agrees to suspend the request. The operating system then sends the pbt_apmsuspend time, and then the operating system immediately suspends.
If an application does not want the operating system to suspend, the operating system can be blocked when receiving the pbt_apmquerysuspend message by returning broadcast_query_deny ;. This operation can prevent the system from manually initiating the suspension (the user's settings in the Power Management Section), and also prevent the user from manually initiating the suspension operation.
If the application wants to process the event before the system enters the suspended state, it must also handle the event when it receives it. Although the operating system will send the pbt_apmsuspend event later, the stay time is short.
If the application does not prevent system suspension when receiving the pbt_apmquerysuspend event, false is returned when the pbt_apmsuspend event is executed. At this time, the operating system will be suspended, but it will be restored immediately. Therefore, to prevent the operating system from being suspended, you can only process the pbt_apmquerysuspend event.
If the application stops the operating system from being suspended, broadcast_query_deny is returned, and then the operating system sends the pbt_apmquerysuspendfailed event to the application, notifying that the suspension fails.
When the operating system recovers from the suspended status, the wm_powerbroadcast message is still used for broadcast and an event is carried. The order of events is pbt_apmresumeautomatic and pbt_apmresumesuspend. Since these two events have little impact on the application, we will not explain them too much here.
The Vista operating system and Windows XP have many differences. The operating system does not broadcast the pbt_apmquerysuspend event before it enters the pending state, and the application cannot prevent the user from manually initiating the pending request. From the broadcast pbt_apmsuspend event to the suspension of the operating system, only two seconds of processing time is reserved for the application. When the status is restored from S3, only the system application wake up with the keyboard or mouse will receive the apmresumeautomatic event. This requires the attention of developers.
Although the user cannot manually initiate a pending operation in the Vista operating system, it can block the pending operation initiated by the operating system (the user's settings in the power management mode ). Windows provides the setthreadexecutionstate function to complete this operation. This function is also applicable to Windows XP operating systems.
When a program needs to stop the operating system from being suspended, you can use the following code.
: Setthreadexecutionstate (es_system_required | es_display_required | es_continuous );
After the processing is complete, you must restore the power management function of the operating system by using the following code.
: Setthreadexecutionstate (es_continuous );
Shutdown
When the Windows system is about to shut down, wm_queryendsession is broadcast. Here, the application can handle the final cost of shutdown. If the response function returns false, the operating system can be stopped. There are two different descriptions in msdn. If a non-zero value is returned in the message, the wm_endsession message is sent. If the return value is zero, the wm_endsession message is sent. The wm_endsession message is sent in the Windows XP operating system no matter whether zero or non-zero is returned,
In Vista, if the system returns false in the response function of the wm_queryendsession message, a block pops up in the operating system, and the user selects yes or no. If you select Yes, the system will shut down.