一个简单的基于mykernel时间片轮转多道程序内核代码分析
一个简单的基于mykernel时间片轮转多道程序内核代码分析
简介
学号375,。原创作品转载请注明出处 https://github.com/mengning/linuxkernel/
本实验是基于实验楼http://www.shiyanlou.com/courses/195提供的虚拟机完成该实验的。
实验过程
1.使用实验楼的虚拟机打开shell
(1)cd LinuxKernel/linux-3.9.4
(2)patch -p1 < …/mykernel_for_linux3.9.4sc.patch
(3)通过https://github.com/mengning/mykernel获取关于mymain.c等的源码覆盖mykernel文件夹下的相关文件。
(4)make allnoconfig
(5) make
(6)qemu -kernel arch/x86/boot/bzImage
在这里可以看到一个操作系统启动完成,并一直运行输出my_timer_handler here。分析一下mymain.c和myinterrupt.c
这里我们发现系统启动完成后会调用_initmy_start_kernel方法,和my_time_handleer方法。我们只需要在上面编写我们的调度策略代码,就可以实现系统的时间片轮转了。
2.实验程序
(1)从https://github.com/mengning/mykernel/tree/master/mykernel-1.1获得扩展_initmy_start_kernel方法,和my_time_handleer方法的源代码。
(2)把源代码放到实验楼虚拟机的相应目录下。
(3)重新编译并运行查看实验结果。
3.执行结果
代码分析
1.mypcb.h
#define MAX_TASK_NUM 4
#define KERNEL_STACK_SIZE 1024*2 # unsigned long
/* 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 */
unsigned long 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);
mypcb.h对进程的PCB进行数据结构的定义,一些常量的定义,还有my_schedule函数的声明。
宏:
MAX_TASK_NUM 代表最大进程数为4
KERNEL_STACK_SIZE 代表每个进程堆栈的大小
PCB:
pid 进程的唯一标识,用于区分进程
state 进程当前的状态,之所以声明为volatile是希望编译器不要对其进行优化,保证每次都能从内存中获取此值。
stack[KERNEL_STACK_SIZE] 进程的堆栈
thread 进程的ip(程序指针)和sp(栈顶指针)
task_entry 进程的入口
next 下一个PCB,将所有PCB连起来,形成环,从而不断循环切换进程。
2.mymain.c
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].stack[KERNEL_STACK_SIZE-1] - 1) = (unsigned long)&task[i].stack[KERNEL_STACK_SIZE-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 */
:
: "c" (task[pid].thread.ip),"d" (task[pid].thread.sp) /* input c or d mean %ecx/%edx*/
);
}
int i = 0;
void my_process(void)
{
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);
}
}
}
函数__init my_start_kerne()是最开始被运行的。最初先初始化进程0,运行起来后再通过一个for循环初始化其他三个进程的pcb。并把这些pcb串成一个链表。
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 */
:
: "c" (task[pid].thread.ip),"d" (task[pid].thread.sp) /* input c or d mean %ecx/%edx*/
);
此部分是嵌入式汇编,通过对sp,ip寄存器的直接操作让相应的进程能够运行起来。
3.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)
{
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)
);
}
return;
通过my_time_handler()函数定时地不断向cpu发出中断,从而实现了时间片轮转。设置变量my_need_sched为1表示当前需要执行,也就是执行mymain.c的my_process()部分。my_schedule是整个程序的关键,它实现的是进程的上下文切换,简单的说主要逻辑是将前一个进程的相关信息保存,并读入下一个进程的相关信息,从而完成进程的切换。关键代码段:
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)
);
通过嵌入式汇编操作保存当前进程信息,完成进程之间的调度。
实验心得
通过这次实验,我对操作系统的进程调度和中断有了更深刻的认识。对以后的学习有很大的帮助。