算法代碼分析
(一)算法分析
在計算機中進程執行時需要操作系統為其分配各種資源,比如內存空間,寄存器等等,但在計算機中不可能只有一個進程,因此操作系統需要為這些進程合理分配資源,使其在運行的時候不發生沖突。時間片輪轉就是一個這樣的算法,使其每個進程輪流使用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。
實驗過程與結果
總結
對於進程切換的關鍵點為保存自己的信息,然后載入下一個指令,這個具體體現在匯編代碼中,下一篇文章會分析一下調度算法里的匯編代碼,指出他們是如何保存信息的,並且切換進程的。