基于mykernel的一个简单的时间片轮转多道程序内核代码分析
Posted lucaschang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于mykernel的一个简单的时间片轮转多道程序内核代码分析相关的知识,希望对你有一定的参考价值。
学号023作品
本实验资源来源: https://github.com/mengning/linuxkernel/
一、观察简易操作系统
此处使用实验楼的虚拟机打开终端
cd LinuxKernel/linux-3.9.4
rm -rf mykernel
patch -p1 < ../mykernel_for_linux3.9.4sc.patch
make allnoconfig
make #编译内核请耐心等待
qemu -kernel arch/x86/boot/bzImage
在QEMU窗口中我们可以看到一个运行中的简易操作系统,并输出字符串:
>>>>>my_timer_handler here<<<<<
和my_start_kernel here
运行截图如下:
在此处分析一下mymain.c和myinterrupt.c
mymain.c
在my_start_kernel函数中有一个循环,不停地输出my_start_kernel here
通过时钟中断周期函数调用my_timer_handler函数,可输出>>>>>my_timer_handler here<<<<<的字符串
总而言之,在mykernel启动之后,系统调用my_start_kernel函数,并利用时钟中断周期函数周期性调用my_timer_handler函数。
二、一个简单的时间片轮转多道程序分析
1、从https://github.com/mengning/mykernel/tree/master/mykernel-1.1上获取mymain.c、myinterrupt.c、pcb.c三个文件
2、在mykernel文件下覆盖mymain.c、myinterrupt.c,并新建pcb.c文件
3、cd linux-3.9.4 执行下列命令,重新编译内核
make allnoconfig
make
qemu -kernel arch/x86/boot/bzImage
运行截图如下:
可见系统进程在切换
以下进行代码分析
mypcb.h : 进程控制块PCB结构体定义。
mymain.c: 初始化各个进程并启动0号进程。
myinterrupt.c:时钟中断处理和进程调度算法。
mypcb.h:
1 /* mykernel--Simple simulation of the linux OS process schedule 2 * 3 * linux/mykernel/mypcb.h 4 * 5 * Kernel internal my_timer_handler 6 * 7 * Copyright (C) 2013 Mengning 8 * 9 * Modified 2014 Yunquan Zhang <[email protected]> 10 * 11 * 12 * You can redistribute or modify this program under the terms 13 * of the GNU General Public License as published by 14 * the Free Software Foundation. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #define MAX_TASK_NUM 10 // max num of task in system 21 #define KERNEL_STACK_SIZE 1024*8 22 #define PRIORITY_MAX 30 //priority range from 0 to 30 23 24 /* CPU-specific state of this task */ 25 struct Thread { 26 unsigned long ip;//point to cpu run address 27 unsigned long sp;//point to the thread stack‘s top address 28 //todo add other attrubte of system thread 29 }; 30 //PCB Struct 31 typedef struct PCB{ 32 int pid; // pcb id 33 volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */ 34 char stack[KERNEL_STACK_SIZE];// each pcb stack size is 1024*8 35 /* CPU-specific state of this task */ 36 struct Thread thread; 37 unsigned long task_entry;//the task execute entry memory address 38 struct PCB *next;//pcb is a circular linked list 39 unsigned long priority;// task priority //////// 40 //todo add other attrubte of process control block 41 }tPCB; 42 43 //void my_schedule(int pid); 44 void my_schedule(void);
在这个文件里,定义了 Thread 结构体,用于存储当前进程中正在执行的线程的ip和sp,PCB结构体中的各个字段含义如下
pid:进程号
state:进程状态,在模拟系统中,所有进程控制块信息都会被创建出来,其初始化值就是-1,如果被调度运行起来,其值就会变成0
stack:进程使用的堆栈
thread:当前正在执行的线程信息
task_entry:进程入口函数
next:指向下一个PCB,模拟系统中所有的PCB是以链表的形式组织起来的。
my_schedule声明,在myinterrupt.c中实现,在mymain.c中的各个进程函数会根据一个全局变量的状态来决定是否调用它,从而实现主动调度。
mymain.c
1 /* 2 * linux/mykernel/mymain.c 3 * 4 * Kernel internal my_start_kernel 5 * 6 * Copyright (C) 2013 Mengning 7 * 8 */ 9 #include <linux/types.h> 10 #include <linux/string.h> 11 #include <linux/ctype.h> 12 #include <linux/tty.h> 13 #include <linux/vmalloc.h> 14 15 16 #include "mypcb.h" 17 18 tPCB task[MAX_TASK_NUM]; 19 tPCB * my_current_task = NULL; 20 volatile int my_need_sched = 0; 21 22 void my_process(void); 23 24 25 void __init my_start_kernel(void) 26 { 27 int pid = 0; 28 int i; 29 /* Initialize process 0*/ 30 task[pid].pid = pid; 31 task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped */ 32 task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process; 33 task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1]; 34 task[pid].next = &task[pid]; 35 /*fork more process */ 36 for(i=1;i<MAX_TASK_NUM;i++) 37 { 38 memcpy(&task[i],&task[0],sizeof(tPCB)); 39 task[i].pid = i; 40 task[i].state = -1; 41 task[i].thread.sp = (unsigned long)&task[i].stack[KERNEL_STACK_SIZE-1]; 42 task[i].next = task[i-1].next; 43 task[i-1].next = &task[i]; 44 } 45 /* start process 0 by task[0] */ 46 pid = 0; 47 my_current_task = &task[pid]; 48 asm volatile( 49 "movl %1,%%esp " /* set task[pid].thread.sp to esp */ 50 "pushl %1 " /* push ebp */ 51 "pushl %0 " /* push task[pid].thread.ip */ 52 "ret " /* pop task[pid].thread.ip to eip */ 53 "popl %%ebp " 54 : 55 : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp) /* input c or d mean %ecx/%edx*/ 56 ); 57 } 58 void my_process(void) 59 { 60 int i = 0; 61 while(1) 62 { 63 i++; 64 if(i%10000000 == 0) 65 { 66 printk(KERN_NOTICE "this is process %d - ",my_current_task->pid); 67 if(my_need_sched == 1) 68 { 69 my_need_sched = 0; 70 my_schedule(); 71 } 72 printk(KERN_NOTICE "this is process %d + ",my_current_task->pid); 73 } 74 } 75 }
my_start_kernel 是系统启动后最先调用的函数,在这个函数里完成了0号进程的初始化和启动,并创建了其它的进程PCB,以方便后面的调度。在模拟系统里,每个进程的函数代码都是一样的,即 my_process 函数,my_process 在执行的时候,会打印出当前进程的 id,从而使得我们能够看到当前哪个进程正在执行。
myinterrupt.c
1 /* 2 * linux/mykernel/myinterrupt.c 3 * 4 * Kernel internal my_timer_handler 5 * 6 * Copyright (C) 2013 Mengning 7 * 8 */ 9 #include <linux/types.h> 10 #include <linux/string.h> 11 #include <linux/ctype.h> 12 #include <linux/tty.h> 13 #include <linux/vmalloc.h> 14 15 #include "mypcb.h" 16 17 extern tPCB task[MAX_TASK_NUM]; 18 extern tPCB * my_current_task; 19 extern volatile int my_need_sched; 20 volatile int time_count = 0; 21 22 /* 23 * Called by timer interrupt. 24 * it runs in the name of current running process, 25 * so it use kernel stack of current running process 26 */ 27 void my_timer_handler(void) 28 { 29 #if 1 30 if(time_count%1000 == 0 && my_need_sched != 1) 31 { 32 printk(KERN_NOTICE ">>>my_timer_handler here<<< "); 33 my_need_sched = 1; 34 } 35 time_count ++ ; 36 #endif 37 return; 38 } 39 40 void my_schedule(void) 41 { 42 tPCB * next; 43 tPCB * prev; 44 45 if(my_current_task == NULL 46 || my_current_task->next == NULL) 47 { 48 return; 49 } 50 printk(KERN_NOTICE ">>>my_schedule<<< "); 51 /* schedule */ 52 next = my_current_task->next; 53 prev = my_current_task; 54 if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */ 55 { 56 /* switch to next process */ 57 asm volatile( 58 "pushl %%ebp " /* save ebp */ 59 "movl %%esp,%0 " /* save esp */ 60 "movl %2,%%esp " /* restore esp */ 61 "movl $1f,%1 " /* save eip */ 62 "pushl %3 " 63 "ret " /* restore eip */ 64 "1: " /* next process start here */ 65 "popl %%ebp " 66 : "=m" (prev->thread.sp),"=m" (prev->thread.ip) 67 : "m" (next->thread.sp),"m" (next->thread.ip) 68 ); 69 my_current_task = next; 70 printk(KERN_NOTICE ">>>switch %d to %d<<< ",prev->pid,next->pid); 71 } 72 else 73 { 74 next->state = 0; 75 my_current_task = next; 76 printk(KERN_NOTICE ">>>switch %d to %d<<< ",prev->pid,next->pid); 77 /* switch to new process */ 78 asm volatile( 79 "pushl %%ebp " /* save ebp */ 80 "movl %%esp,%0 " /* save esp */ 81 "movl %2,%%esp " /* restore esp */ 82 "movl %2,%%ebp " /* restore ebp */ 83 "movl $1f,%1 " /* save eip */ 84 "pushl %3 " 85 "ret " /* restore eip */ 86 : "=m" (prev->thread.sp),"=m" (prev->thread.ip) 87 : "m" (next->thread.sp),"m" (next->thread.ip) 88 ); 89 } 90 return; 91 }
my_time_handler()函数,是个时间片轮转,周期性地发出中断信号,也就是my_need_sched。my_start_kernel()完成每个进程初始化,每个进程的任务都是my_process(),由于这个函数中有个无限循环,任务永远不会结束;并且启动了0号进程。任务需要调度时根据任务链表顺序进行调度。
实验总结
通过本次实验,对于进程调度和中断有了更加深刻的了解。总的来说,操作系统的工作需要进行进程的启动与切换,切换时需要保存上下文,切换的程序完成后需要恢复上下文,进程切换通过时钟中断进行控制。
以上是关于基于mykernel的一个简单的时间片轮转多道程序内核代码分析的主要内容,如果未能解决你的问题,请参考以下文章
一个简单的时间片轮转多道程序内核代码分析 (学号后三位418)
Linux内核分析—完成一个简单的时间片轮转多道程序内核代码