实验二:简单的时间片轮转多道程序内核代码
实验要求
完成一个简单的时间片轮转多道程序内核代码
实验过程
1、默认代码执行
使用实验楼的虚拟机打开 shell
# 注意路径是区分大小的
$ 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、完成一个简单的时间片轮转多道程序内核代码
(1)mypcb.h文件
/*
* linux/mykernel/mypcb.h
*
* Kernel internal PCB types
*
* Copyright (C) 2013 Mengning
*
*/
#define MAX_TASK_NUM 4 /* 最大任务数为4*/
#define KERNEL_STACK_SIZE 1024*2 /*内核堆栈为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);
在默认代码的基础上创建mypcb.h头文件,以此来添加对进程控制块的定义
(2)mymain.c
/*
* linux/mykernel/mymain.c
*/
#include "mypcb.h"
#include <linux/tty.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; /*初始化进程id*/
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" /*堆栈栈顶寄存器指向进程0的栈底,将task[pid].thread.ip初始化为进程0栈底*/
"pushl %1\n\t" /*压入EDP的值*/
"pushl %0\n\t" /*压入EIP值*/
"ret\n\t"
"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%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_schedule函数
/*
* linux/mykernel/myinterrupt.c
*/
#include "mypcb.h"
#include <linux/types.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/tty.h>
#include <linux/vmalloc.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.
*/
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"
"movl %%esp,%0\n\t"
"movl %2,%%esp\n\t"
"movl $1f,%1\n\t"
"pushl %3\n\t"
"ret\n\t"
"1:\t"
"popl %%ebp\n\t"
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
}
else
{
next->state = 0;
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" /*压栈进程0的值EDP*/
"movl %%esp,%0\n\t" /*将ESP压入PCB中*/
"movl %2,%%esp\n\t" /*切换到进程1*/
"movl %2,%%ebp\n\t"
"movl $1f,%1\n\t" /*$1f保存至进程0的thread.ip*/
"pushl %3\n\t"。/*将task[1].thread.ip压入栈中*/
"ret\n\t"
"1:\t"
"popl %%ebp\n\t"
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
}
return;
}
实验结果
总结
本次的实验更加接近操作系统的知识了,本次的时间片管理实现让我进一步认识了进程切换时对于当前进度的保存和恢复,主要依赖于ESP和EIP两个寄存器的变化