码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-03-12 18:05:16      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:进程调度算法   efi   return   redis   span   重新编译   hotmail   时间片   inux   

学号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号进程。任务需要调度时根据任务链表顺序进行调度。

实验总结

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

 

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

标签:进程调度算法   efi   return   redis   span   重新编译   hotmail   时间片   inux   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!