基于mykernel实现的时间片轮转调度代码分析
来源:互联网 发布:googlenet tensorflow 编辑:程序博客网 时间:2024/05/18 05:47
前言
作者:廖弘俊
原创作品转载请注明出处 《Linux内核分析》MOOC课程+http://mooc.study.163.com/course/USTC-1000029000
本文为《Linux内核分析》MOOC课程 第二周作业
直接按照作业说明在本地搭建好本课的环境,然后将mypcb.h 等三个文件复制过来重新编译。
下面是效果:
好了,下面是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);
#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);
这段代码很简单,主要是PCB的定义,PCB 里面包含了进程的各种信息而且各个进程之间用(循环)链表存储
下面是 mymian.c 文件中的函数
void __init my_start_kernel(void) //此函数相当于普通程序的main函数
{
int pid = 0;
int i;
/* Initialize process 0 (初始化0号进程)*/
{
int pid = 0;
int i;
/* Initialize process 0 (初始化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];
}
//从这里开始启动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*/
);
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];
}
//从这里开始启动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*/
);
//前一篇文章已分析过汇编代码,这里就不在步步分析了 红色代码部分的一二句将ebp,esp指向0号进程的栈
//3,4句是跳至0号进程执行
}
在此次实验中,为了简化实验所以所有的进程都执行下面的函数
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);
}}
}
}
{
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);
}}
}
}
有一个中断程序
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; //当my_need_sched == 1时,进程(void my_process(void))会调用函数my_schedule()进行进程切换
}
time_count ++ ;
#endif
return;
}
{
#if 1
if(time_count%1000 == 0 && my_need_sched != 1) //每隔一段时间执行下面的语句
{
printk(KERN_NOTICE ">>>my_timer_handler here<<<\n");
my_need_sched = 1; //当my_need_sched == 1时,进程(void my_process(void))会调用函数my_schedule()进行进程切换
}
time_count ++ ;
#endif
return;
}
下面是进程切换的函数的主要部分
if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */
{
/* 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)
);//上面的汇编代码 1,2句保存当前进程的栈顶栈底,3指向下一个进程的栈,4保存当前的eip地址,5,6指向下一个进程执行
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)
);//上面的汇编代码 1,2句保存当前进程的栈顶栈底,3指向下一个进程的栈,4保存当前的eip地址,5,6指向下一个进程执行
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)
);//此处与上面大致相同,但注意这里是切换至一个从未执行过的进程执行
}
{
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)
);//此处与上面大致相同,但注意这里是切换至一个从未执行过的进程执行
}
上面这段代码是很关键的,主要写了如何切换进程
通过这次学习,让我知道了单cpu一次只能做一件事,但通过巧妙的方法(进程调度)就可以让用户以为它能同时做多件事。
0 0
- 基于mykernel实现的时间片轮转调度代码分析
- 基于mykernel的时间片轮转调度的实现
- 基于mykernel的一个简单的时间片轮转多道程序内核代码分析
- 时间片轮转调度算法的C语言模拟实现
- 基于时间片轮转多道程序内核代码分析操作系统的工作过程
- 简单的时间片轮转多道程序内核代码分析
- 通过内嵌汇编实现模拟时间片轮转多道程序的内核代码分析
- 基于时间片的程序调度分析
- 操作系统 进程调度 基于时间片轮转(C实现 Visual Stdio 2005)
- 基于优先权的时间片轮转算法
- 时间片轮转调度算法
- 时间片轮转调度算法
- 时间片轮转调度算法
- 时间片轮转调度算法
- 07-时间片轮转调度
- VxWorks时间片轮转调度
- 基于一个简单内核mykernel的Linux内核时间调度的分析
- 【操作系统】学习笔记(三)基于时间片轮转的进程调度算法
- Python线程指南
- Sorting - bubble sort
- Android异步更新UI-线程池-Future-Handler实例分析
- FFmpeg源代码简单分析:makefile
- Android应用开发相关下载资源(2014/12/14更新)
- 基于mykernel实现的时间片轮转调度代码分析
- Android工程文件不能自动生成R文件
- Number of 1 bits(算法练习)
- Palindrome Number
- 《大型网站系统与JAVA中间件实践》读书笔记
- 3D演示
- C#打开或者创建一个文件,然后向其末尾写入数据的方法
- 私网安装Ambari实现Hadoop、Hbase等自动化安装(离线 本地源)
- HotSpot运行时概览#1