寇亚飞 + 原创作品转载请注明出处 + 《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000
实验内容
完成一个简单的时间片轮转多道程序内核代码
- 完成一个简单的时间片轮转多道程序内核代码,代码见视频中或从mykernel找
- 分析进程的启动和进程的切换机制
- 理解操作系统如何工作
实验步骤及结果
参见这实验报告
代码分析
mypcb.h
#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 */
char 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头文件中实现了以下几个声明:
- 最大进程数MAX_TASK_NUM,即最多有四个进程受时间片轮转调度
- 进程的栈大小KERNEL_STACK_SIZE,即每个进程运行时有8KB大小的堆栈可以使用
- Thread结构体,包含指令指针ip和栈顶指针sp
- 进程控制块PCB,包含进程编号pid,进程状态state,进程堆栈stack,每个进程的指令指针和栈顶指针,入口函数地址task_entry,以及进程指针next,用于将进程连接起来
- 调度函数my_schedule,用于进程执行一段时间后切换
mymain.c
#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);
}
}
}
mymain.c中,task数组,用来存放所有进程;my_current_task用来指向当前进程;my_need_sched用来标记进程执行状态,-1为未执行,0为执行;__init my_start_kernel 函数是内核的入口,它完成了初始化进程并启动进程的工作。首先初始化0号进程,为其分配pid,state,task_entry和栈顶指针sp,并将其next指针指向自己;接着在循环里创建所有进程,将0号进程的状态信息拷贝过去,修改pid,state,栈顶指针sp以及将所有进程用next指针链接起来。
进程创建完毕后,将my_current_task指针指向0号进程,通过嵌入式汇编代码,开始执行。
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*/
);
在上面代码中,最后一行代表输入,”c”代表寄存器ecx,其内容为task[pid].thread.ip,即该进程的eip,标号为%0;”d”代表寄存器edx,其内容为task[pid].thread.sp,即栈顶指针,标号为%1。然后开始看汇编代码,第一行将%1置入esp,即esp指向了当前进程的栈顶;第二行将当前进程的esp压栈,此时栈为空,即将当前进程的ebp压栈,esp向下移一个单位;第三行将%0压栈,即将当前进程的指令指针eip压栈;第四行ret,即popl eip, 将eip指向当前进程的eip,开始执行当前进程;第五行表时执行完后,出栈返回之前的ebp。
具体过程如下图:
然后实现了my_process函数,每个进程都是执行此函数。每10000000次,打印当前进程的pid,并对my_need_sched进行判断,若为1,则执行调度,然后打印调度后的进程pid。
myinterrupt.c
#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" /* 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
{
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;
}
myinterrupt.c中,my_timer_handler 函数由系统时间中断来调用,每1000次执行一次判断,将my_need_sched置为1,只有my_need_sched为1时才能被调度(my_process中)。
my_schedule函数具体实现了进程的切换。声明了两个指针,prev和next,分别指向当前进程和下一个进程。进程切换时分两种情况,当next->state ==0 时,即下一个进程正在执行
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)
);
上边代码实现了进程的切换,第一行将当前的ebp保存压栈,然后将当前的esp存入内存中(prev->thread.sp);第三行将ebp指向要切换进程的esp;第四行保存当前的eip到pre->thread.ip;第五行将下一个进程的eip压栈;第六行ret实现将栈顶,即刚刚压栈的eip弹出,赋给eip,程序开始从此处执行(即要切换的进程),完成了进程切换。
当下一个进程状态不为0时,即表示还未执行,此时esp等与ebp,其余部分和第一种情况相同。
总结
个人理解,操作系统像一个不停移动的机械手臂,不断将指令所指示的内容从传送带(内存)上抓取进CPU,也将从CPU中出来的结果取回到传送带上,而某些指令如call,jmp等等则像是给机械手臂的命令,命令来了就可能切换到不同的传送带(内存不同区域)上,同时之前的传送带就停止(保存状态信息,ebp压栈等等)。总之就是一个流水的模型,但中间的调度却十分巧妙,堆栈的思想体会的淋漓尽致。
可能我的理解还不够深入,自己看了一遍都觉得没说明白,在以后还需要继续学习,加深理解。我的比喻来自之前玩的一个游戏,下边附上图,玩过之后才知道,合理的调度分配是有多重要!