实验截图
实验代码分析
mypcb.h
#define MAX_TASK_NUM 4 // 最大进程数
#define KERNEL_STACK_SIZE 1024*8 //内核堆栈大小
/* CPU-specific state of this task */
struct Thread {
unsigned long ip; //定义eip
unsigned long sp; //定义esp
};
typedef struct PCB{
int pid; //定义进程标识符(进程号)
volatile long state; //定义进程状态:-1不运行,0运行,>0停止
char stack[KERNEL_STACK_SIZE]; //定义内核堆栈
struct Thread thread; //定义一个Thread(eip,esp)
unsigned long task_entry; //定义进程入口
struct PCB *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]; //声明一个task数组
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; //初始化进程pid为0.
task[pid].state = 0; //初始化0号进程状态为运行
task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process; //初始化程序入口为,my_process
task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1]; //初始化堆栈栈顶,esp指向栈顶。
task[pid].next = &task[pid]; //初始时系统只有一个进程,所以自己指向自己。
/*创建更多的进程*/
for(i=1;i<MAX_TASK_NUM;i++)
{
memcpy(&task[i],&task[0],sizeof(tPCB)); //将0号进程的状态copy给i号进程
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];
}
/* 开始执行0号进程*/
pid = 0;
my_current_task = &task[pid];
asm volatile(
"movl %1,%%esp\n\t" /* 将task[pid].thread.sp赋给esp */
"pushl %1\n\t" /* 因为当前栈为空,所以esp=ebp,因此此步为push ebp*/
"pushl %0\n\t" /* 将当前eip压栈*/
"ret\n\t" /* pop task[pid].thread.ip to eip */
"popl %%ebp\n\t" //pop ebp
:
: "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)//循环1000万次判断是否需要调度
{
printk(KERN_NOTICE "this is process %d -\n",my_current_task->pid);
if(my_need_sched == 1)
{
my_need_sched = 0; //将进程调度标识符置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)//时钟终端1000次,且调度标识符不为1时输出提示,且将调度标识符变为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)//判断下一个进程是否为正在执行状态
{
/* switch to next process */
asm volatile(
"pushl %%ebp\n\t" //保存当前进程的ebp
"movl %%esp,%0\n\t" //将当前进程的esp保存在prev->thread.sp中
"movl %2,%%esp\n\t" //将下一个进程的esp(next->thread.sp)放在esp中
"movl $1f,%1\n\t" //将1:标志处的内存地址保存在prev->thread.ip中
"pushl %3\n\t" //将下一个进程的eip放入栈中
"ret\n\t" //释放下一个进程的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)
);
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;
}
总结
操作系统通过在某种时间片轮转算法下,通过中断和调度机制,不断切换处理各个进程已达到高效处理系统工作任务的目的。
Sawoom原创作品转载请注明出处
《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000
网友评论