实验二:完成一个简单的时间片轮转多道程序内核代码
- 实验步骤
- 使用实验楼的虚拟机打开 shell
- 然后 cd mykernel 您可以看到 qemu 窗口输出的内容的代码 mymain.c 和 myinterrupt.c
- 使用自己的 Linux 系统环境
- 实验结果截图
- 下图用make编译了内核
- 下图是执行默认的
- 下图是编写好自己的内核程序后的执行结果(下附代码及分析)
- 实验代码及分析
- 以下为新增内容mypcb.h(进程控制块)
#define MAX_TASK_NUM 4
#define KERNEL_STACK_SIZE 1024*2
struct Thread {
unsigned long ip; //eip
unsigned long sp; //esp
};
typedef struct PCB{
int pid; //进程id
volatile long state; //-1表示不可运行,0表示可运行,>0表示停止
unsigned long stack[KERNEL_STACK_SIZE]; //内核堆栈
struct Thread thread; //线程
unsigned long task_entry; //入口
struct PCB *next; //next指针
}tPCB;
void my_schedule(void);
#include
#include
#include
#include
#include
#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;
task[pid].pid = pid;
task[pid].state = 0;
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];
for(i=1;ipid);
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(中断控制函数)
#include
#include
#include
#include
#include
#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;
void my_timer_handler(void)
{
#if 1
if(time_count%1000 == 0 && my_need_sched != 1)
{
printk(KERN_NOTICE ">>>my_timer_handler here<<next == NULL)
{
return;
}
printk(KERN_NOTICE ">>>my_schedule<<next;
prev = my_current_task;
if(next->state == 0)
{
my_current_task = next;
printk(KERN_NOTICE ">>>switch %d to %d<<pid,next->pid);
asm volatile(
"pushl %%ebpnt"
"movl %%esp,%0nt"
"movl %2,%%espnt"
"movl $1f,%1nt"
"pushl %3nt"
"retnt"
"1:t"
"popl %%ebpnt"
: "=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<<pid,next->pid);
asm volatile(
"pushl %%ebpnt"
"movl %%esp,%0nt"
"movl %2,%%espnt"
"movl %2,%%ebpnt"
"movl $1f,%1nt"
"pushl %3nt"
"retnt"
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
}
return;
}
- 实验中遇到的问题及解决方案
- 实验中在mykernel目录下使用make会报错,因为只有在linux-3.9.4文件夹下的makefile文件才有使用价值,返回到上一目录后再使用make即可解决问题