陈康+原创作品转载请注明出处+本文章为《Linux内核分析》(MOOC课程http://mooc.study.163.com/course/USTC-1000029000 )的实验报告,主要探讨Linux内核是采用怎样的机制实现进程间的轮转,最终完成一个简单的时间片轮转多道程序内核代码,实现进程调度。
1 实验环境
实验主要实在Linux kernel 3.9.4源代码的基础上进行修改。使用qemu运行编译好的内核镜像,qemu的说明参见使用 QEMU 进行系统仿真。
2 实验过程
实验在实验楼对应课程上操作完成,编译及运行步骤如下:
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
运行结果如下图所示:
可以看到每隔固定的时间进程会切换到下一个,如此反复。
3 源代码及分析
修改的代码在
mykernel文件夹下,主要涉及三个文件
- mypcb.h 用来声明简单的线程和进程PCB结构的文件;
- mymain.c 用来初始化和启动进程,定义进程代码逻辑的文件;
- myinterupt.c 用来处理时间中断函数,定义进程切换过程的文件。
3.1 mypcb.h代码分析
mypcb.h代码如下:#define MAX_TASK_NUM 4
#define KERNEL_STACK_SIZE 1024*2
/* 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和PCB。
Thread中的ip代表进行执行的代码位置;sp代表进程执行的堆栈位置。
PCB中的pid代表进程号,该进程号独一无二的,代表该进程;state代表进程的状态(未运行、运行、停止);stack代表进程运行的堆栈空间;thread代表进程中执行运行任务的线程;task_entry代表进程的入口地址;next代表进程指向的下一个进程。
3.2 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;
/* 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].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是入口函数,最先执行该函数。主要作用是初始化各个进程的PCB结构,然后通过嵌入的汇编语句设置堆栈和eip,使内核执行PCB的id为0的进程的入口函数,使pid为0的进程开始执行。
my_process是示例进程的入口函数,进程执行时主要执行该函数。
3.3 myinterrupt.c代码分析
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_timer_handler是时钟中断执行函数,这里主要定时设置my_need_sched变量,该进程入口函数的进程调度。
my_schedule函数执行进程的调度,通过内嵌汇编语言,将当前进程的相关寄存器(esp ebp eip)保存起来,更改esp和eip寄存器使之能够执行下一个进程。
4 实验总结
同时该实验也说明了进程如何进行切换,怎样保存现场和恢复现场,使切换到该进程时,能够接着上次执行到的位置进行执行,并且恢复到切换前的上下文环境。