基于mykernel的一个简单的时间片轮转多道程序内核代码分析

学号023作品

本实验资源来源: 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窗口中我们可以看到一个运行中的简易操作系统,并输出字符串:

>>>>>my_timer_handler here<<<<<

和my_start_kernel here

运行截图如下:

在此处分析一下mymain.c和myinterrupt.c

mymain.c

在my_start_kernel函数中有一个循环,不停地输出my_start_kernel here

通过时钟中断周期函数调用my_timer_handler函数,可输出>>>>>my_timer_handler here<<<<<的字符串

总而言之,在mykernel启动之后,系统调用my_start_kernel函数,并利用时钟中断周期函数周期性调用my_timer_handler函数。

二、一个简单的时间片轮转多道程序分析

1、从https://github.com/mengning/mykernel/tree/master/mykernel-1.1上获取mymain.c、myinterrupt.c、pcb.c三个文件

2、在mykernel文件下覆盖mymain.c、myinterrupt.c,并新建pcb.c文件

3、cd linux-3.9.4 执行下列命令,重新编译内核

make allnoconfig

make

qemu -kernel arch/x86/boot/bzImage

运行截图如下:

可见系统进程在切换

以下进行代码分析

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

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

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

mypcb.h:

 1 /*    mykernel--Simple simulation of the linux OS  process schedule
 2  *
 3  *  linux/mykernel/mypcb.h
 4  *
 5  *  Kernel internal my_timer_handler
 6  *
 7  *  Copyright (C) 2013  Mengning
 8  *
 9  *  Modified 2014 Yunquan Zhang  <zhangyunquan@hotmail.com>
10  *
11  *
12  *  You can redistribute or modify this program under the terms
13  *  of the GNU General Public License as published by
14  *  the Free Software Foundation.
15  *
16  * You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #define MAX_TASK_NUM 10 // max num of task in system
21 #define KERNEL_STACK_SIZE 1024*8
22 #define PRIORITY_MAX 30 //priority range from 0 to 30
23
24 /* CPU-specific state of this task */
25 struct Thread {
26     unsigned long    ip;//point to cpu run address
27     unsigned long    sp;//point to the thread stack‘s top address
28     //todo add other attrubte of system thread
29 };
30 //PCB Struct
31 typedef struct PCB{
32     int pid; // pcb id
33     volatile long state;    /* -1 unrunnable, 0 runnable, >0 stopped */
34     char stack[KERNEL_STACK_SIZE];// each pcb stack size is 1024*8
35     /* CPU-specific state of this task */
36     struct Thread thread;
37     unsigned long    task_entry;//the task execute entry memory address
38     struct PCB *next;//pcb is a circular linked list
39     unsigned long priority;// task priority ////////
40     //todo add other attrubte of process control block
41 }tPCB;
42
43 //void my_schedule(int pid);
44 void my_schedule(void);

在这个文件里,定义了 Thread 结构体,用于存储当前进程中正在执行的线程的ip和sp,PCB结构体中的各个字段含义如下

pid:进程号

state:进程状态,在模拟系统中,所有进程控制块信息都会被创建出来,其初始化值就是-1,如果被调度运行起来,其值就会变成0

stack:进程使用的堆栈

thread:当前正在执行的线程信息

task_entry:进程入口函数

next:指向下一个PCB,模拟系统中所有的PCB是以链表的形式组织起来的。

my_schedule声明,在myinterrupt.c中实现,在mymain.c中的各个进程函数会根据一个全局变量的状态来决定是否调用它,从而实现主动调度。

mymain.c

 1     /*
 2      *  linux/mykernel/mymain.c
 3      *
 4      *  Kernel internal my_start_kernel
 5      *
 6      *  Copyright (C) 2013  Mengning
 7      *
 8      */
 9     #include <linux/types.h>
10     #include <linux/string.h>
11     #include <linux/ctype.h>
12     #include <linux/tty.h>
13     #include <linux/vmalloc.h>
14
15
16     #include "mypcb.h"
17
18     tPCB task[MAX_TASK_NUM];
19     tPCB * my_current_task = NULL;
20     volatile int my_need_sched = 0;
21
22     void my_process(void);
23
24
25     void __init my_start_kernel(void)
26     {
27         int pid = 0;
28         int i;
29         /* Initialize process 0*/
30         task[pid].pid = pid;
31         task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped */
32         task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process;
33         task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1];
34         task[pid].next = &task[pid];
35         /*fork more process */
36         for(i=1;i<MAX_TASK_NUM;i++)
37         {
38             memcpy(&task[i],&task[0],sizeof(tPCB));
39             task[i].pid = i;
40             task[i].state = -1;
41             task[i].thread.sp = (unsigned long)&task[i].stack[KERNEL_STACK_SIZE-1];
42             task[i].next = task[i-1].next;
43             task[i-1].next = &task[i];
44         }
45         /* start process 0 by task[0] */
46         pid = 0;
47         my_current_task = &task[pid];
48         asm volatile(
49             "movl %1,%%esp\n\t"     /* set task[pid].thread.sp to esp */
50             "pushl %1\n\t"             /* push ebp */
51             "pushl %0\n\t"             /* push task[pid].thread.ip */
52             "ret\n\t"                 /* pop task[pid].thread.ip to eip */
53             "popl %%ebp\n\t"
54             :
55             : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)    /* input c or d mean %ecx/%edx*/
56         );
57     }
58     void my_process(void)
59     {
60         int i = 0;
61         while(1)
62         {
63             i++;
64             if(i%10000000 == 0)
65             {
66                 printk(KERN_NOTICE "this is process %d -\n",my_current_task->pid);
67                 if(my_need_sched == 1)
68                 {
69                     my_need_sched = 0;
70                     my_schedule();
71                 }
72                 printk(KERN_NOTICE "this is process %d +\n",my_current_task->pid);
73             }
74         }
75     }

my_start_kernel 是系统启动后最先调用的函数,在这个函数里完成了0号进程的初始化和启动,并创建了其它的进程PCB,以方便后面的调度。在模拟系统里,每个进程的函数代码都是一样的,即 my_process 函数,my_process 在执行的时候,会打印出当前进程的 id,从而使得我们能够看到当前哪个进程正在执行。

myinterrupt.c

 1 /*
 2  *  linux/mykernel/myinterrupt.c
 3  *
 4  *  Kernel internal my_timer_handler
 5  *
 6  *  Copyright (C) 2013  Mengning
 7  *
 8  */
 9 #include <linux/types.h>
10 #include <linux/string.h>
11 #include <linux/ctype.h>
12 #include <linux/tty.h>
13 #include <linux/vmalloc.h>
14
15 #include "mypcb.h"
16
17 extern tPCB task[MAX_TASK_NUM];
18 extern tPCB * my_current_task;
19 extern volatile int my_need_sched;
20 volatile int time_count = 0;
21
22 /*
23  * Called by timer interrupt.
24  * it runs in the name of current running process,
25  * so it use kernel stack of current running process
26  */
27 void my_timer_handler(void)
28 {
29 #if 1
30     if(time_count%1000 == 0 && my_need_sched != 1)
31     {
32         printk(KERN_NOTICE ">>>my_timer_handler here<<<\n");
33         my_need_sched = 1;
34     }
35     time_count ++ ;
36 #endif
37     return;
38 }
39
40 void my_schedule(void)
41 {
42     tPCB * next;
43     tPCB * prev;
44
45     if(my_current_task == NULL
46         || my_current_task->next == NULL)
47     {
48         return;
49     }
50     printk(KERN_NOTICE ">>>my_schedule<<<\n");
51     /* schedule */
52     next = my_current_task->next;
53     prev = my_current_task;
54     if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */
55     {
56         /* switch to next process */
57         asm volatile(
58             "pushl %%ebp\n\t"         /* save ebp */
59             "movl %%esp,%0\n\t"     /* save esp */
60             "movl %2,%%esp\n\t"     /* restore  esp */
61             "movl $1f,%1\n\t"       /* save eip */
62             "pushl %3\n\t"
63             "ret\n\t"                 /* restore  eip */
64             "1:\t"                  /* next process start here */
65             "popl %%ebp\n\t"
66             : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
67             : "m" (next->thread.sp),"m" (next->thread.ip)
68         );
69         my_current_task = next;
70         printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);
71     }
72     else
73     {
74         next->state = 0;
75         my_current_task = next;
76         printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);
77         /* switch to new process */
78         asm volatile(
79             "pushl %%ebp\n\t"         /* save ebp */
80             "movl %%esp,%0\n\t"     /* save esp */
81             "movl %2,%%esp\n\t"     /* restore  esp */
82             "movl %2,%%ebp\n\t"     /* restore  ebp */
83             "movl $1f,%1\n\t"       /* save eip */
84             "pushl %3\n\t"
85             "ret\n\t"                 /* restore  eip */
86             : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
87             : "m" (next->thread.sp),"m" (next->thread.ip)
88         );
89     }
90     return;
91 }

my_time_handler()函数,是个时间片轮转,周期性地发出中断信号,也就是my_need_sched。my_start_kernel()完成每个进程初始化,每个进程的任务都是my_process(),由于这个函数中有个无限循环,任务永远不会结束;并且启动了0号进程。任务需要调度时根据任务链表顺序进行调度。

实验总结

通过本次实验,对于进程调度和中断有了更加深刻的了解。总的来说,操作系统的工作需要进行进程的启动与切换,切换时需要保存上下文,切换的程序完成后需要恢复上下文,进程切换通过时钟中断进行控制。

 

原文地址:https://www.cnblogs.com/LucasChang/p/10518364.html

时间: 2024-07-30 05:13:39

基于mykernel的一个简单的时间片轮转多道程序内核代码分析的相关文章

一个简单的时间片轮转多道程序内核代码分析 (学号后三位418)

一.基于mykernel的基本Linux内核分析 1.我们按照老师在github上给出的步骤在实验楼上启动最高小内核,可以看到如下现象 在窗口中我们可以看到一个内核以及运行起来了,比较简单的内核,只时不停的输出一些字符串,>>>>>>my_time_handler here<<<<<<<和my_start_kernel here和一些计数.这时因为我们并没有加入其他的代码,再次基础上我们可以加入我们主机要实现的功能. 在myin

对一个简单的时间片轮转多道程序内核代码的浅析

这周在网易云课堂上学习了<Linux内核分析>——操作系统是如何工作的.本周学习内容有利用 mykernel 实验模拟计算机平台和利用 mykernel 实验模拟计算机硬件平台两部分内容. 这是实验楼中 mykernel 平台运行的结果: 下面是一段一个简单的时间片轮转多道程序内核代码: 1 /* 2 * linux/mykernel/myinterrupt.c 3 * 4 * Kernel internal my_timer_handler 5 * 6 * Copyright (C) 201

Linux内核分析—完成一个简单的时间片轮转多道程序内核代码

---恢复内容开始--- 20135125陈智威 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” 实验要求: mykernel实验指导(操作系统是如何工作的) 运行并分析一个精简的操作系统内核,理解操作系统是如何工作的 使用实验楼的虚拟机打开shell cd LinuxKernel/linux-3.9.4 qemu -kernel arch/x86/boot/bzImage 然后cd

完成一个简单的时间片轮转多道程序内核代码(二)

完成一个简单的时间片轮转多道程序内核代码 重要汇编代码分析 asm volatile( "movl %1,%%esp\n\t" "pushl %1\n\t" "pushl %0\n\t" "ret\n\t" "popl %%ebp\n\t" : : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp) ); 保存恢复进

Linux内核分析:完成一个简单的时间片轮转多道程序内核代码

PS.贺邦   原创作品转载请注明出处  <Linux内核分析>MOOC课程    http://mooc.study.163.com/course/USTC-1000029000 1.mykernel实验指导(操作系统是如何工作的) 使用实验楼虚拟机打开shell输入下列代码 1 cd LinuxKernel/linux-3.9.4 2 qemu -kernel arch/x86/boot/bzImage 可以看到初始的内核运行情况如下: 内核不停的执行my_start_kernel(),每

完成一个简单的时间片轮转多道程序内核代码

王康 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 " 分别是1 存储程序计算机工作模型,cpu执行程序的基础流程: 2 函数调用堆栈:各种寄存器和存储主要是为了指令的传取值,通过eip,esp,eax,ebp和程序内存的分区,搭配push pop call return leave等一系列指令完成函数调用操作. 3 中断:多道批程序! 在复习一下上一讲的几个重要指令

第二周:一个简单的时间片轮转多道程序内核代码及分析

吕松鸿+ 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.函数调用堆栈 1. 计算机工作的三个法宝 存储程序计算机工作模型,计算机系统最最基础性的逻辑结构: 函数调用堆栈,高级语言得以运行的基础,只有机器语言和汇编语言的时候堆栈机制对于计算机来说并不那么重要,但有了高级语言及函数,堆栈成为了计算机的基础功能: enter pushl %ebp movl %esp,%ebp lea

lab2:完成一个简单的时间片轮转多道程序内核代码

李俊锋 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.实验目的 1.熟悉.理解Linux内核工作方式 2.尝试编写自己的内核 3.理解多进程时间片轮转的工作方式 二.实验步骤 1.编写时间片轮转程序. 2.运行程序 1 cd LinuxKernel/linux-3.9.4 2 qemu -kernel arch/x86/boot/bzImage 三.代码分析 3.1 myp

Linux内核设计第二周学习总结 完成一个简单的时间片轮转多道程序内核代码

陈巧然 原创作品 转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.使用实验楼的虚拟机, 观察只有一个死循环的mykernel与时钟中断的关系 步骤:cd LinuxKernel/linux-3.9.4 qemu -kernel arch/x86/boot/bzImage 执行效果如下图 Paste_Image.png 现在查看mymain.c: Paste_Image.png 再查看myin