学号: 363

原创作品,转载请注明出处。
本实验资源来源: https://github.com/mengning/linuxkernel/

一、 实验环境配置

本次实验在实验楼完成:

在实验楼的终端下输入下面命令:

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

可查看运行结果:

关闭qemu窗口,进入mykernel文件夹,可以查看mymain.c和myinterrupt.c文件。

mymain.c的代码不断循环的去执行,周期性的产生时钟中断信号,去执行myinterrupt.c的代码。

二、实现时间片轮转多道程序

将mymain.c,myinterrupt.c,mypcb.h三个文件复制替换到mykernel文件夹下。

运行如下:

可以看到进程1切换到了进程2。

三、时间片轮转多道程序的代码分析

mypcb.h

/**  linux/mykernel/mypcb.h**  Kernel internal PCB types**  Copyright (C) 2013  Mengning**/#define MAX_TASK_NUM        4
#define KERNEL_STACK_SIZE   1024*2 # unsigned long
/* 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);

可以看到最大进程数定义为四个,程序控制块PCB中定义了pid,状态statue,线程thread,进程入口函数task_entry等.

mymain.c文件

/**  linux/mykernel/mymain.c**  Kernel internal my_start_kernel**  Copyright (C) 2013  Mengning**/
#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;/* 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].stack[KERNEL_STACK_SIZE-1] - 1) = (unsigned long)&task[i].stack[KERNEL_STACK_SIZE-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 */: : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)    /* input c or d mean %ecx/%edx*/);
} int i = 0;void my_process(void)
{    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 __init my_start_kernel(void)这个函数fork了4个新进程,把新fork的进程加入到进程链表在这个文件中。
汇编过程如下:
(1)将0号进程的esp的值赋给ESP寄存器
(2)将0号进程的esp的值压栈(此时堆栈状态为进程0的堆栈)
(3)将0号进程的eip的值压栈
(4)通过ret指令,让栈顶的eip的值出栈到EIP寄存器中(间接改变EIP寄存器的值),完成进程0的启动

myinterupt.c

/**  linux/mykernel/myinterrupt.c**  Kernel internal my_timer_handler**  Copyright (C) 2013  Mengning**/
#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 1if(time_count%1000 == 0 && my_need_sched != 1){printk(KERN_NOTICE ">>>my_timer_handler here<<<\n");my_need_sched = 1;} time_count ++ ;
#endifreturn;
}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;
}

通过my_time_handler()函数定时地不断向cpu发出中断,从而实现了时间片轮转。每调用1000次,就去将全局变量my_need_sched的值修改为1,通知正在执行的进程执行调度程序my_schedule。从而在my_schedule函数中完成进程的不断切换。

四、总结

(1)进程和中断在操作系统是是非常重要的两个部分,需要熟练掌握。
(2)EIP寄存器储存着当前执行的代码,可以通过更改EIP寄存器的值来更改当前执行的代码,从而实现进程切换。出于安全考虑,EIP寄存器的值不能被直接改变,但可以通过压栈+ret指令来间接改变。
(3)进程在执行过程中,当时间片用完之后需要进程切换时,需要保存当前的执行上下文环境,下次被调度的时候,需要回复进程的上下文环境。

转载于:https://www.cnblogs.com/xiguas/p/10519273.html

基于mykernel的时间片轮转调度相关推荐

  1. linux操作系统分析实验—基于mykernel的时间片轮转多道程序实现与分析

    linux操作系统分析实验-基于mykernel的时间片轮转多道程序实现与分析 学号384 原创作业转载请注明出处+中国科学技术大学孟宁老师的Linux操作系统分析 https://github.co ...

  2. linux实验三:基于mykernel 2.0编写一个简单的操作系统内核

    实验内容 按照https://github.com/mengning/mykernel的说明配置mykernel 2.0,熟悉Linux内核的编译:基于mykernel 2.0编写一个操作系统内核,参 ...

  3. java写linux内核,基于mykernel 2.0编写一个操作系统内核

    一.实验要求 1.按照https://github.com/mengning/mykernel 的说明配置mykernel 2.0,熟悉Linux内核的编译: 2.基于mykernel 2.0编写一个 ...

  4. Boost:基于Boost的优先调度器程序

    Boost:基于Boost的优先调度器程序 实现功能 C++实现代码 实现功能 boost::asio模块,基于Boost的优先调度器程序 C++实现代码 #include <boost/asi ...

  5. python dag调度系统开发_基于机器学习的DAG调度平台

    什么是DAG? 有向无环图 树形结构:除根节点,每个节点有且仅有一个上级节点,下级节点不限.根节点没有上级节点. 图结构:每个节点上级.下级节点数不限. DAG调度平台的定义及场景 任务调度是在各行各 ...

  6. 操作系统实验(二):进程调度(c实现优先权调度和时间片轮转调度)

    一.[实验目的] ①理解有关进程控制块.进程队列的概念. ②掌握进程优先权调度算法和时间片轮转调度算法的处理逻辑. 二.[实验内容] ①设计进程控制块PCB的结构,分别适用于优先权调度算法和时间片轮转 ...

  7. Linux 学习笔记(八):时间片轮转调度

    看这篇文章前可以先了解一下时间片:Linux 学习笔记(七):时间片_Amentos的博客-CSDN博客 一.基本概念 时间片轮转调度算法(Round-Robin,RR)主要用于分时操作系统中的进程调 ...

  8. 基于SSH开发物流仓储调度系统 课程设计 大作业 毕业设计

    基于SSH开发物流仓储调度系统(大作业/毕业设计) 开发环境: Windows操作系统 开发工具:Myeclipse+Jdk+Tomcat+MYSQL数据库 运行效果图:  基于SSH开发物流仓储调度 ...

  9. eclipse spring boot项目搭建_基于Spring-boot的kettle调度项目

    介绍 基于Spring-boot的kettle调度项目,参考了zhaxiaodong9860的代码并引用了其中的页面管理,后台代码自行参考了API进行工具化编写,方便使用 在原代码的基础上加入以下功能 ...

最新文章

  1. ZooKeeper的API操作(二)(通俗易懂)
  2. 时讯无线为你提供快速的上网设备
  3. php滑动换视频,php工具类之【视频变换类】
  4. 装个Redmine真是麻烦啊
  5. 深入理解计算机系统(1.3)---金字塔形的存储设备、操作系统的抽象概念
  6. RocketMQ:NameServer架构设计以及启动关闭流程源码分析
  7. Spring Boot 使用 AOP 防止重复提交
  8. app配置智能硬件的解决方案
  9. sourcetree 卡顿_哈曼卡顿Harman Kardon音响开奖!
  10. Target runtime com.genuitec.runtime.generic.jee60 is not defined.报错解决
  11. RuntimeError: view size is not compatible with input tensor‘s size and stride
  12. 【VScode】优雅地将代码打印为 PDF
  13. 多文档文本编辑器(Qt)
  14. 谷歌推出人肉搜索引擎
  15. ddt数据驱动常见的用法【多测师_王sir】
  16. 微信公众号粉丝、文章迁移流程及方法
  17. 计算机存储盘设置密码,如何在USB驱动器上设置密码并教您如何设置
  18. 下列哪项不属于Html5中input,以下哪项不属于 Html5 中 input 标签新增的输入类型?...
  19. zoom使用教程_如何使用Zoom在线玩Jackbox游戏
  20. HDU 1874 畅通工程续 Floyd

热门文章

  1. [译] 通过官网 Go 语言学习笔记 | How to Write Go Code
  2. 洛谷——P4053 [JSOI2007]建筑抢修
  3. JSP_tomcat_mysql_注冊验证用户;
  4. 在数组中寻找出现次数超过数组长度一半的数
  5. caffe学习日记--lesson4:windows下caffe DEMO (mnist and cifar10)
  6. CRLF对GIT DIFF的影响
  7. (01)VTK读取OBJ格式模型
  8. sqoop增量导入hive_Sqoop 增量导MySQL数据 至Hive
  9. 樊登36个问题建立亲密关系_36个问题建立亲密关系
  10. php简介的编辑器,推荐几款功能强大的PHP编辑器