在Linux中,休眠主要分三个主要的步骤:
冻结用户态进程和内核态任务
调用注册的设备的suspend的回调函数
顺序是按照注册顺序
休眠核心设备和使CPU进入休眠态冻结进程是内核把进程列表中所有的进程的状态都设置为停止,并且保存下所有进程的上下文.
当这些进程被解冻的时候,他们是不知道自己被冻结过的,只是简单的继续执行.如何让Linux进入休眠呢?用户可以通过读写sys文件/sys
/power/state 是实现控制系统进入休眠. 比如
# echo standby > /sys/power/state命令系统进入休眠. 也可以使用
# cat /sys/power/state来得到内核支持哪几种休眠方式.
Linux Suspend 的流程
相关的文件:
你可以通过访问 Linux内核网站 来得到源代码,下面是文件的路径:
linux_soruce/kernel/power/main.c
linux_source/kernel/arch/xxx/mach-xxx/pm.c
linux_source/driver/base/power/main.c
接下来让我们详细的看一下Linux是怎么休眠/唤醒的. Let 's going to see how these
happens.
用户对于/sys/power/state 的读写会调用到 main.c中的state_store(), 用户可以写入 const
char * const pm_state[] 中定义的字符串, 比如"mem", "standby".
然后state_store()会调用enter_state(), 它首先会检查一些状态参数,然后同步文件系统.
下面是代码:
**
* enter_state - Do common work of entering low-power state.
* @state: pm_state structure for state we're entering.
*
* Make sure we're the only ones trying to enter a sleep state.
Fail
* if someone has beat us to it, since we don't want anything weird
to
* happen when we wake up.
* Then, do the setup for suspend, enter the state, an