一、实验要求
完成一个简单的时间片轮转多道程序内核代码
二、实验过程
1.输入以下指令编译运行mykernel
$ 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
执行结果如下
2.查看mymain.c与myinterrupt.c的源代码
首先通过cat命令查看mymain.c
的代码,如下:
可以看到,在my_start_kernel函数中,不停地循环执行一条输出语句,打印出了在QEMU窗口中看到的>>>my_timer_handler here <<<<<
然后查看myinterrupt.c
的代码,如下:
这是一个被时钟中断状态下调用的函数:my_timer_handler,调用这个函数的结果是打印出my_start_kernel here + number 语句。
于是,以上两个函数不断执行,产生了上述的效果,模拟出了一个简单的操作系统。
3.一个简单的时间片轮转多道程序
获取代码,覆盖前文所述的mykernel文件夹下mymain.c和myinterrupt.c,并新增mypcb.h,重新用之前的命令编译、运行,效果如下:
可以看到此时内核已经在模拟进程运行和切换,在进程0,1,2,3直接不停切换。
代码分析
mypcb.h
#define MAX_TASK_NUM 4
#define KERNEL_STACK_SIZE 1024*2
/* 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);
在这个文件里,定义了 Thread 结构体,用于存储当前进程中正在执行的线程的ip和sp。
- ip :保存当前指令执行位置
- sp:保存栈顶位置
文件还定义了PCB结构体,其中各个字段含义如下
- pid:进程号
- state:进程状态,在模拟系统中,所有进程控制块信息都会被创建出来,其初始化值就是-1,如 果被调度运行起来,其值就会变成0
- stack:进程使用的堆栈
- thread:当前正在执行的线程信息
- task_entry:进程入口函数
- next:指向下一个PCB,模拟系统中所有的PCB是以链表的形式组织起来的。
这里还有一个函数的声明 my_schedule
,它的实现在my_interrupt.c中,在mymain.c中的各个进程函数会根据一个全局变量的状态来决定是否调用它,从而实现主动调度。
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;
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].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 rsp */
"pushl %1\n\t" /* push rbp */
"pushl %0\n\t" /* push task[pid].thread.ip */
"ret\n\t" /* pop task[pid].thread.ip to rip */
:
: "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);
}
}
}
在第一部分中已经看到,函数 my_start_kernel
是系统启动后立即调用的函数,在这个函数里完成了0号进程的初始化和启动,并创建了其它的进程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*/
);
这是嵌入式汇编的部分,这里的%1指的是task[pid].thread.sp
,%0是task[pid].thread.ip
分别解读每个语句的意思:
movl %1,%%esp\n\t
将原堆栈的栈顶放到sp寄存器中pushl %1\n\t
将ep寄存器的值存入栈pushl %0\n\t
将当前进程ip值入栈
如此一来,0号进程开始启动,程序接下来就去调用my_process()
在当前系统里,每个进程都调用my_process
函数,my_process
在执行的时候,在界面中打印出当前调度了几号进程。
此外 my_process
中也会判断一个全局标志变量 my_need_sched
,当其值为 1 ,就调用 my_schedule
完成进程的调度。
myinterrupt.c
void my_timer_handler(void) {
if(time_count%1000 == 0 && my_need_sched != 1)
{
printk(KERN_NOTICE ">>>my_timer_handler here<<<\n");
my_need_sched = 1;
}
time_count ++ ;
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_timer_handler
函数会被内核周期性的调用,每调用1000次,全局变量my_need_sched
的值重置为1,并通知正在执行的进程执行调度程序my_schedule。因此,题目中所说的时间片轮转就是 my_timer_handler
函数,它在轮转中周期性地发出中断信号。
在my_schedule
函数中,完成进程的切换。
进程的切换分两种情况,一种情况是下一个进程没有被调度过,另外一种情况是下一个进程被调度过,可以通过下一个进程的state知道其状态。
进程切换依然是通过内联汇编代码实现,无非是保存旧进程的eip和堆栈,将新进程的eip和堆栈的值存入对应的寄存器中。
my_start_kernel()
完成每个进程初始化,每个进程的任务都是my_process()
,由于这个函数中有个无限循环,任务永远不会结束;并且启动了0号进程。任务需要调度时根据任务链表顺序进行调度。
总结
- 通过本次实验内容,加深了我对操作系统的理解;结合堆栈知识和一些特殊寄存器的变化,清晰了调度过程中发生中断的原因与过程; 最后,通过本次实验明白了进程调度和中断机制再操作系统中的重要性。