算法代码分析
(一)算法分析
在计算机中进程执行时需要操作系统为其分配各种资源,比如内存空间,寄存器等等,但在计算机中不可能只有一个进程,因此操作系统需要为这些进程合理分配资源,使其在运行的时候不发生冲突。时间片轮转就是一个这样的算法,使其每个进程轮流使用cpu资源,不发生冲突。
(二)代码分析
头文件代码(mypcb.h):
#define MAX_TASK_NUM 4 #define KERNEL_STACK_SIZE 1024*8 /* CPU-specific state of this task */ struct Thread { unsigned long ip; unsigned long sp; }; typedef struct PCB{ int pid; volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */ char stack[KERNEL_STACK_SIZE]; /* CPU-specific state of this task */ struct Thread thread; unsigned long task_entry; struct PCB *next; }tPCB; void my_schedule(void);
这段代码中首先是两个结构体,在Tread中ip为指令指针(程序执行的位置),sp为进程执行时的堆栈栈顶位置,PCB为进程的信息包括进程ID,进程状态,进程的堆栈空间大小,还有thread,进程入口,还有一个为指向下一个进程的指针,my_schedule为调度函数的声明。
#include <linux/types.h> #include <linux/string.h> #include <linux/ctype.h> #include <linux/tty.h> #include <linux/vmalloc.h> #include "mypcb.h" tPCB task[MAX_TASK_NUM]; tPCB * my_current_task = NULL; volatile int my_need_sched = 0; void my_process(void); void __init my_start_kernel(void) { int pid = 0; int i; /* Initialize process 0*/ task[pid].pid = pid; task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped */ 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]; /*fork more process */ for(i=1;i<MAX_TASK_NUM;i++) { memcpy(&task[i],&task[0],sizeof(tPCB)); task[i].pid = i; task[i].state = -1; task[i].thread.sp = (unsigned long)&task[i].stack[KERNEL_STACK_SIZE-1]; task[i].next = task[i-1].next; task[i-1].next = &task[i]; } /* start process 0 by task[0] */ pid = 0; my_current_task = &task[pid]; 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*/ ); } void my_process(void) { int i = 0; while(1) { i++; if(i%10000000 == 0) { printk(KERN_NOTICE "this is process %d -\n",my_current_task->pid); if(my_need_sched == 1) { my_need_sched = 0; my_schedule(); } printk(KERN_NOTICE "this is process %d +\n",my_current_task->pid); } } }
在这段代码中代码的执行入口为void __init my_start_kernel(void),我们先声明了三个外部变量,分别为进程数组,当前进程,和是否需要调度的标志位(1为需要调度,0为不需要),我们先设置0号进程的信息,注意task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process;此句的意思是将process函数的指针赋给ip和entry以便使系统去哪寻找进程入口,其他的赋值信息很简单,不再多说,赋完值后,把当前进程设成进程0,从零开始执行,后边的汇编代码的目的是调到process函数,启动进程。process函数为具体执行的内容i每次增大时10000000时打印进程id并且判断进程是否调度。
#include <linux/types.h> #include <linux/string.h> #include <linux/ctype.h> #include <linux/tty.h> #include <linux/vmalloc.h> #include "mypcb.h" extern tPCB task[MAX_TASK_NUM]; extern tPCB * my_current_task; extern volatile int my_need_sched; volatile int time_count = 0; /* * Called by timer interrupt. * it runs in the name of current running process, * so it use kernel stack of current running process */ void my_timer_handler(void) { #if 1 if(time_count%1000 == 0 && my_need_sched != 1) { printk(KERN_NOTICE ">>>my_timer_handler here<<<\n"); my_need_sched = 1; } time_count ++ ; #endif return; } void my_schedule(void) { tPCB * next; tPCB * prev; if(my_current_task == NULL || my_current_task->next == NULL) { return; } printk(KERN_NOTICE ">>>my_schedule<<<\n"); /* schedule */ next = my_current_task->next; prev = my_current_task; if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */ { my_current_task = next; printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid); /* 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) ); } else { next->state = 0; my_current_task = next; printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid); /* switch to new process */ 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; }
my_timer_handler函数是中断函数,每当它执行的时候说明进程已经用完自己的时间,该调度了。my_schedule为调度函数,next为下一个进程,prev为当前进程,调度时为两种情况,一个是要调度的为执行过的进程,另一个为没有执行的。第一种情况(if)是将自己的ebp赋给esp,然后将IP赋给ebp在执行进程,而第二种情况就有不同,是自己载入自己的基地址赋给esp。
实验过程与结果
总结
对于进程切换的关键点为保存自己的信息,然后载入下一个指令,这个具体体现在汇编代码中,下一篇文章会分析一下调度算法里的汇编代码,指出他们是如何保存信息的,并且切换进程的。