基于mykernel完成多进程的简单内核

实验信息

学号后三位:010
原创作品转载请注明出处 https://github.com/mengning/linuxkernel/

实验过程

在实验环境中执行以下命令:

sudo apt-get install qemu # install QEMU
sudo ln -s /usr/bin/qemu-system-i386 /usr/bin/qemu
wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.9.4.tar.xz # download Linux Kernel 3.9.4 source code
wget https://raw.github.com/mengning/mykernel/master/mykernel_for_linux3.9.4sc.patch # download mykernel_for_linux3.9.4sc.patch
xz -d linux-3.9.4.tar.xz
tar -xvf linux-3.9.4.tar
cd linux-3.9.4
patch -p1 < …/mykernel_for_linux3.9.4sc.patch
make allnoconfig
make
qemu -kernel arch/x86/boot/bzImage
在这里插入图片描述
可以看到编译完成的内核已开始运行,my_start_kernel与my_timer_handler交替周期性执行

cd mykernel后,执行ls与vi编辑器命令,可以看到mymain.c与myinterrupt.c文件内容:
在这里插入图片描述
mymain.c中定义了一个my_start_kernel函数,是一个循环,不停的输出my_start_kernel here在这里插入图片描述
myinterrupt.c中,有一个被时钟中断周期调用的函数my_timer_handler,不断输出图中的>>>>>>>>my_timer_handler here<<<<<<<
因此mykernel实现的是不断周期性调用my_start_kernel函数与my_timer_handler,模拟进程间的切换

一个简单的时间片轮转多道程序

执行git clone https://github.com/mengning/mykernel命令
将github上老师提供的源码下载到本地,覆盖原来的mymain.c myinterrupt.c文件并新建mypcb.h文件。
重新编译内核
cd LinuxKernel/linux-3.9.4
执行下面的命令:
make allnoconfig
make
qemu -kernel arch/x86/boot/bzImage
在这里插入图片描述
重新编译后的内核实现了进程切换

实验代码分析

mypcb.h : 进程控制块PCB结构体定义
#define MAX_TASK_NUM        4	//代表最大进程数为10
#define KERNEL_STACK_SIZE   1024*2 # unsigned long	//代表每个进程堆栈的大小,应改为(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);

mypcb.h中宏定义语法存在问题,应当将# unsigned long改为(unsigned long)
pid 进程的唯一标识,用于区分进程
stack[KERNEL_STACK_SIZE] 进程的堆栈
thread 进程的ip(程序指针)和sp(栈顶指针)
task_entry 进程的入口
next指向下一个PCB,形成指针环链,从而不断循环切换进程。

mymain.c: 初始化各个进程并启动0号进程
#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);
        }     
    }
}

my_start_kernel() 是系统启动后的函数入口,首先进行初始化和启动0号进程,同时创建其它的进程PCB,以方便后面的调度。
在该系统中,每个进程都执行my_process() 函数,my_process()将会打印出当前进程的 id,方便观察进程的切换。

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;

/*
 * 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 */
    {        
    	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()函数模拟时间片轮转,该函数周期性地发出中断信号,也就是my_need_sched。my_start_kernel()初始化每个进程,执行my_process()

实验总结

操作系统有三个核心组成,缺一不可,分别是存储程序原理、堆栈以及中断,对于进程来说,唯一存在的标志就是PCB,操作系统通过对PCB该数据结构的修改进行对进程的控制。PCB中存储进程相关信息和运行信息,操作系统中首先有一个内核进程,然后有大量的中断处理程序,由内核在发生中断时进行对应的处理。

Linux操作系统通过fork()函数创建进程,首先将CPU正在运行的进程上下文保存在内核态堆栈中;然后加载PCB中指向的需创建的进程的上下文信息到相应的寄存器中,运行当前新建进程;运行完毕后根据系统的调度继续执行相应的进程。操作系统的中断机制由硬件实现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值