姓名 胡子宸
0.打开shell
(编译内核之后就看不到之前的代码了,下图是实验环境断了之后重新做的)
$ cd ~/LinuxKernel/linux-3.9.4
进入Linux内核源码所在目录
$ rm -rf mykernel
递归删除mykernel文件夹
$ patch -p1 < ../mykernel_for_linux3.9.4sc.patch
打一个补丁,该补丁被用于当前目录的Linux内核源码
$ make allnoconfig
生成默认配置文件
$ make
根据刚才生成的配置文件编译内核,完成之后如下图
$ qemu -kernel arch/x86/boot/bzImage
使用qemu虚拟机加载刚刚编译的内核(是个死循环)
1.完成时间片乱转多道程序内核代码
关闭死循环,并cd mykernel,代码内已添加额外注释来分析
1.1 mypcb.h
来源于mooc
#define MAX_TASK_NUM 4//最大任务数
#define KERNEL_STACK_SIZE 1024*8//内核栈大小
/* 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 */ // CPU状态
char stack[KERNEL_STACK_SIZE];
/* CPU-specific state of this task */
struct Thread thread;
unsigned long task_entry;
struct PCB *next;
}tPCB;
//定义的pcb结构体包含以下几项:1.某个进程的CPU状态 2.该进程的内核栈
void my_schedule(void);//定义一个函数
1.2 mymain.c
这是原本的mymain.c,死循环,无跳出条件,符合i%100000==0时输出当前i值
来源于mooc
#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].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*/
);
}
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);
}
}
}
该main函数模拟多任务运行
1.3 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)//满足该条件时触发任务调度
//即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;
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)
//保存当前任务的ip、sp
);
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;
}
该函数模拟任务调度器
1.4 重新编译之后的实验结果
修改完三个文件之后回到上级目录重新make,再加载内核,结果如下