实验:mykernel时间片轮转多道程序内核

进入实验楼实验,在终端中分别输入以下命令

cd LinuxKernel/linux-3.9.4
rm -rf mykernel
patch -p1 < ../mykernel_for_linux3.9.4sc.patch //打补丁
make allnoconfig
make
qemu -kernel arch/x86/boot/bzImage

make过程如下图:

https://dn-simplecloud.shiyanlou.com/8449831540547871321-wm

https://dn-simplecloud.shiyanlou.com/8449831540548661907-wm

mykernel时间片轮转代码分析

这里主要分析上面实验中改写的三个文件,其作用简述如下,

mypcb.h : 进程控制块PCB结构体定义。

mymain.c: 初始化各个进程并启动0号进程。

myinterrupt.c:时钟中断处理和进程调度算法。

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;
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);
在这个文件里,定义了 Thread 结构体,用于存储当前进程中正在执行的线程的ip和sp
这里还有一个函数的声明 my_schedule,它的实现在my_interrupt.c中,在mymain.c中的各个进程函数会根据一个全局变量的状态来决定是否调用它,从而实现主动调度。

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; 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]; 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];
} 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);
}
}
}
这里的函数 my_start_kernel 是系统启动后,最先调用的函数,在这个函数里完成了0号进程的初始化和启动,并创建了其它的进程PCB,以方便后面的调度。在模拟系统里,每个进程的函数代码都是一样的,即 my_process 函数,my_process 在执行的时候,会打印出当前进程的 id,从而使得我们能够看到当前哪个进程正在执行。
另外,在 my_process 也会检查一个全局标志变量 my_need_sched,一旦发现其值为 1 ,就调用 my_schedule 完成进程的调度。

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; void my_timer_handler(void)
{
#if 1
if(time_count%100 == 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);
/* 切换进程 */
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;
}
通过本讲的学习和实验,我们知道操作系统的核心功能就是:进程调度和中断机制,通过与硬件的配合实现多任务处理,再加上上层应用软件的支持,最终变成可以使用户可以很容易操作的计算机系统。通过这个实验我们可以知道,mykernel系统启动后,调用my_start_kernel函数和my_timer_handler函数,完成系统进程的初始化和进程的轮转调度。

最新文章

  1. 修改.net mvc中前端验证信息的显示方式
  2. css权威指南学习笔记 —— css选择器
  3. dos学习
  4. ubuntu安装虚拟磁带库mhvtl
  5. TIJ——Chapter Eight:Polymorphism
  6. 夺命雷公狗----Git---7---GitHub当仓库本地使用(完)
  7. JavaScript数据类型之隐式类型转换
  8. iOS开发--关于TableViewCell的可视化设置细节
  9. laravel_5《数据库迁移》
  10. SQL保留关键字不能用作表名
  11. I Count Two Three---hdu5878(打表+二分)
  12. NSArray 迭代
  13. IIS配置网站(WebServices),局域网都能访问
  14. dos下循环复制一张图片的bat
  15. [YZOJ1579]&amp;&amp;[BZOJ2450]arr
  16. android的fragment基本介绍
  17. Android自定义XML属性
  18. line-height属性总结
  19. shell脚本头,#!/bin/sh与#!/bin/bash的区别.
  20. C#如何在生成文件夹或者文件时候自动重命名

热门文章

  1. 雷林鹏分享:jQuery EasyUI 数据网格 - 列运算
  2. 压力测试+接口测试(工具jmeter)
  3. Flask框架整理
  4. mybatis-generator自动生成代码工具
  5. vue配置编译本地打开dist/index.html文件
  6. 高级FTP服务器开发
  7. css预处理器--sass学习($变量名)
  8. centos 7 安装iptables防火墙
  9. 数据的双向绑定 Angular JS之开端篇
  10. with&amp;as上下文管理协议