Linux sleep and wake-up in Linux, sleep mainly consists of three main steps: (1) Freezing user-state processes and kernel-State tasks; (2) call the suspend callback function of the registered device. (3) sleep the core device and enable the CPU to sleep in the registration order. Freezing a process is a kernel that sleep and wakes up a process in linux.
In Linux, sleep consists of three main steps: (1) Freezing user-state processes and kernel-State tasks; (2) calling the suspend callback function of the registered device; (3) sleep The core device and enable the CPU to sleep in the registration order. To freeze a process, the kernel sets the state of all processes in the process list to stop and saves the context of all processes. When these processes are restored, they do not know that they have been frozen, but simply continue to execute. How can we break Linux into sleep? You can read and write the sys file/sys/power/state to enable the control system to sleep. For example, the # echo standby>/sys/power/state command system goes to sleep. You can also use # cat/sys/power/state to obtain the kernel-supported sleep modes. Linux Suspend process. Related file path: linux_soruce/kernel/power/main. clinux_source/kernel/arch/xxx/mach-xxx/pm. c linux_source/driver/base/power/main. c (1) Next let's take a detailed look at how Linux sleep/wakes up. The user's read/write operations on/sys/power/state call main. state_store () in c. You can write strings defined in const char * const pm_state [], such as "mem" and "standby ". Then state_store () will call enter_state (). It will first check some status parameters and then synchronize the file system. (2) prepare to freeze the process. After entering suspend_prepare (), it will allocate a virtual terminal for suspend to output information, broadcast a system to enter suspend's ipvy, and disable the user-mode helper process, then call suspend_freeze_processes () to freeze all processes. the current state of all processes will be saved here. some processes may refuse to enter the frozen state. when such a process exists, this function will stop freezing processes and restore all the frozen processes. (3) sleep the peripherals. Now, all processes (including workqueue/kthread) have stopped, and kernel-State tasks may have semaphores when they are stopped, so if you unlock the Semaphores in the peripherals at this time, there may be deadlocks, so you should be very careful when making the lock/unlock lock in the suspend () function of the peripherals, we recommend that you do not wait for the lock in suspend () during design. At last, we will call suspend_devices_and_enter () to sleep all peripherals. in this function, if the platform registers suspend_pos (usually defined and registered in the board-level definition ), here we will call suspend_ops-> begin (), and then driver/base/power/main. in c, device_suspend ()-> dpm_suspend () will be called, and they will call the suspend () callback of the driver in sequence to sleep all devices. When all devices sleep, suspend_ops-> prepare () will be called. this function usually makes some preparations to let the machine sleep. In Linux, non-startup CPUs in multi-core CPUs will be turned off. you can see through comments that the other CPUs will not cause race condion, and then only one CPU will be running. Suspend_ops is an on-board power management operation, which is usually registered in the file arch/xxx/mach-xxx/pm. c. Next, suspend_enter () will be called. This function will disable arch irq and call device_power_down (). it will call the suspend_late () function, this function is the function that the system actually calls after sleep. it usually performs the final check in this function. If the check is correct, sleep all the system devices and buses and call suspend_pos-> enter () to enable CPU power-saving. At this time, the system has sleep, and the code execution stops here. (4) Resume. If the system is interrupted or other events are awakened during sleep, the subsequent code starts to run. The wake-up sequence is the opposite to that of sleep, therefore, the system device and bus will first wake up, enable the system to be disconnected, enable non-startup CPU to be stopped during sleep, and call suspend_ops-> finish (), and in suspend_devices_and_enter () the function will also wake up each device to enable virtual terminals. Finally, call suspend_ops-> end (). Return to the enter_state () function. when suspend_devices_and_enter () returns, the peripherals are awakened, but the processes and tasks are frozen. suspend_finish () is called here () to unfreeze these processes and tasks, and issue ipvy to show that the system has exited from the suspend status, wake up the terminal. By now, all sleep and wakeup are completed, and the system continues to run.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.