Idle process Analysis of Linux kernel __linux

Source: Internet
Author: User

1. What is idle

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

2. Creation of idle

We know that the system is from the BIOS power self-test, loading in the MBR of the Bootstrapper (Lilo/grub), and then load the Linux kernel began to run until the specified shell began to run, when the user began to operate Linux. and 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 begins to perform 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 invoke fork () to create the first user process:

Kernel_thread (Kernel_init, NULL, CLONE_FS | Clone_sighand); This process is known as the 1 init process for PID, which continues to perform the remaining initialization work and then Execve (/sbin/init) as the ancestor of all other processes in the system. About INIT We do not study this time, look back at the process of pid=0, after the creation of the INIT process, pid=0 process call Cpu_idle () evolved into idle process.

Current_thread_info ()->status |= ts_polling;

In an SMP system, the idle process was created on the main processor (the processor that performs the initialization), as well as the idle process from the processor (the processor activate by the host processor), and how they were created. Then look at the init process, which performs part of the initialization work before it evolves into/sbin/init, one of which is Smp_prepare_cpus (), which initializes the SMP processor, which is called during each processing from the processor

Task = copy_process (CLONE_VM, 0, Idle_regs (&regs), 0, NULL, NULL, 0);

Init_idle (task, CPU);

That is, a process is copied from Init and initialized to the idle process (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 a idle process (pid=0). The init process creates a idle process (pid=0) for each from the processor (run queue) and then evolves into a/sbin/init.

3. Idle's operating time

Idle process priority is Max_prio, which is the lowest priority. In earlier versions, Idle was a reference

And scheduling, the execution idle is scheduled only when no other process is available to run.    In the current version, Idle does not participate in scheduling in the run queue, but in the run queue structure with idle pointers, pointing to the idle process, running when the dispatcher discovers that the run queue is empty. 4. Idle's workload from the above analysis we can see that idle is scheduled when the system does not have other ready processes to execute. Either the primary processor or the processor, the last Cpu_idle () function is executed.  So let's take a look at what Cpu_idle did.  Because the idle process does not perform any meaningful tasks, it is usually considered 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 in 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 exit latency and use idle () to save energy. The default idle implementation is the HLT instruction, the HLT instruction causes the CPU to suspend, waits for the hardware to be interrupted when restores, thus achieves the energy saving goal. That is, from the processor C0 state to the C1 state (see ACPI Standard). This is also the primary tool for the various "processor cooling" tools on the Windows platform earlier this year. Of course idle can also be defined in other ACPI or APM modules, or even a custom idle (for example, NOP).

Summary:

1.idle is a process with a PID of 0.

2. The idle on the main processor evolved from the original process (pid=0). Idle from the processor are fork by the Init process, but their PID is 0.

The 3.Idle process is the lowest priority and does not participate in scheduling, but is only scheduled when the run queue is empty.

The

4.Idle loop waits for the need_resched position. Use HLT energy by default. Related article recommendation: Linux kernel--Network stack Implementation Analysis (i)--Network stack initialization write the simplest kernel: HelloWorld Linux kernel to get time and convert cost time zone time this article from: Love Linux technology Network this article link: http:// www.ahlinux.com/start/kernel/9180.html

Related Article

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.