The process of understanding process scheduling and process switching during the time-tracking analysis process

Source: Internet
Author: User
Tags prev

The process of understanding process scheduling and process switching during the time-tracking analysis process

20135224 Chen Shi + original works reproduced please specify the source + "Linux kernel analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000 "

The first part---scheduling time

How it happens:

1 Interrupt processing (clock interrupts,I/O interrupts, system calls, and exceptions) or return user state according to the need_resched tag call

2 kernel threads are directly called for process switching, can also be scheduled during interrupt processing, kernel threads can be actively scheduled, or can be passively dispatched

3 User-State process can not realize the active scheduling, only through the kernel state after a certain point in time to dispatch, in the interrupt processing process scheduling

Dispatch Mediation: Schedule () function

1 S The chedule() function starts, disables kernel preemption and initializes some local variables, at which point theschedule() checks the number of running processes remaining in the run queue and, if there is a callable process, begins the call.

2  Theinstructions immediately after the SWITCH_TO macro in the schedule () function are not executed by the next process at once, but later by Prev when the scheduler chooses prev and executes . execution. The prev local variable does not point to the original process that was replaced when it started to describe schedule (), but instead points to the original process that was replaced by Prev when Prev was dispatched .

3 immediately after the Context_switch () function call, macro barrier() produces a code optimization barrier. Executes the finish_task_switch() function.

The 4 schedule() function last performed: Regain the large kernel lock, reboot the kernel preemption, and check if other processes have set the TIF_RESCHED flag for the current process, if so, the entire schedule() function to start execution again

swtich_to Key Code :

"Movl $1f,%[prev_ip]\n\t" saves the EIP of the current process , restoresthe eip from Prev_ip when recovering

"Pushl%[next_ip]\n\t" pushes the starting IP position of the next process to the top of the next process stack stack (starting point)

"JMP __switch_to\n" jmp passes parameters via prev and next registers

"Popl%%ebp\n\t" recovery context,next has been push over ebp

Context_switch (struct RQ *rq,struct task_struct *prev context_switch () Context switch, establishes Next's address space.

struct task_struct *next)

{

struct mm_struct *mm, *oldmm;

Prepare_task_switch (RQ, Prev, next);

Mm= Next, MM; The MM field points to the memory descriptor owned by the process.

OLDMM = prev, active_mm; The active_mm field points to the memory descriptor used by the process.

Arch_start_context_switch (prev);

if (!mm) {

next->active_mm = OLDMM; If Next is a kernel thread, the thread uses the address space used by Prev;

Atomic_inc (&oldmm->mm_count); The schedule () function sets the thread to lazy TLB mode.

Enter_lazy_tlb (Oldmm,next);

}else

SWITCH_MM (Oldmm,mm,next); If Next is a normal process, the schedule () function replaces the Prev address space with Next

if (!prev->mm) {

prev->active_mm = NULL; If prev is a kernel thread or a process that is exiting, the Context_switch () function will

rq->prev_mm = OLDMM; The support that points to the Prev memory description is saved to the Prev_mm field in the run queue;

} and Reset prev->active_mm.

Spin_release (&RQ->LOCK,DEP_MAP,1,_THIS_IP_);

Context_tracking_task_switch (Prev,next); Context_switch () can call switch_to to perform a process switch between Prev and next

Finish_task_switch (This_rq (), prev);

}

Part II: GDB tracking

Set breakpoints--schedule,context_switch,switch_to corresponding breakpoints to view them separately

Single-step analysis of the switch function in each breakpoint to see the code

Summary: For understanding the process scheduling time tracking analysis process scheduling and process switching process, generally can be summed up as the following points:

1 Process execution state for user requirements

2 Intentional or system interruption

3 Save_all

4 Call Schedule ()

5 Execute to switch_to process context switch

6 Another process enters and makes

7 Restore_all

8 Iret-pop cs:eip/ss:esp/eflag from kernel stack

9 continue to run the user-state process

The process of understanding process scheduling and process switching during the time-tracking analysis 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.