Linux系统分析实验(一):时间片轮转多道程序内核
注:后三位416原创作品转载请注明出处<https://github.com/mengning/linuxkernel/
实验环境
Ubuntu16.04虚拟机VMware workstation 12 Player
实验步骤
-
下载
linux3.9.4版本内核源码并打上mykernel的补丁wget <https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.9.4.tar.xz>wget <https://raw.github.com/mengning/mykernel/master/mykernel_for_linux3.9.4sc.patch>tar -xvf linux-3.9.4.tarcd linux-3.9.4patch -p1 < ../mykernel_for_linux3.9.4sc.patch

-
编译
make allnoconfig make

编译报错:缺少compiler-gcc5.h的头文件。cd到include/linux下发现只有compiler-gcc4.h,我们将该文件重命名为compiler-gcc5.h:mv compiler-gcc4.h compiler-gcc5.h。
-
安装qemu启动内核
sudo apt-get install qemu # install QEMU sudo ln -s /usr/bin/qemu-system-i386 /usr/bin/qemu qemu -kernel arch/x86/boot/bzImage # 使用qemu启动内核

mykernel代码分析
从孟宁老师的主页上获取源码 https://github.com/mengning/mykernel 下载mymain.c ,myinterrupt.c 和 mypcb.h三个文件。
mypcb.h:
该头文件包含一个thread结构体线程的上下文(指令指针和栈顶指针)。PCB结构定义了进程控制块:
(1)pid进程标识符;
(2)state状态,-1表示不可运行,0表示可运行,>0表示停止;
(3)定义了一个栈空间;
(4)一个Thread变量;
(5)任务入口点;
(6)下一个PCB的指针;
struct Thread {
unsigned long ip;//point to cpu run address
unsigned long sp;//point to the thread stack's top address
//todo add other attrubte of system thread
};
//PCB Struct
typedef struct PCB{
int pid; // pcb id
volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
char stack[KERNEL_STACK_SIZE];// each pcb stack size is 1024*8
/* CPU-specific state of this task */
struct Thread thread;
unsigned long task_entry;//the task execute entry memory address
struct PCB *next;//pcb is a circular linked list
unsigned long priority;// task priority ////////
//todo add other attrubte of process control block
}tPCB;
myinterrupt.c:
主要实现定时器中断,每隔1000ms进行my_need_sched的检查,如果不为1,则置为1使其进入就绪队列。my_schedule函数具体实现了进程的切换。声明了两个指针,prev和next,分别指向当前进程和下一个进程。
void my_timer_handler(void)
{
#if 1
if(time_count%

本文介绍了Linux系统分析实验,主要内容是时间片轮转多道程序内核的实现。实验涉及下载内核源码、编译、分析代码,特别是对进程控制块PCB的讲解,以及如何通过定时器中断实现进程切换。实验总结部分探讨了基于优先级的调度算法,包括nice值和实时优先级,并解释了时间片对系统响应和进程切换的影响。
最低0.47元/天 解锁文章
3233

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



