Pan Cong Original works reproduced please specify the source "Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000
This experiment is mainly to study the active scheduling in thread scheduling.
---------------------------------------------
First, the basic structure
(1) Process: In Mypcb.h, processes are composed of IP (EIP) and SP (ESP).
(2) Process Control block (PCB): Process number PID, status (Error/Run/stop), process stack (including kernel stack and user stack, which is merged here, see [1]), process, task entry, a linked list through next pointers.
Second, initialize (__init my_start_kernel)
Generate 0th Process: Process number 0, state run, Task entry and IP address of my_process (here the function is used as a pointer, return function address), SP is set to the top of the stack, the list next point to itself.
Generate additional processes: The process number is assigned, the entry is the same as the 0th process, the state is not running, the SP is set to the top of the stack (each process maintains its own stack), and the linked list is linked to a ring list.
The current task is set to process number 0th.
ASM Code Snippet:
1 ASM Volatile2 (3 "movl%1,%%esp\n\t"//set task[pid].thread.sp to ESP4 "PUSHL%1\n\t"//PushEBP5 "PUSHL%0\n\t"//PushTask[pid].thread.ip6 "ret\n\t"//PopTask[pid].thread.ip to Eip7 "popl%%ebp\n\t"8 :9:"C"(TASK[PID].THREAD.IP),"D"(TASK[PID].THREAD.SP)Ten);
The stack-top pointer esp of process 0th is assigned to ESP in the system and the ESP stack (which can be understood as the EBP stack), and the IP stack of process 0th is pushed, and the command is executed from that IP using the RET instruction.
Theoretically, if these processes do not exit, the last line POPL will never be executed.
Third, master process method (My_process)
Each instruction cycle is cycled once, each 10000000 of which prints the process number of the currently running process two times, and if the process schedule is identified as 1, the dispatch function is executed during the two printing cycles.
Iv. Dispatch handle (My_timer_handler)
Run the function after each interrupt, set the dispatch identity to 1 after accumulating 1000 interrupts, so that the main process method runs the dispatch function.
Five, dispatch function (My_schedule)
Defines two pointers to the TPCB type, pointing to the current process and the next process, respectively. The running state of the next process is different (running or otherwise) into two pieces of assembly code for the corresponding process context switch.
1) The next process is executing:
1 ASM Volatile2 (3 "PUSHL%%ebp\n\t"//save EBP4 "movl%%esp,%0\n\t"//save ESP5 "movl%2,%%esp\n\t"//restore ESP6 "movl $1f,%1\n\t"Save EIP |1f mean label17 "PUSHL%3\n\t"8 "ret\n\t"//restore EIP9 "1:\t"//next process Start hereTen "popl%%ebp\n\t" One:"=m"(PREV->THREAD.SP),"=m"(PREV->THREAD.IP) A:"m"(NEXT->THREAD.SP),"m"(NEXT->THREAD.IP) -);
Will EBP stack, the current process SP record ESP, the IP record of the current process Label1 address, when the process switch returned to the stack of the contents of the original EBP back to EBP.
In addition to this, the current process is set to the next process, and the switch text is printed.
2) The next process is not executed:
Before executing the assembly command, set the next process state to run and set it as the current process.
The assembly code executed is similar to 1 and is not discussed here.
------------------------------------------------------------------------------
Finally, make use the command Qemu-kernel arch/x86/boot/bzimage, generate the diagram see:
--------------------------------------------
[1] Understanding the principle of schedule scheduling from the mini Linux kernel http://itdreamerchen.com/%E4%BB%8E%E8%BF%B7%E4%BD%A0%E5%9E%8Blinux%E5%86%85%E6%A0%B8%E7 %90%86%e8%a7%a3%e8%bf%9b%e7%a8%8b%e8%b0%83%e5%ba%a6%e7%9a%84%e5%8e%9f%e7%90%86/
"Linux kernel Analysis" The second lesson experiment homework