Idle process analysis for Linux kernel

Source: Internet
Author: User

1. What is idle?

Simply put, idle is a process with a PID number of 0. Its predecessor was the first process created by the system and the only one that was not produced by fork (). In SMP systems, each processor unit has a separate running queue, and there is an idle process on each running queue, that is, how many processor units there are, and how many are idle processes. The idle time of the system, in fact, refers to the "Running time" of the idle process. Since idle is the process, let's take a look at how idle was created, and what exactly did it do?

2. Creation of idle
We know that the system is from the BIOS power-on self-test, loaded into the MBR of the Boot program (LILO/GRUB), and then load the Linux kernel began to run, until the specified shell began to run, the user began to operate Linux. And it's roughly the entrance to the Vmlinux.
Startup_32 (head. S) The execution environment is set for the original process with PID number 0, and then the process starts executing start_kernel () to complete the initialization of the Linux kernel. This includes initializing the page table, initializing the interrupt vector table, initializing the system time, and so on. Then call Fork () to create the first user process:
Kernel_thread (Kernel_init, NULL, CLONE_FS | Clone_sighand); This process is the famous init process with PID 1, which will continue to do the rest of the initialization work and then Execve (/sbin/init) to become the ancestor of all the other processes in the system. About INIT We do not study this time, looking back at the process of pid=0, after the creation of the INIT process, pid=0 process calls Cpu_idle () evolved into the idle process.

Current_thread_info ()->status |= ts_polling;
In the SMP system, in addition to the creation of the idle process on the main processor (the processor performing the initialization work) just above, and the idle process from the processor (the processor activate by the main processor), how did they create it? Following the init process, Init will perform part of the initialization before it evolves into/sbin/init, one of which is Smp_prepare_cpus (), which initializes the SMP processor, which is called when each slave processor is processed.
Task = copy_process (CLONE_VM, 0, Idle_regs (&regs), 0, NULL, NULL, 0);
Init_idle (task, CPU);
That is to copy a process from Init and initialize it to the idle process (the PID is still 0). The idle process from the processor performs some activate work, and then executes Cpu_idle ().
The whole process is simply that the original process (pid=0) creates the init process (pid=1) and then evolves into the idle process (pid=0). The init process creates an idle process (pid=0) for each slave processor (running queue) and then evolves into/sbin/init.

3. Idle run time
The idle process priority is Max_prio, which is the lowest priority. In the previous version, Idle was the reference

is scheduled, so its priority is set to the lowest, and execution idle is dispatched when no other process can run. In the current version, Idle does not participate in scheduling in the running queue, but rather in the run queue structure with the idle pointer, pointing to the idle process, running when the scheduler discovers that the run queue is empty, and running in.    4. Idle workload   from the above analysis we can see that idle is dispatched when the system does not have other ready processes to execute. The Cpu_idle () function, whether it is the main processor or the processor, is finally executed. So let's see what Cpu_idle has done.   Because the idle process does not perform any meaningful tasks, it usually takes two points: 1. Energy saving, 2. Low exit delay.   Its core code is as follows:  void cpu_idle (void) {  int cpu = SMP_PROCESSOR_ID ();   Current_thread_info ()->status | = ts_polling;  /* Endless idle loop with no priority at all */ while (1) {   tick_nohz_stop_sched_tick (1);   while (!need_resched ()) { 

Check_pgt_cache (); RMB ();
if (rcu_pending (CPU)) Rcu_check_callbacks (CPU, 0); if (Cpu_is_offline (CPU)) Play_dead ();
Local_irq_disable ();
__get_cpu_var (irq_stat). Idle_timestamp = jiffies;    /* Don ' t trace IRQs off for idle */stop_critical_timings (); Pm_idle ();
Start_critical_timings (); }
Tick_nohz_restart_sched_tick ();   Preempt_enable_no_resched ();   Schedule ();  Preempt_disable (); } }

Cycle judgment need_resched to reduce the exit delay, using idle () to save energy. The default idle implementation is the HLT instruction, the HLT instruction keeps the CPU in a paused state and waits for the hardware interrupt to recover, thus achieving the purpose of saving energy. That is, from the processor C0 state to the C1 state (see ACPI Standard). This is also the primary means of the various "processor cooling" tools on the Windows platform in the early years. Of course, idle can also be defined in other ACPI or APM modules, or even a custom idle (such as NOP).

Summary:
1.idle is a process with a PID of 0.
2. Idle on the main processor evolved from the original process (pid=0). Idle from the processor is obtained by the Init process fork, but their PID is 0.
The 3.Idle process is the lowest priority and does not participate in scheduling, but is dispatched only when the running queue is empty.
The 4.Idle loop waits for the need_resched to be placed. Use HLT to save energy by default.

Idle process analysis for Linux kernel

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.