在简单的时间片轮转多道程序内核代码浅析(一),跟大家演示了一个最简单的操作系统内核大概是个什么样子,这篇文章我将修改内核代码,使之成为一个简单的时间片轮转多道程序内核,然后重新编译运行。
从https://github.com/mengning/mykernel上下载mypcb.h;mymain.c;myinterrupt.c;
将原来home/shiyanlou/LinuxKernel/linux-3.9.4/mykernel/mymain.c;myinterrupt.c用新下载的文件替换并且将mypcb.h也放在这里。
然后将工作目录退回到home/shiyanlou/LinuxKernel/linux-3.9.4/
然后执行make,重新编译内核。
具体操作如下
cd ~/LinuxKernel/linux-3.9.4
git clone https://github.com/mengning/mykernel.git mykernel_new
cd mykernel_new
cp mymain.c myinterrupt.c mypcb.h ../mykernel
cd ..
效果如下:
然后再次输入:
- qemu -kernel arch/x86/boot/bzImage 命令
启动内核。
![](https://i-blog.csdnimg.cn/blog_migrate/bbc1e844df62998dececa969ee4de96d.jpeg)
代码分析
mypcb.h
/*
* linux/mykernel/mypcb.h
*
* Kernel internal PCB types
*
* Copyright (C) 2013 Mengning
*
*/
#define MAX_TASK_NUM 4 //最大进程数,这里设置为了4个。
#define KERNEL_STACK_SIZE 1024*8 //每个进程的内核栈的大小。
/* CPU-specific state of this task */
struct Thread { //声明线程的结构
unsigned long ip; //用于保存进程的eip
unsigned long sp; //用户保存进程的esp
};
typedef struct PCB{ //定义进程的pcb
int pid; //进程的id号
volatile long state; //进程的状态:-1 不可运行, 0 可运行, >0 停止的
char 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,它的任务是内核初始化的0号进程启动
/*
* 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数组
tPCB * my_current_task = NULL; //当前tPCB数组的指针
volatile int my_need_sched = 0; //需要调度与否标志
void my_process(void); //声明进程函数
void __init my_start_kernel(void)
{
int pid = 0;
int i;
/* Initialize process 0*/ //初始化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)); //复制0号进程状态,即初始化
task[i].pid = i; //更改id号
task[i].state = -1; //进程状态都设置成未运行
//*(&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; //新创建的进程的next指向0号进程的首地址 task[i-1].next = &task[i]; //前一个进程的next指向最新创建的进程的首地址,从而成为一个循环链表。 } /* start process 0 by task[0] */ //启动当前进程 pid = 0; my_current_task = &task[pid]; //当前进程指向pcb0 asm volatile( //嵌入汇编代码,构建起0号进程运行的相关堆栈,从而构建起其运行的相关cpu环境 "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*/ ); } //到此为止,0号进程已经启动 int i = 0; void my_process(void) //所有进程都是执行my_process动作 { while(1) { i++; if(i%10000000 == 0) //每1000万次输出当前进程并产生时钟中断 { 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) //1000次没有被调度就被调度并且调度标志置为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; //next记录下一个进程pcb prev = my_current_task; //prev记录当前进程pcb if(next->state == 0) //如果一下个进程正在执行,即两个正在执行的进程之间进行上下文切换 { /* 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) ); }
else //如果下一个将要运行的进程还从未运行过。 { next->state = 0; //将其设置为运行状态。 my_current_task = next; //当前进程切换为next printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->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 */ // 将要被切换出去的进程的ip设置为$1f。这样等一下它被切换回来时(一定是运行状态)肯定会进入if判断分支,可以从if中的标号1处继续执行。 "pushl %3\n\t" //将next->thread.ip(因为它还没有被运行过,所以next->thread.ip现在仍处于初始状态,即指向my_process(),压入将要被切换出去的进程的堆栈。
"ret\n\t" // 将刚刚压入的next->thread.ip出栈至eip,完成进程切换。: "=m" (prev->thread.sp),"=m" (prev->thread.ip) : "m" (next->thread.sp),"m" (next->thread.ip) ); } return; }
总结
mypcb.h 定义了进程pcb和线程结构体
mymain.c 初始化了0号process的pcb,并将进程设为runnable,而且将执行入口设为my_process.依次初始化了1 2 3号,设为unrunnable,并将0 1 2 3 号process的next指针 分别设为 1 2 3 0的地址,(形成一个单循环链表), 并设置各自thread.sp指针为各自内核栈的起始地址。然后汇编代码中,先将当前esp设为0号进程的线程堆栈地址,并入栈保存,然后通过push/ret的方式,间接call了0号process的线程ip地址处的my_process函数。之后的
pop %ebp
是下一个被调度到的process第一个执行的代码。到了my_process函数中,每1000万次时钟中断一次。myinterrupt.c my_schedule函数负责调度,若next process第一次执行(state == -1),则按else 分支中的流程:入栈ebp, 保存当前esp 到prev->thread.sp, eip到 prev->thread.ip, 然后将ebp, esp设置为next->thread.sp, 然后与0号process同样的方法(push thread.ip; ret)来call my_process函数。若next process 又一次被调度(state == 0), 完成进程上下文切换。
操作系统是如何工作的
操作系统的内核有一个起始位置,从这个起始位置开始执行第一个进程。一个进程有一个进程pcb,即进程控制块,包含了进程id,状态等一系列信息,是进程存在的标志。在c语言实现时,我们将pcb设置为一个包含许多进程信息的结构体tPCB。执行第一个进程,即0号进程的过程如下,cpu首先创建0号进程的pcb,然后构建起进程上下文,就是设置操作相关寄存器出栈入栈,然后通过一定的调度算法,比如时间片轮转算法,在一个时间片后,发生中断,0号进程被阻塞,在完成保存现场后将CPU分配给下一个进程,执行下一个进程。这样,操作系统就完成了基本的进程调度的功能。
中断上下文切换和进程上下文切换,是操作系统的“两把剑”。
-
张何灿 + 原创作品转载请注明出处 + 《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000