[Linux] Process (iii)--idle process

Source: Internet
Author: User

9,linux Process Switching

Process switching:
Basic concepts:
Process context: When a process executes, the values in all registers of the CPU, the state of the process, and the contents of the stack are referred to as the context of the process. When the kernel needs to switch to another process, it needs to save all the state of the current process, that is, the context of the current process, and the process running in the process context can be preempted.
Hardware context: A set of data that must be loaded into a register before the process resumes execution is called the hardware contexts
The Linux kernel does not differentiate between processes and threads when the process is switched because the switch is for a task,
Timing of process switching: typically occurs in the kernel state when the system is called or interrupted.
Process Switching process:
Process switching unification occurs in the schedule () function, the core function is the Context_switch () function, code location: kernel/kernel/sched/ CORE.C file, based on arm to explain Linux process switching: Linux process switching process, schedule () function Code Analysis: Schedule () Code Analysis Schedule Code Analysis two

10,idle process

The concept of the idle process:
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. The idle process pid==o, that is, Init_task.


Creation of the Idle process:
The entrance to the Vmlinux is arch/arm/kernel/head. S, (focus on the location of entry (stext)) The entry address can be from kernel/arch/arm/kernel/ VMLINUX.LDS.S learned that the file, in the assembly file for the PID 0 of the original process set the execution environment, including first prohibit the MMU, the kernel is just beginning with the physical address, prohibit I-cache and D-cache, and then read RO,R1,R2, Then the original process started 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: The function to create the init process (pid==1) is as follows:
Kernel_thread (Kernel_init, NULL, CLONE_FS | Clone_sighand);
The PID is 1 init process, it will continue to complete the remaining initialization work, and then Execve (/sbin/init), become the ancestor of all other processes in the system, the idle process with PID 0 after the Init process has been created, pid=0 process calls Cpu_idle () evolved into the idle process. Current_thread_info ()->status |= Ts_polling;init A part of the initialization work before it evolves into/sbin/init, one of which is Smp_prepare_cpus (), Initializes the SMP processor, which in this process calls task = Copy_process (CLONE_VM, 0, Idle_regs (&regs), 0, NULL, NULL, 0) during each processing from the processor; Init_idle (task, CPU);

Run time of the idle process:
The idle process priority is Max_prio, which is the lowest priority. In earlier versions, Idle was involved in scheduling, so its priority was set to a minimum, and when no other process could run, the execution of Idle was dispatched, while the current version of Idle did not participate in scheduling in the running queue, but instead contained the idle pointer in the run queue structure, pointing to the idle process, Run when scheduler discovers running queue is empty, incoming run


What exactly did the idle process do:
The idle process eventually calls the Cpu_idle () function

[CPP]View Plaincopy
  1. Process.c
  2. void Cpu_idle (void)
  3. {
  4. ......
  5. / * Endless idle loop with no priority @ all */
  6. While (1) {
  7. ......
  8. While (!need_resched ()) {
  9. if (Cpu_is_offline (smp_processor_id ())) {
  10. Tick_set_cpu_plugoff_flag (1);
  11. Cpu_die (); / * Plugoff CPU * /
  12. }
  13. ......
  14. if (Cpuidle_idle_call ())
  15. Pm_idle (); / * Enter low power * /
  16. }
  17. ......
  18. Schedule_preempt_disabled (); /* Call Schedule () */
  19. }
  20. }

Add: Linux kernel start-up process
When the embedded device jumps from uboot to kernel, the first thing to run is the self-extracting program of kernel, and the Linux kernel is compiled in compressed file form, the path is Kernel/out/arch/arm/boot/zimage
Zimage code and data are stored, a: According to the Out/arch/arm/boot/compressed/vmlinux.lds file, if you want to modify the code snippet and data segment location also need to modify the link script.
The basic flow of kernel init is as follows:
. stext
Start_kernel ()
Rest_init ()
Kernel_init ()

[Linux] Process (iii)--idle process

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.