Process switching and general implementation of the system one, the content summary and analysis process scheduling and process scheduling process scheduling requirements of the classification:
The first classification method:
I/O-bound (frequent I/O, often spending a lot of time waiting for I/O operations )
Cpu-bound (computationally intensive, takes a lot of CPU time to perform operations)
The second classification method:
Batch process (background, typical: compiler, scientific calculation)
Real-time process (real time demand response time short, stable, typical: video, audio, mechanical control)
Interactive processes (more interaction with users, faster response times, typical: Shell, text-editing programs, graphics applications)
Linux scheduling based on ticks and priorities (queued according to priority, dynamic)
Process Scheduling Policy : (a set of rules that decide when and how to choose a new process to run )
The kernel's scheduling algorithm related code uses a policy pattern similar to that in Ood, The scheduling algorithm is decoupled from the other parts.
timing of process scheduling:
1. During interrupt processing, call schedule () directly, or call schedule () from the need_resched tag when returning to the user state
* User-State process can only be dispatched passively
2. Kernel thread (only kernel state, no user state) can directly call schedule () for process switching, can also be scheduled in the interrupt processing process, that is, the kernel thread as a special kind of process can be actively invoked, can also be passively dispatched
3. The user-state process can not be active scheduling, only through the kernel state after a certain point in time process scheduling. scheduling during interrupt processing.
Process Context Switch
Process switching (the kernel must have the ability to suspend a process that is executing on the CPU)
Suspending a process that is executing on the CPU is not the same as saving the scene at the time of the outage, before and after the interrupt is in the same process context, only by the user state to the kernel state execution
The process context contains all the information that is required to execute:
User address space: Includes program code, data, user stack
Control information: Process descriptor, kernel stack
Hardware context
The schedule () function selects a new process to run and invokes Content_switch for context switching, which calls the Switch_to function
Next = Pick_next_task (Rq,prev);
Switch_to takes advantage of the prev and next two parameters: Prev points to the current process, and next points to the scheduled process
Code Analysis!
* Context-switching clobbers All registers, so we clobber * them explicitly, via unused output variables. * (EAX and EBP are not listed because EBP are saved/restored PNS * explicitly for Wchan access and EAX return value of __switch_to * * * * * ()) * */unsigned lon G ebx, ECX, edx, ESI, EDI; ASM volatile ("pushfl\n\t"/* Save Flags */"PUSHL%%EBP \n\t "/* Save EBP */MOVL%%esp,%[prev_sp]\n\t"/* Save ESP */\esp saved on current stack top 45 "Movl%[next_sp],%%esp\n\t"/* * restore ESP/\ completes the current process kernel stack switchover "MOVL $1f,%[prev_ip]\n\t"/* Save EI P/\ Save current process Eip "PUSHL%[next_ip]\n\t"/* restore EIP */\ Put the position of the next process starting point on the stack __switch_can ary "JMP __switch_to\n"/* regparm call */\ Use Register to pass parameter 50 "1:\t "Wuyi" POPL%%ebp\n\t "/* Restore EBP */" popfl\n "/ * Restore Flags */The parameters */output */55 : [prev_sp] "=m" (PREV->THREAD.SP), \SP is the stack top of the kernel stack, THREAD.SP is the EIP for the current process [PREV_IP] "=m" (prev->th READ.IP), \ Pushes the stack base address of the current process to "=a" (last), 58 59 /* Clobbered OUTPUT registers: */"=b" (EBX), "=c" (ecx), "=d" (edx), 61 "=s" (ESI), "=d" (EDI) __switch_canary_oparam */* Input parameters:/66: [NEXT_SP] "M" (NEXT->THREAD.SP), \next_sp the top of the next kernel stack, next->thread.sp the beginning of the next process [Next_ip] "M" (NEXT-&G T;THREAD.IP), * * Regparm parameters for __switch_to (): */70 [Prev] "A" (prev), [next] "D" (next) 72 73 __switch_canary_iparam:/* Reloaded segmen T registers * * "memory");General execution of Linux systems
The most common scenario: The running user-state process x switches to the process of running user-state process y
Running user-state process X
An interrupt occurred--save cs:eip/esp/eflags (current) to kernel Stack,then load Cs:eip (entry of a specific ISR) and SS:ESP (point to Kernel Stack).
Save_all//Save site
Schedule () is called during interrupt processing or before an interrupt is returned, where SWITCH_TO does a critical process context switch
The user-state process y is started after the label 1 (where Y has been switched out through the above steps so it can continue from label 1)
Restore_all//Recovery site
Iret-pop cs:eip/ss:esp/eflags from kernel stack
Continue to run user-state process y
Several special cases during Linux execution
By interrupting the timing of the processing process, the user-state process and kernel threads switch between each other and the kernel threads switch to each other, very similar to the most common situation, but the kernel thread is running in the process of interruption without process user state and kernel state conversion;
Kernel thread actively calls schedule (), only the process context of the switch, there is no interrupt context switch, and the most general situation is slightly abbreviated;
The system call that creates the child process starts at the execution point in the subprocess and returns the user state, such as fork; next_ip=ret_from_fork;
A situation in which a new executable program is loaded and returned to the user state, such as EXECVE;
Any computer system contains a basic set of programs that become the operating system.
Kernel (process management, process scheduling, interprocess communication mechanism, memory management, interrupt exception handling, file system, I/O system network section)
Other programs (shell programs, System programs)
The purpose of the operating system
- Interact with hardware, manage all hardware resources
- Provides a good execution environment for user programs (applications)
The simplest and most complex operation
In the console input LS command->shell program parses the input parameters, determines that this is the LS command-call system call Fork to generate a copy of the shell itself, call the exec system to call the LS executable file
Second, the experimental process (GDB tracking debugging process scheduling)
Experimental Analysis:
Schedule () selects a new process to run and invokes Context_switch for context switching, Context_switch calls SWITCH_TO macros for critical context switching.
next=pick_next_task(rq,prev);//进程调度算法被封装在内
context_switch(rq,prev,next);//进程上下文的切换
jmp 函数名//使用寄存器来传递参数,而不是压栈
Switch_to takes advantage of the prev and next two parameters: Prev points to the current process, and next points to the scheduled process. 、
Linux kernel Seventh-week process switchover and system general execution 20135311 Fu Dong