Code Analysis
Mypcb.h
Mymain.c
The above code mainly completes the initialization of the NO. 0 process, that is, the PID is set to 0, the state is set to 0 (that is, the runnable state), the process entrance and the current process of the thread's IP point to My_process, the thread's SP points to the process stack of the current process, because there are only No. 0 process, So the next pointer points to itself to form a single PCB linked list.
The above code is mainly to expand the loop list, using memcpy () to replicate the status of Process No. 0 to create from the 1th to Max_task_num-1 process, and with the No. 0 process to form a circular PCB link list.
The above code function is to start the No. 0 process from the task[0 of the loop PCB link list, which is achieved by using GCC inline assembly. Next we analyze the process in detail:
Initial stack state:
Movl%1,%%esp
Push1%1
PUSHL%0
Ret
POPL%%EBP
In fact, this assembly code will not be executed, because the RET \n\t after the EIP point to the beginning of process No. 0 address.
The above code to achieve 10 million times per cycle of the current process of printing the PID, and then determine whether the clock interrupt the dispatch flag (my_need_sched) is set to 1, if 1 will be the dispatch flag 0, call My_schedule (), avoid the message mechanism, Then print the PID of the current process again.
Myinterrupt.c
The My_time_handler () implementation is called per thousand and the Dispatch flag (my_need_sched) is not 1 o'clock, printing ">>>my_timer_handler here<<<" and placing the dispatch flag at 1, To facilitate my_schedule () to be called in my_process ().
The above code is the process scheduling function fault-tolerant processing and next and prev pointers are defined and initialized.
Assuming that process number NO. 0 is now running, and that process 1th is unrunnable state, which is next->state-1, it goes to the Create Process branch:
Set the next->state to 0, point to the next process, print the process switch relationship, and then execute the assembly, analyzing the inline assembly execution from a stack perspective:
Stack initial state
PUSHL%%EBP
MOVL%%esp,%0
MOVL%%2,%%esp
MOVL%%2,%%EBP
Movl $1f,%1
PUSHL%3
Ret
The above code function, if the current process is located on the PCB loop linked list of the next node state of the value of zero that is the next process state of runnable, switch to the next process, and then print the switch relationship. The specific context switching process is written using GCC inline sinks, which are analyzed by Image:
Register Initial state
......
Not finished, to be continued
Linux Kernel Analysis MOOC course to understand process time wheel scheduling from the Mini Linux kernel perspective (not finished)