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

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

学号293,转载请注明出处

本实验来源: https://github.com/mengning/linuxkernel/

一、mykernel

这是孟老师基于linux kernel 3.9.4 source code建立的开发操作系统内核平台。

源码

可以根据老师写的readme文件进行部署

也可以直接在实验楼课程中提供的实验环境进行实验

二 、实验分析

本实验采取在实验楼提供的环境中进行实验

首先可以通过以下命令,查看QEMU虚拟机

cd LinuxKernel/linux-3.9.4
qemu -kernel arch/x86/boot/bzImage

结果如下:

cd mykernel

可以看到在mymain.c中的 my_start_kernel函数中:

即为一个循环,不停的输出my_start_kernel here.

然后查看myinterrupt.c

类似也是一个my_timer_handler函数

输出

"\n>>>>>>>>>>>>>>>>>my_timer_handler here<<<<<<<<<<<<<<<<<<\n\n"

以上也就是我们看到的运行的qemu得到的结果,不停地输出这两个字符串。

三、实现一个简单的时间片轮转多道程序

可以使用mykernel中提供的示例代码,这里我们通过git来下载源代码文件

此时已经将clone下来的我们需要的文件移动到了Linkernel/linux3.9.4/mykernel中了

然后需要重新编译下工程。

make allnoconfig
make -j4
qemu -kernel arch/x86/boot/bzImage

结果如下:

四、源代码分析

主要是三个文件mypcb.h mymain.c myinterrupt.c

在mypcb.h中主要是进程控制块PCB的结构体定义

/*
 *  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);

Thread结构体,用于存储当前线程的ip,sp

pid是进程号,state 进程状态,stack进程使用的栈, thread当前的线程信息,task_entry进程入口函数,next:指向下一PCB(假设进程是以链表的形式连接起来的)

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

在mymain.c中,初始化各个进程并启动0号进程

/*
	 *  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].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];
		}
		/* 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 */
			"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 完成进程的调度。

0号线程的启动,采用了内联汇编代码完成,详细参见源码中的注释。

在myinterrupt.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 1
    if(time_count%1000 == 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 */
    {
    	/* 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)
    	); 
    	my_current_task = next; 
    	printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);   	
    }
    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;	
}

这里 my_timer_handler 函数会被内核周期性的调用,每调用1000次,就去将全局变量my_need_sched的值修改为1,通知正在执行的进程执行调度程序my_schedule。在my_schedule函数中,完成进程的切换。进程的切换分两种情况,一种情况是下一个进程没有被调度过,另外一种情况是下一个进程被调度过,可以通过下一个进程的state知道其状态。进程切换依然是通过内联汇编代码实现,无非是保存旧进程的eip和堆栈,将新进程的eip和堆栈的值存入对应的寄存器中,详见代码中的注释。

五、总结

在本次的学习和实验中,了解了操作系统中的进程的调度机制与其中断机制,了解一下C语言内嵌汇编的方法。

操作系统通过其对硬件的控制,合理分配计算机资源,实现多任务处理,使用户更加容易的操作计算机。

posted @ 2019-03-17 20:42  PCM  阅读(280)  评论(0编辑  收藏  举报