完成一个简单的时间片轮转多道程序内核代码
实验步骤:
实验结果:
使用make 编译内核
?下图默认执行:
?
查看mymain.c文件
?
?查看myinterrupt.c文件:
mykernel时间片轮转代码分析
新增mypcb.h
#define MAX_TASK_NUM 4
#define KERNEL_STACK_SIZE 1024*2
/* CPU-specific state of this task */
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);
修改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)); //把0号进程的状态赋给新进程
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" /* 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);
}
}
}
修改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;
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" /* 保存当前EBP到堆栈中 */
"movl %%esp,%0\n\t" /* 保存当前ESP到当前进程PCB中 */
"movl %2,%%esp\n\t" /* 将next进程的堆栈栈顶的值存到ESP寄存器 */
"movl $1f,%1\n\t" /* 保存当前进程的EIP值 */
"pushl %3\n\t" /* 将next进程继续执行的代码位置压栈 */
"ret\n\t" /* 出栈标号1到EIP寄存器 */
"1:\t" /* 标号1,即next进程开始执行的位置 */
"popl %%ebp\n\t" /* 恢复EBP寄存器的值 */
: "=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 new process */
asm volatile(
"pushl %%ebp\n\t" /* 保存当前进程EBP到堆栈 */
"movl %%esp,%0\n\t" /* 保存当前进程ESP到PCB */
"movl %2,%%esp\n\t" /* 载入next进程的栈顶地址到ESP寄存器 */
"movl %2,%%ebp\n\t" /* 载入next进程的堆栈基地址到EBP寄存器 */
"movl $1f,%1\n\t" /* 保存当前EIP寄存器值到PCB,这里$1f是指上面的标号1 */
"pushl %3\n\t" /* 把即将执行的进程代码入口地址入栈 */
"ret\n\t" /* 出栈进程的代码入口地址到EIP寄存器 */
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
}
return;
}
再次退回到linux-3.9.4目录下 make 然后执行qemu-system-x86_64 -kernel arch/x86/boot/bzImage
总结
计算机工作的三个法宝:
????????通过本次实验楼的学习,我对于操作系统的工作原理更加清楚。最重要的是:在操作系统中,当时间片用完需要切换进程时,需要保存进程的运行环境和进度,待下次再被调度时,需要恢复这样的运行环境。
|