基于mykernel完成多进程的简单内核
一:背景
实现一个时间片轮转多道程序内核代码,实验在Ubuntu14.04下进行
学号:186
参考资料: https://github.com/mengning/linuxkernel/
二:实现环境
安装qemu工具
当前的Linux内核打补丁
补丁来源于中科大孟老师的资源
编译内核源码
查看代码执行过程
三:一个时间轮转多道程序
源码
源码来自孟老师的GIthub:https://github.com/mengning/mykernel
主要有三个文件mypcb.h,mymain.c,myinterrupt.c
将三个文件替换原来的mymain.c,myinterrupt.c文件,重新编译内核
mypcb.h
关键信息:设置了最大进程数,堆栈大小及相关信息
代码如下:
#define MAX_TASK_NUM 4
#define KERNEL_STACK_SIZE 10242 # 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);
mymain.c
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);
}
}
}
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*/
);
关键代码
%1指的是task[pid].thread.sp,%0是task[pid].thread.ip
“movl %1,%%esp\n\t” 将原堆栈的栈顶放到sp寄存器中
“pushl %1\n\t” 将ep寄存器的值存入栈
“pushl %0\n\t” 将当前进程ip值入栈
myinterrupt.c
关键信息:通过my_time_handler()函数定时地不断向cpu发出中断,从而实现了时间片轮转。
my_schedule是整个程序的关键,它实现的是进程的上下文切换.将前一个进程的相关信息保存,并读入下一个进程的相关信息,从而完成进程的切换。
#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;
四:实验结果和总结
实验结果
如图所示,内核模拟进程的运行和切换如下:
实验总结
这一个实验,加深了我对内核中多进程调度的理解:OS初始化时只有一个PROCESS 0进程,之后的所有进程都由该进程fork而来。
实验中加深了对于计算机堆栈的理解,并且丰富了操作Ubuntu系统的熟练度。
谢谢观看
谢谢观看,2019年3月12日记