Linux内核分析第三次作业

实验: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
	"     /* set task[pid].thread.sp to esp */
        "pushl %1
	"          /* push ebp */
        "pushl %0
	"          /* push task[pid].thread.ip */
        "ret
	"               /* pop task[pid].thread.ip to eip */
        "popl %%ebp
	"
        : 
        : "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 -
",my_current_task->pid);
            if(my_need_sched == 1)
            {
                my_need_sched = 0;
                my_schedule(); 
            }
            printk(KERN_NOTICE "this is process %d +
",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<<<
");
        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<<<
");
    /* 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<<<
",prev->pid,next->pid);  
        /* 切换进程 */
        asm volatile(   
            "pushl %%ebp
	"       /* save ebp */
            "movl %%esp,%0
	"     /* save esp */
            "movl %2,%%esp
	"     /* restore  esp */
            "movl $1f,%1
	"       /* save eip */  
            "pushl %3
	" 
            "ret
	"               /* restore  eip */
            "1:	"                  /* next process start here */
            "popl %%ebp
	"
            : "=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<<<
",prev->pid,next->pid);
        /* switch to new process */
        asm volatile(   
            "pushl %%ebp
	"       /* save ebp */
            "movl %%esp,%0
	"     /* save esp */
            "movl %2,%%esp
	"     /* restore  esp */
            "movl %2,%%ebp
	"     /* restore  ebp */
            "movl $1f,%1
	"       /* save eip */  
            "pushl %3
	" 
            "ret
	"               /* 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函数,完成系统进程的初始化和进程的轮转调度。    
原文地址:https://www.cnblogs.com/20189223cjt/p/9858205.html