一、内容简介
在有万经之源之称的《易》中有”太极生两仪,两仪生四象,四象生八卦”这一说法,再复杂的东西其核必简。毫无疑问,计算机的设计亦遵循这一规律。计算机的核心由“0”和“1”组成,但是构建于计算机之上的操作系统确十分复杂。
二、两把宝剑
计算机有三个法宝:存储程序计算机、函数调用堆栈、中断机制。操作系统有两把宝剑:中断与恢复现场,或许将其称作剑与鞘更为贴切。因为中断与恢复都依赖一个数据结构—-堆栈。拔出宝剑,则为出栈。宝剑入鞘,则为进栈。
堆栈可以提供:
- 函数调用框架
- 传递参数
- 保存返回地址
- 提供局部变量空间
- …..
在一个进程中断之后,必须保存该进程的信息,才可以切换到下个进程。而当下面的进程运行结束或同样处于中断状态时,切换到原先的进程则可以继续运行原先的进程。
三、实验与分析
纸上得来终觉浅,绝知此事要躬行。接下来我们运行分析一个精简的操作系统内核。
打开shell运行以下命令
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
全部代码如下
#define MAX_TASK_NUM 4
#define KERNEL_STACK_SIZE 1024*2 # 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);
定义进程相关的数据结构
主程序 在init_my_start_kernel中初始化0号线程,并创建MAX_TASK_NUM数量的线程。process启动运行线程,并且在每运行10000000次之后,进行一次时钟调度。
#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].thread.sp - 1) = task[i].thread.sp;
task[i].thread.sp -= 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);
}
}
}
处理中断
#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;
}
在处理中断的代码中插入了以下汇编指令。该指令保存了将当前进程的esp,ebp,eip信息。并切换至下一个进程。
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)
);
在C语言中内嵌汇编的语法如下
四、总结
操作系统构建于计算机三层“堡垒”之上,具有了更为复杂的特性,其中包含了多个线程的运行。进程的中断与恢复,便是操作系统的两个“翅膀”,使得进程之间可以切换。所谓“道生一,一生二,二生三,三生万物”,各个不同进程构建了如今多如繁星的应用程序的蓝图。
Linux内核分析
writen by 江明星

本文通过精简版Linux内核实现,详细介绍了操作系统中的进程调度机制。包括如何利用存储程序计算机、函数调用堆栈及中断机制等核心概念进行进程管理,以及如何通过汇编语言实现进程间的切换。
1292

被折叠的 条评论
为什么被折叠?



