章强 + 原创作品转载请注明出处 + 《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000
实验截图
使用实验楼的虚拟机打开shell ,输入指令
1.cd LinuxKernel/linux-3.9.4
2.qemu -kernel arch/x86/boot/bzImage
然后cd mykernel 可以看到qemu窗口输出的内容的代码mymain.c和myinterrupt.c
mymain.c:内核从这里启动,从这个函数开始运行并无限循环
void __init my_start_kernel(void)
{
int i = 0;
while(1)
{
i++;
if(i%100000 == 0)
printk(KERN_NOTICE "my_start_kernel here %d \n",i);
}
}
myinterrupt.c:由内核周期性调用,从而产生一个周期性的中断机制
void my_timer_handler(void)
{
printk(KERN_NOTICE "\n>>>>>>>>>>>>>>>>>my_timer_handler here<<<<<<<<<<<<<<<<<<\n\n");
}
然后修改内核代码,使之成为一个简单的时间片轮转多道程序内核,然后重新编译运行,从https://github.com/mengning/mykernel下载代码替换mykernel中的mymain.c和myinterrupt.c,mypuc.h也放在该文件夹下。接下来在shell中将工作目录退回到home/shiyanlou/LinuxKernel/linux-3.9.4/ 然后执行make,重新编译
然后再次输入:qemu -kernel arch/x86/boot/bzImage 启动内核执行结果如下
从上图中可以看到进程的切换过程:在0号进程运行过程中,先是my_timer_handler()被执行,然后执行myschedule(),myschedule()在运行过程中输出switch 0(被切换出去的进程号) to 1(切换到的进程号)。然后就跳到新的进程1继续执行。
代码分析
mypcb.h
/*
* 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;//进程的eip
unsigned long sp;//进程的esp
};
typedef struct PCB{
int pid;//进程id
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
/*
* 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;//0号进程的入口地址为my_process()
task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1];//0号进程的栈顶为stack[]数组的最后一个元素
task[pid].next = &task[pid];//next指针指向自己
/*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].thread.sp - 1) = task[i].thread.sp;
task[i].thread.sp -= 1;
task[i].next = task[i-1].next;新建的进程next指针指向0号进程首地址
task[i-1].next = &task[i];//前一个进程的next指向新创建的进程首地址,形成一个循环链表
}
/* start process 0 by task[0] */
pid = 0;
my_current_task = &task[pid];//当前运行的进程设置为0号进程
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);
}
}
}
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 */
{
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 保存当前进程的ebp*/
"movl %%esp,%0\n\t" /* save esp 保存当前进程的esp*/
"movl %2,%%esp\n\t" /* restore esp 由next->thread.sp存入下一个进程的esp*/
"movl $1f,%1\n\t" /* save eip 下一个进程的eip设置为1f,$1f指标号1:的代码在内存中的地址*/
"pushl %3\n\t" /* 将next->thread.ip压入当前进程的栈*/
"ret\n\t" /* restore eip 弹出上一进程的eip进行进程切换*/
"1:\t" /* next process start here 即$1f指向的地址*/
"popl %%ebp\n\t" /* ebp出栈*/
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
}
return;
}
总结
操作系统的内核有一个起始点,进程从某个点开始执行,在初始化等操作后,如分配内存等,cpu分配给第一个进程执行进程,等到系统所采用的进程调度算法将该进程阻塞后,发生中断,保护现场,cpu执行下一个进程,这样cpu就完成了进程的调度。