王智生---原创作品转载请注明出处
本系列为《Linux内核分析》MOOC课程(http://mooc.study.163.com/course/USTC-1000029000 )对应学习笔记,文章不定期更新
基于mykernel的时间片轮转调度的实现
本次实验是在linux-3.9.4内核源码库基础上编写程序实现简单的时间片轮转调度功能,也就是每隔一段时间(时间片)切换一次进程。
一、系统环境搭建
资料下载地址:https://github.com/mengning/mykernel
系统环境:ubuntu-kylin14.10_64位
//soft link,以后执行qemu命令就等效于执行qemu-system-i386(模拟32位的x86平台)
二、GNC内联汇编
(空白,后期补上)
三、代码分析
实验主要有三个文件,分别是:mypcb.h、mymain.c、myinterrupt.c,下面依次做下简要介绍:
mypcb.h
/*
* linux/mykernel/mypcb.h
* Kernel internal PCB types
* Copyright (C) 2013 Mengning
*/
#define MAX_TASK_NUM 4
#define KERNEL_STACK_SIZE 1024*8
/* CPU-specific state of this task */
struct Thread {
unsignedlong ip;
unsignedlong sp;
};
typedefstruct PCB{
int pid; //进程ID
volatilelong state;/* -1unrunnable, 0 runnable, >0 stopped */
char stack[KERNEL_STACK_SIZE];
/* CPU-specific state of this task */
struct Thread thread;
unsignedlong task_entry; //程序入口
struct PCB *next; //链表指针
}tPCB;
void my_schedule(void);
mypcb.h中主要定义了本次实验最大进程数、内核堆栈大小,两个结构体Thread和PCB,前者存放进程的ip、sp,后者实现类似进程描述符保存进程的关键信息,最后还声明了进程调度程序my_schedule。
*thread在操作系统中准确来说应该为线程,linux下进程和线程是相似的,不同于其他操作系统,这里就不用区分了
mymain.c
/*
* linux/mykernel/mymain.c
* Kernel internal my_start_kernel
* Copyright (C) 2013 Mengning
*/
#include
#include
#include
#include
#include
#include"mypcb.h"
tPCB task[MAX_TASK_NUM];
tPCB * my_current_task = NULL;
volatileint 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 = (unsignedlong)my_process;
task[pid].thread.sp = (unsignedlong)&task[pid].stack[KERNEL_STACK_SIZE-1];
task[pid].next = &task[pid];
/*fork more process */
for(i=1;i
{
memcpy(&task[i],&task[0],sizeof(tPCB));
task[i].pid = i;
task[i].state = -1;
task[i].thread.sp = (unsignedlong)&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 toeip */
"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%1000000000 == 0)
{
printk(KERN_NOTICE "this isprocess %d -\n",my_current_task->pid);
if(my_need_sched == 1)
{
my_need_sched = 0;
my_schedule();
}
else my_schedule();
printk(KERN_NOTICE "this is process %d +\n",my_current_task->pid);
}
}
}
主要声明两个函数:
my_start_kernel:主要实现进程的创建,本次实验中首先声明并运行0号进程,之后依次创建其余进程(处于就绪状态没有运行);
my_process:进程的内容,在程序中调用my_schedule实现进程轮转调度;
myinterrupt.c
/*
* linux/mykernel/myinterrupt.c
* Kernel internal my_timer_handler
* Copyright (C) 2013 Mengning
*/
#include
#include
#include
#include
#include
#include"mypcb.h"
extern tPCB task[MAX_TASK_NUM];
extern tPCB * my_current_task;
externvolatileint my_need_sched;
volatileint time_count = 0;
/*
* Called by timer interrupt.
* it runs in the name of currentrunning process,
* so it use kernel stack of currentrunning process
*/
void my_timer_handler(void)
{
#if 1
if(time_count%1000 == 0 &&my_need_sched != 1)
{
printk(KERN_NOTICE ">>>my_timer_handlerhere<<<\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 */
{
/* switch to next process---->进程上下文切换*/
asmvolatile(
"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 */
//$1f--->means the location of标号 1:
"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)
);
my_current_task= next;
printk(KERN_NOTICE">>>switch%d to %d<<<\n",prev->pid,next->pid);
}
else
{
next->state = 0;
my_current_task = next;
printk(KERN_NOTICE ">>>switch%d to %d<<<\n",prev->pid,next->pid);
/* switch to new process */
asmvolatile(
"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:中断处理程序,其时钟中断机制由qemu硬件模拟器内部实现,该中断处理程序周期执行,独立于其他程序。
my_schedule:进程轮转调度的实现函数,后面会详细介绍
l 程序执行过程:
程序由my_start_kernel开始执行,依次初始化所有进程,注意每个进程都有自己独立的堆栈,之后启动0号进程,配置相关环境。实现代码为:
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 toeip */
"popl%%ebp\n\t"
:
:"c" (task[pid].thread.ip),"d" (task[pid].thread.sp) /* input c or d mean %ecx/%edx*/
);
该段汇编将进程堆栈的SP、BP值赋给模拟x86硬件平台的SP、BP,加之前面
task[pid].task_entry =task[pid].thread.ip = (unsignedlong)my_process;
声明了进程入口地址即为my_process函数入口地址(不同进程均对应my_process函数,这里就用process_0表示);此时process_0启动,每隔一段时间判断进程是否需要轮换(my_need_sched=1?),中断处理程序则是在一定时间后发出轮换的命令(即令my_need_sche为1),之后便进入my_schedule实现进程轮换,依此循环。
轮换实现机制:
两个指针prev和next分别指向当前进程和待轮换的下一进程,之后用一段内联汇编:
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 */
//$1f--->means the location of标号 1:
"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)
);
前两句将当前process的ebp入栈,esp存入prev->thread.sp中,将新process的next->thread.sp存入esp,之后执行:
"movl $1f,%1\n\t" /* saveeip */
把标号“1”对应的地址存入prev->thread.ip,1f中f应为”forward”即向前寻找标号为“1”的地址,即下面的:
"1:\t" /* next process start here */
所以下次重新轮到当前process时,程序从eip所对应的地址即该处开始执行,下面一句为:
"popl %%ebp\n\t"
把最开始压入栈的ebp弹出,下次process_0就可以从当前执行位置继续执行,实现了保护现场和恢复现场的作用。
中间的一段:
"pushl%3\n\t"
"ret\n\t" /* restore eip */
则是将下一process的eip地址赋给当前eip;总个汇编执行完就实现了进程的轮换调度!
系统搭建好之后,在终端下依次输入:
qemu窗口中可以看到相应的提示!!!
(注意!这里我QEMU自身问题导致中断处理程序没法运行。。。。暂时还在调试中)
总结:
这只是一个简单的例子来说明进程时间片轮换调度的实现,具体到操作系统的工作原理,操作系统在bootloader启动的后面会启动一个init的进程,为所有其他进程的父进程,由该父进程陆续启动n多的子进程,进行进程的管理和调度。