郑卓彬 + 原创作品转载请注明出处 + 《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000
前言:
该实验,主要是老师让我们理解内核中进程切换的原理。
内容:
代码解析:
mypch.h
#define MAX_TASK_NUM 4
#define KERNEL_STACK_SIZE 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);
解析:该头文件主要定义了如下两个结构
1、Thread : 线程,里面就定义了 ip、sp两个属性,分别用来存储线程切换时的eip和esp的值,在下回调度该进程时可以用该线程回推出进程的上下文。
2、PCB:进程管理块,里面存储了状态变量、进程栈、管理的进程、下一个调度的pcb的指针。
mymain.c
#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;
/* 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
{
memcpy(&task[i],&task[0],sizeof(tPCB));
task[i].pid = i;
task[i].thread.sp = (unsigned long)&task[i].stack[KERNEL_STACK_SIZE-1];
*((unsigned long *)task[i].thread.sp - 1) = task[i].thread.sp;
task[i].thread.sp -= 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);
}
}
}
解析:
1、__init my_start_kernel 方法是内核启动后会执行的方法:
在方法里我们定义了4个线程,并使他们形成一个环,内核一开始就调用进程0.之后进程0执行my_process方法,
2、my_process 方法是每个进程会执行的函数:
在该方法里,会一直反复循环,每循环100000000次,就会检查进程的状态,如果状态值等于1则会执行进程的调度,然后下一个进程进入执行。
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_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)
);
}
return;
}
解析:该调度,使用了汇编代码,在汇编代码中,将下个进程的sp和ip放入了cpu的esp和eip中,让cpu去该进程的ip取指,并将栈顶寄存器esp指向了该线程的栈顶。
运行结果:
总结:
进程的切换第一步:就是当前进程上下文,在本文中就是sp和ip值。
第二步:将下一个进程的上下文提取到cpu中。在本文就是将sp和ip的值分别取到esp和eip中。
第三步:cpu直接执行eip地址的指令。这样进程就完成了切换。
所以,操作系统就是一直运行着的,对进程进行调度,资源进行管理,文件管理的连接软件和硬件的程序。