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

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

实验

实验环境

Windows下VMware虚拟机 ubuntu14(32位)系统
系统信息

实验目的

  1. 完成一个简单的时间片轮转多道程序内核代码,参考代码见mykernel版本库;
  2. 分析该精简内核的源代码;
  3. 理解操作系统是如何工作的;
  4. 了解进程的启动和进程的切换机制;

实验内容

1.编译内核

使用下列命令编译mykernel内核

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 #从qemu窗口中您可以看到my_start_kernel在执行,同时my_timer_handler时钟中断处理程序周期性执行。

实验结果如图所示:
mykernel初始内核查看mykernel代码内容:
mykernel初始文件夹其中mymain.c 的主要内容如下:
mymain.cmymain.c中是一个while循环,每次 i%100000 = 0 时,输出“my_start_kernel here",见上图qemu中。

再查看myinterrupt.c文件:
myinterrupt.cmy_timer_handler被时间中断调用输出">>>>>>>>>>>>>>my_timer_handler here<<<<<<<<<<<<",见上图qemu。
内核启动后,系统调用my_start_kernel函数,并通过时钟中断周期调用my_timer_handler函数。

2.修改mykernel源码

实验源码参考https://github.com/mengning/mykernel,感谢孟老师的帮助。
修改了mymain.c和myinterrupt.c文件,并添加mypcb.h文件定义相关结构体。

  1. mypcb.h 部分代码
struct Thread {
    unsigned long	ip;//point to cpu run address
    unsigned long	sp;//point to the thread stack's top address
    //todo add other attrubte of system thread
};

定义了线程ip,sp用于保存现场。

//PCB Struct
typedef struct PCB{
    int pid; // pcb id 
    volatile long state;	/* -1 unrunnable, 0 runnable, >0 stopped */
    char stack[KERNEL_STACK_SIZE];// each pcb stack size is 1024*8
    /* CPU-specific state of this task */
    struct Thread thread;
    unsigned long	task_entry;//the task execute entry memory address
    struct PCB *next;//pcb is a circular linked list
    unsigned long priority;// task priority 
    //todo add other attrubte of process control block
}tPCB;

定义PCB进程控制块,进程状态信息(-1表示不能运行),进程堆栈大小,进程入口地址,下一个进程控制块的程序入口,形成进程的可循环链表。增加了进程优先级。

  1. myinterrupt.c 部分代码
void my_timer_handler(void)
{
#if 1
    // make sure need schedule after system circle 2000 times.
    if(time_count%2000 == 0 && my_need_sched != 1)
    {
        my_need_sched = 1;
	//time_count=0;
    }
    time_count ++ ;
#endif
    return;
}

时间中断程序,模拟时间片轮转。

	void my_schedule(void)
	{
		    tPCB * next;
		    tPCB * prev;
		    // if there no task running or only a task ,it shouldn't need schedule
	    if(my_current_task == NULL
	        || my_current_task->next == NULL)
	    {
		printk(KERN_NOTICE "                time out!!!,but no more than 2 task,need not schedule\n");
	     return;
	    }
	    /* schedule */
	    next = get_next();
	    prev = my_current_task;
	    printk(KERN_NOTICE "                the next task is %d priority is %u\n",next->pid,next->priority);
	    if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */
	    {//save current scene
	     /* 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;//switch to the next task
	    printk(KERN_NOTICE "                switch from %d process to %d process\n                >>>process %d running!!!<<<\n\n",prev->pid,next->pid,next->pid);
	  }
	    else
	    {
	        next->state = 0;
	        my_current_task = next;
	    printk(KERN_NOTICE "                switch from %d process to %d process\n                >>>process %d running!!!<<<\n\n\n",prev->pid,next->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;	
	}//end of my_schedule

进程调度函数,完成进程的切换。
主要通过对堆栈和指令寄存器的压栈再出栈,来完成上下文的切换,实现进程调度。
pushl %%ebp 将当前栈顶压栈
movl %%esp,%0 将esp保存至prev->thread.sp
movl %2,%%esp 将next->thread.sp赋值给esp
movl %2,%%ebp 将next->thread.sp赋值给ebp,重新设置栈顶
movl $1f,%1 将当前ip保存至prev->thread.ip
pushl %3 将下一个进程的ip压入栈中
ret 将ip弹出,赋值给eip,使系统跳转至下一个进程执行。
经过上述操作后,当前进程的ebp,esp,eip信息被保存。将下一个进程的ebp,esp和ip信息加载到系统中,跳转到下一个进程执行。完成进程的切换。

  1. mymian.c 部分代码
void __init my_start_kernel(void)
{
    int pid = 0;
    /* Initialize process 0*/
    task[pid].pid = pid;
    task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped */
    // set task 0 execute entry address to my_process
    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(pid=1;pid<MAX_TASK_NUM;pid++)
    {
        memcpy(&task[pid],&task[0],sizeof(tPCB));
        task[pid].pid = pid;
        task[pid].state = -1;
        task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1];
	task[pid].priority=get_rand(PRIORITY_MAX);//each time all tasks get a random priority
    }
	task[MAX_TASK_NUM-1].next=&task[0];
    printk(KERN_NOTICE "\n\n\n\n\n\n                system begin :>>>process 0 running!!!<<<\n\n");
    /* 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 */0号进程的sp的值赋值给esp寄存器
     "pushl %1\n\t" /* push ebp */0号进程的sp值压栈
     "pushl %0\n\t" /* push task[pid].thread.ip */0号进程的ip值压栈
     "ret\n\t" /* pop task[pid].thread.ip to eip */通过ret指令将eip的值放入eip寄存器中
     "popl %%ebp\n\t"
     :
     : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)	/* input c or d mean %ecx/%edx*/
);
}

内核启动初始函数,启动0号进程

void my_process(void)
{
    int i = 0;
    while(1)
    {
        i++;
        if(i%10000000 == 0)
        {
	  
            if(my_need_sched == 1)
            {
                my_need_sched = 0;
		sand_priority();
	   	my_schedule();  
		
	   }
        }
    }
}//end of my_process

while循环实现进程的时间片轮转调度。

3. 重新编译内核运行

mykern改
运行结果如图所示。

实验总结

操作系统主要的任务就是管理和调度进程,要完成这个任务,需要维护每个进程的进程描述和进程间的关系(调度关系)。
内核是操作系统功能的具体实现,主要有两个部分组成。内核线程和中断处理程序(进程调度)。
进程调度:将先前CPU正在运行的进程的进程上下文保存在内核态堆栈中,包括有eip,esp,ebp,cs等寄存器的数据;然后加载创建的进程的上下文信息到相应的寄存器中,运行当前新建进程;运行完毕后根据系统的调度继续执行相应的进程。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值