杨明辉 + 原创作品转载请注明出处 + 《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000
一、实验过程:
1. 按照如下步骤Linux操作系统中配置实验环境:
1. sudo apt-get install qemu # install QEMU
2. sudo ln -s /usr/bin/qemu-system-i386 /usr/bin/qemu
3. wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.9.4.tar.xz # download Linux Kernel 3.9.4 source code
4. wget https://raw.github.com/mengning/mykernel/master/mykernel_for_linux3.9.4sc.patch # downloadmykernel_for_linux3.9.4sc.patch
5. xz -d linux-3.9.4.tar.xz
6. tar -xvf linux-3.9.4.tar
7. cd linux-3.9.4
8. patch -p1 < ../mykernel_for_linux3.9.4sc.patch
9. make allnoconfig
struct Thread { unsigned long ip;//指向CPU的运行地址 unsigned long sp;//指向堆栈的栈顶地址 //todo add other attrubte of system thread }; //PCB Struct typedef struct PCB{ int pid; // pcb id volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */ char stack[KERNEL_STACK_SIZE];// each pcb stack size is 1024*8 /* CPU-specific state of this task */ struct Thread thread; unsigned long task_entry;//the task execute entry memory address struct PCB *next;//本次进程执行完毕后下次需要执行的进程 unsigned long priority;// 进程的优先级 //todo add other attrubte of process control block }tPCB;2. 系统启动前的初始化过程
//首先初始化0号进程,0号进程就是操作系统启动的第一个进程,0号进程地址为系统确定的一个地址 //每次系统从这个地址开始运行 int pid = 0; task[pid].pid = pid;//给予进程进程号 task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped */ // 设置0号进程的执行的起始地址,本次模拟系统即为从my_process()函数开始执行 task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process; //设置进程的栈顶地址 task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1]; //设置本次进程切换后下个执行的进程 task[pid].next = &task[pid]; /*按上面步骤依次初始化所有进程 */ for(pid=1;pid<MAX_TASK_NUM;pid++) { memcpy(&task[pid],&task[0],sizeof(tPCB)); task[pid].pid = pid; task[pid].state = -1; task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1]; task[pid].priority=get_rand(PRIORITY_MAX);//随机给予线程一个优先级 } //设置最后一个进程的下一个进程为0号进程,形成一个环路调用 task[MAX_TASK_NUM-1].next=&task[0]; printk(KERN_NOTICE "\n\n\n\n\n\n system begin :>>>process 0 running!!!<<<\n\n"); /* start process 0 by task[0] */ pid = 0; my_current_task = &task[pid];//设置当前执行的进程为0号进程
1.源码asm volatile( "movl %1,%%esp\n\t" /* set task[pid].thread.sp to esp */ "pushl %1\n\t" /* push ebp */ "pushl %0\n\t" /* push task[pid].thread.ip */ "ret\n\t" /* pop task[pid].thread.ip to eip */ "popl %%ebp\n\t" : : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp) /* input c or d mean %ecx/%edx*/ ); }2.启动过程分析系统总是从0号进程开始启动,首先将当前进程的栈顶指针(task[pid].thread.sp)附值给%esp,然后压入堆栈保存下来,然后将所要执行的进程的首地址(tash[pid].thread.ip)压入堆栈,最后利用ret指令进入到所要执行的进程开始执行,这样就达到了进程的启动。
4. 时间片轮转以及进程行代码分析
时间片轮转代码及分析:
void my_timer_handler(void) { #if 1 // 程序执行2000次,设置my_need_sched为1,使正在执行的进程交出CPU运行下一个进程。 if(time_count%2000 == 0 && my_need_sched != 1) { my_need_sched = 1; //time_count=0; } time_count ++ ; #endif return; }进程执行代码及分析:
void my_process(void) { int i = 0; while(1) { i++; if(i%10000000 == 0)//每执行10000000次输出一次结果 { printk("this is pcd %d",my_current_task->pid); if(my_need_sched == 1)//检测CPU时间是否用完,当时间用完时重新给进程设置优先级,并调用my_schedule()进行进程切换。 { my_need_sched = 0; sand_priority(); my_schedule(); } } } }//end of my_process
5. 进程切换代码及分析
代码
void my_schedule(void) { tPCB * next; tPCB * prev; // if there no task running or only a task ,it shouldn't need schedule if(my_current_task == NULL || my_current_task->next == NULL) { printk(KERN_NOTICE " time out!!!,but no more than 2 task,need not schedule\n"); return; } next = get_next(); prev = my_current_task; if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */ {//save current scene /* switch to next process */ asm volatile( "pushl %%ebp\n\t" /* save ebp */ "movl %%esp,%0\n\t" /* save esp */ "movl %2,%%esp\n\t" /* restore esp */ "movl $1f,%1\n\t" /* save eip */ "pushl %3\n\t" "ret\n\t" /* restore eip */ "1:\t" /* next process start here */ "popl %%ebp\n\t" : "=m" (prev->thread.sp),"=m" (prev->thread.ip) : "m" (next->thread.sp),"m" (next->thread.ip) ); my_current_task = next;//switch to the next task printk(KERN_NOTICE " switch from %d process to %d process\n >>>process %d running!!!<<<\n\n",prev->pid,next->pid,next->pid); } else { next->state = 0; my_current_task = next; printk(KERN_NOTICE " switch from %d process to %d process\n >>>process %d running!!!<<<\n\n\n",prev->pid,next->pid,next->pid); asm volatile( "pushl %%ebp\n\t" /* save ebp */ "movl %%esp,%0\n\t" /* save esp */ "movl %2,%%esp\n\t" /* restore esp */ "movl %2,%%ebp\n\t" /* restore ebp */ "movl $1f,%1\n\t" /* save eip */ "pushl %3\n\t" "ret\n\t" /* restore eip */ : "=m" (prev->thread.sp),"=m" (prev->thread.ip) : "m" (next->thread.sp),"m" (next->thread.ip) ); } return; }//end of my_schedule过程分析
1.进程切换时首先使用get_next()函数获取进程优先级最高的进程作为将要调度的进程。
2.利用pushl %%ebp指令保护现场,然后利用pushl %3指令将下一个进程的代码首地址(next->thread.ip)压入堆栈,然后利用ret指令将该地址附值给eip寄存器,使程序跳转到下一个进程的代码首地址开始执行,从而使CPU切换到下一个进程开始执行。
三、总结
1. 当变量太多,寄存器不够使用时,计算机利用堆栈来保证变量,同样函数调用时,系统也利用堆栈来传递参数
2. 系统使用eip寄存器来定位程序的执行地址,eip寄存器的值不能直接附值,只能通过指令来改变。
3. 系统给每个进程分配一定的时间片来执行,时间片用完后就要交出CPU运行下一个进程,即利用中断来实现进程的并发。
4. 当进程切换时,系统会保存当前的工作状态,以便于下次恢复进程,使进程继续执行。