实验二 完成一个简单的时间片轮转多道程序内核代码
1.使用实验楼的虚拟机打开 shell
2.输入如下代码
代码含义:1)将当前工作目录更改为 Linux 内核源代码的目录路径
2)递归地删除名为 mykernel 的目录
3)使用 patch 工具来应用一个补丁文件 mykernel_for_linux3.9.4sc.patch 到当前工作目录下的 Linux 内核源代码中
4)使用make命令生成.config配置文件
接下来使用qemu命令加载指定的 Linux 内核映像文件,即刚才编译完成的Linux内核。
进入mykernel目录,查看qemu 窗口输出的内容的代码 mymain.c 和 myinterrupt.c。
3.完成一个简单的时间片轮转多道程序内核代码
1.mypcb.h文件
该文件定义了一个名为 tPCB
的数据结构,并声明了一个函数 my_schedule
。tPCB
包含进程ID、任务状态、任务入口地址、指向下一个任务控制块指针的地址等信息。
//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; //记录进程执行时eip,esp等值
unsigned long task_entry;
struct PCB *next;
}tPCB;
void my_schedule(void); //调度函数
2.mymain.c文件
该文件是一个简单的多任务调度的实现,其中包含了一个名为my_start_kernel
的函数和一个名为 my_process
的函数。其中,函数my_start_kernel
用于初始化操作系统,设置进程 0(即系统初始化进程)以及其他进程的任务控制块,并通过汇编代码设置初始的任务堆栈和指令指针。函数 my_process
调用 my_schedule
函数进行任务调度。
//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*/
/*初始化0号进程,状态为运行,进程逻辑为process函数*/
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 */
/*fork多个进程,构成一个循环进程链表*/
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的进程
*分别让cpu寄存器esp,ebp指向本进程的堆栈空间
*eip寄存器指向my_process函数,这样进程被启动之后就开始执行my_process代码
*/
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_need_sched配合时钟中断处理函数,实现进程调度
*/
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);
}
}
}
3.myinterrupt.c文件
该文件是一个简单的多任务调度实现中的中断处理函数和任务调度函数。其中,函数my_timer_handler
是一个定时器中断处理函数。函数 my_schedule
用于进行任务调度。具体的切换过程使用了汇编代码来保存和恢复寄存器,并跳转到下一个任务的执行入口点。
//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;
/*下一进程状态为0*/
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 */
/*
*1.保存当前进程环境ebp,esp,eip
*2.恢复下一个进程堆栈环境
*3.eip指向下一个进程的入口
*/
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)
);
}
else
{ /*
*从未被调度过的进程
*除了保存当前进程(prev)的堆栈环境,还需要新建(不是恢复)下一个进程的堆栈环境
*/
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;
}
4.实验结果
将文件重新修改编译后,运行结果如下:
5.总结
本实验通过时钟中断驱动的进程切换模拟了现代操作系统中重要的两个部分:中断、进程切换。其中进程切换时上下文保护是实验重点关注的内容。通过编写代码加深了自身对于时间片轮转的理解,亲身体会到os是如何实现多道程序运行环境的。