Kernel learning process preemption and switching

Source: Internet
Author: User
Tags prev thread

Preemption is accompanied by the execution of schedule (). The kernel provides a tif_need_resched flag to indicate whether to dispatch the schedule () once.

According to the timing of preemption is divided into user preemption and kernel preemption. User preemption occurs when the kernel is about to return to user space. Kernel preemption occurs when the kernel space is returned.

1, user preemption: The kernel is about to return to user space to check whether the process has set the TIF_NEED_RESCHED flag, if set, will occur user preemption. The opportunity for a user preemption to occur when a user's space is returned from a system call or an interrupt handler.

2, Kernel preemption: In the kernel that does not support kernel preemption, the kernel process will continue to run if it does not voluntarily stop. Unable to respond to the real-time process. The preemption kernel, while sacrificing the cost of context switching, obtains greater throughput and response time. Kernel preemption is added to the 2.6 core, and a spin lock is added for some places to not be preempted. Added Preempt_count in the thread_info structure of the process the value is 0, and when the process uses a spin lock, add 1, minus 1 when releasing a spin lock. 0 indicates that the kernel can preempt.

The timing of the kernel preemption: 1, when the kernel space is returned from the interrupt handler, the kernel checks the Preempt_count and tif_need_resched flags, if the process has the TIF_NEED_RESCHED flag set and Preempt_count is 0, Kernel preemption occurs. 2, when the kernel is again used for preemption, when the process of all spin locks are released, the release program will check the TIF_NEED_RESCHED flag, if set will call schedule. 3, Show call Schedule 4, the kernel of the process is blocked.

Transitions for processes:

When transitioning from one running process to another, it is called process switch or context switch.

1. Hardware contexts (Hardware context)

2, Task status section TSS

3. Hardware context Switching: the hardware contexts in the Linux kernel are not stored in TSS as originally designed by Intel, but rather with a specific data structure.

4, Context_switch

* *context_switch-switch to the new MM and the new *thread ' s register state. */static inline void Context_switch (struct RQ *rq, structtask_struct *prev, struct task_struct, *next  
     ) {structmm_struct *mm, *oldmm;  
     Prepare_task_switch (Rq,prev, next);  
     Trace_sched_switch (Prev,next);  
     /* Get the scheduled process memory pointer/mm =next->mm;  
     /* Save the memory data structure used by the process you are currently replacing/* oldmm= prev->active_mm; * * For Paravirt, the This is coupled with a exitin switch_to to * See more highlights in this column: Http://www.bianceng.cnhttp://www.bianc 
      eng.cn/os/unix/* Combine the page table reload and the switchbackend into * one hypercall.  
     * * Arch_start_context_switch (prev);  
         /* If the incoming process memory pointer to the call is NULL, the kernel thread */if (!mm) {next->active_mm= oldmm;  
         /* Memory description structure using Count plus 1*/atomic_inc (&oldmm->mm_count);  
     Enter_lazy_tlb (Oldmm,next); }else/* is not a kernel thread, switch page table * * * SWITCH_MM (OLDMM,MM, next);  
         if (!prev->mm) {prev->active_mm= NULL;  
     Rq->prev_mm= OLDMM; } * * Since the Runqueue lock'll be released bythe next * Task (which are an invalid L Ocking OP but inthe case * of the scheduler it ' s a obviousspecial-case), so we * does an early LOCKDEP  
E here: * * #ifndef __ARCH_WANT_UNLOCKED_CTXSW spin_release (&rq->lock.dep_map,1, _THIS_IP_); #endif/*here We just switch the register state and the stack.  
     */////////////Switch_to (Prev,next, prev)  
      
     Barrier (); * * THIS_RQ must be evaluated again because Prevmay have moved * CPUs since it called schedule (), thus the ' 
      RQ ' on its stack * frame'll be invalid.  
      
* * Finish_task_switch (THIS_RQ (), prev); }

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.