一、概述
本文通过分析一个简单的时间片轮转多道程序的内核 mykernel,来理解操作系统是如何工作的。
mykernel 是孟宁老师的一个开源项目,借助 Linux 内核部分源代码模拟存储程序计算机工作模型及时钟中断,添加与修改部分代码实现的(详情点 此处)。未加入进程管理功能的 mykernel,会在初始化后,周期性地执行时间中断处理程序。在实验楼虚拟机的终端输入以下指令(图形模式下),即可运行 mykernel 。运行时效果如下:
可以看出,这个系统目前只是简单地不停输出“my_timer_handler here ”、“my_start_kernel here”等字样。通过查看源码,可以发现这些输出字符串分别位于 mymain.c 和 myinterrupt.c 中。只要在此基础上,再加入进程描述 PCB 和进程链表管理、进程切换等代码,一个可运行的小OS kernel 就完成了。下面便开始逐步实现这些功能(本文所有代码均由孟宁老师提供,笔者只分析其功能与实现原理)。
二、添加 PCB 描述信息
为了实现进程管理,需要先引入进程块描述信息,我们将其放入一个新的头文件 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
{
unsigned longip;
unsigned longsp;
};
/* Processing Control Block */
typedef struct PCB
{
int pid;
volatile long state;/* -1 unrunnable, 0 runnable, >0 stopped */
char stack[KERNEL_STACK_SIZE];
struct Thread thread;
unsigned longtask_entry;
struct PCB *next;
}tPCB;
void my_schedule(void); /* code in myinterrupt.c */
由代码可知,一个 PCB 的相关信息,是通过结构体进行封装的,其组成元素的含义及用途,由变量名及注释即可知其意,就不详述了。
三、PCB初始化(修改 mymain.c )
未修改前, mymain.c 中只有如下一个初始化函数,周期性地输出当前运行位置:
void __init my_start_kernel(void)
{
int i = 0;
while(1)
{
i++;
if(i%100000 == 0)
printk(KERN_NOTICE "my_start_kernel here %d \n",i);
}
}
显然,进程块的初始化信息也应添加到这个初始化函数中。
#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*/
);
}
原来的初始化函数中的打印输出信息,就交由 my_process() 处理了,并调用了进程调度管理程序。
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);
}
}
}
四、时间片分配(修改 myinterrupt.c )
未修改前,时间中断处理函数my_timer_handler()内只有一条标识位置的打印输出指令:
void my_timer_handler(void)
{
printk(KERN_NOTICE "\n>>>>>>>>>>>>>>>>>my_timer_handler here<<<<<<<<<<<<<<<<<<\n\n");
}
现在我们修改下,让其支持进程的时间片轮转功能:
#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;
}
现在的 my_time_handle() 所执行的主要功能为:每被中断调用1000次,且没有需要调度的进程时,就将全局变量 my_need_sched 置为1(中断返回后,正在执行的进程就会执行调度程序 my_schedule())。进程每次执行的时间片长度由 time_count 控制。
当一个进程执行完毕,或用完所分配的时间片后,就会让位给其他进程运行。进程切换功能由 my_schedule() 实现。
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 */
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)
);
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 */
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;
}
</pre><pre name="code" class="cpp"><img src="http://img.blog.csdn.net/20160306110638573" alt="" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);" />
my_schedule() 的流程如下:
(未定稿)