一個簡單的時間片輪轉多道程序內核操作系統工作流程
阿新 • • 發佈:2017-05-22
gson star 高級 time author family num 個數 count
借用還有一篇博文,以新任務切換為例進行堆棧變化分析:
一.操作系統工作概述
-
存儲程序計算機工作模型,計算機系統最最基礎性的邏輯結構;
-
函數調用堆棧,高級語言得以執行的基礎;
-
中斷。多道程序操作系統的基點。
二.代碼分析
在上一篇博文《搭建OS kernel環境方法》的基礎上進行時間片輪轉多道程序的小os.
主要對mypcb.h, mymain.c 和myinterrupt.c這三個文件進行分析。
<pre name="code" class="cpp"><span style="font-size:12px;">//mypcb.h </span>
<span style="font-size:12px;">#define MAX_TASK_NUM 4 #define KERNEL_STACK_SIZE 1024*8 /* CPU-specific state of this task */ struct Thread {//給任務定義一個eip和esp unsigned longip; unsigned longsp; }; 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; //定義進程的結構體thread, 當中有eip和esp unsigned longtask_entry;//任務的函數起始處, 也就是任務第一次運行的起始位置 struct PCB *next;//一個任務鏈表, 指向下一個任務 }tPCB;</span>
//mymain.c #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;//定義是否調度, 1則調度, 0則不調度 void my_process(void); void __init my_start_kernel(void) //起始函數位置 { int pid = 0; int i; <strong>/* Initialize process 0*/</strong> 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]; <strong>//0號進程棧在最開始的位置</strong> task[pid].next = &task[pid]; <strong> /*fork more process */</strong> for(i=1;i<MAX_TASK_NUM;i++) { memcpy(&task[i],&task[0],sizeof(tPCB));//復制0號進程的結構形式 task[i].pid = i; task[i].state = -1;//初始的任務(除0號進程外)都設置成未運行 task[i].thread.sp = (unsigned long)&task[i].stack[KERNEL_STACK_SIZE-1]; task[i].next = task[i-1].next;<strong>//新fork的進程加到進程鏈表的尾部, 該新建任務的next指向上一個任務的next,也就是自己(最後一個)</strong> task[i-1].next = &task[i]; <strong>//配置上一個任務的next指向這時候新創建的任務</strong> } /* start process 0 by task[0] */ pid = 0; my_current_task = &task[pid];//先讓0號進程先運行 <strong> asm volatile( "movl %1,%%esp\n\t" /* set task[pid].thread.sp to esp */ "pushl %1\n\t" /* push ebp ,當前esp=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*/ );</strong> } 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)//推斷是否調度。該值可有itnerrupt.c中的函數來配置 { my_need_sched = 0; my_schedule(); //主動調動的機制 } printk(KERN_NOTICE "this is process %d +\n",my_current_task->pid); } } }
//myinterrupt.c #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)//時鐘中斷1000次的時候,調度一次, 配置調度值為1 { printk(KERN_NOTICE ">>>my_timer_handler here<<<\n"); my_need_sched = 1; } time_count ++ ; #endif return; } void my_schedule(void) //<span style="color:#ff0000;">調度函數, 核心函數</span> { tPCB * next;//定義兩個指針 tPCB * prev; if(my_current_task == NULL //當前進程和下一進程為空, 即沒有任務, 返回 || my_current_task->next == NULL) { return; } printk(KERN_NOTICE ">>>my_schedule<<<\n"); <strong><span style="color:#ff0000;">/* 在調度函數中, next指向的是下一個將要被調度的任務, prev指向的是當前正在運行的任務*/</span></strong> /* schedule */ next = my_current_task->next;//把當前進程的下一個進程賦值給next。當前進程賦值給prev prev = my_current_task; if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */ { //<strong>假設下一個任務不是第一次被調度, 則運行,下一個進程<span style="color:#ff0000;">有進程上下文</span></strong> /* switch to next process */ <span style="color:#ff0000;">asm volatile( "pushl %%ebp\n\t" /* save 當前進程 ebp */ "movl %%esp,%0\n\t" /* save 當前 esp 賦值到prev.thread.sp */ "movl %2,%%esp\n\t" /* restore 下一個進程的sp到 esp */ "movl $1f,%1\n\t" /*<strong> save 當前進程的 eip =[ 1:]處地址,即下一次從[ 1:]處開始繼續運行</strong> */ /* 啟動下一個進程*/ "pushl %3\n\t" /*保存下一個進程eip保存到棧裏面*/ "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) ); </span> my_current_task = next; printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid); } else { <strong> //下一個進程為第一次運行時,<span style="color:#ff0000;">沒有進程上下文</span>, 則以以下這樣的方式來處理</strong> next->state = 0; my_current_task = next; printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid); /* switch to new process */ <span style="color:#ff0000;">asm volatile( "pushl %%ebp\n\t" /* save ebp */ "movl %%esp,%0\n\t" /* save esp */x` "movl %2,%%esp\n\t" /* restore esp */ "movl %2,%%ebp\n\t" /* restore ebp */ "movl $1f,%1\n\t" /*<strong> save 當前進程的 eip =[ 1:]處地址,即下一次從[ 1:]處開始繼續運行</strong> */ /* 啟動下一個進程*/ "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) ); </span> } return; }
author: 於凱
參考課程:《Linux內核分析》MOOC課程http://mooc.study.163.com/course/USTC-1000029000
一個簡單的時間片輪轉多道程序內核操作系統工作流程