2019-2020-1 20209329《Linux内核原理与分析》第三周作业
《Linux内核原理与分析》第三周作业
1.mykernel实验
实验楼的虚拟机的环境配置已经搭载好,输入以下命令,可以直观地感受到cpu的中断机制。
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
运行结果如下:
在mykernel目录下查看mymain.c和myinterrupt.c源代码,可以发现qemu窗口输出是mymain.c里的printk语句,并周期性的输出myinterrupt.c里的printk语句。
其中每隔一段时间,发生一次时钟中断,能够触发myinterrupt.c中的代码。
在此基础上,可以在mymain.c写入进程描述PCB和进程链表管理等代码,在myinterrupt.c写入进程切换代码。这样就完成了一个简单的时间片轮转多道程序内核。
2.编写一个简单的时间片轮转多道程序内核
2.1编写mypcb.h头文件
#define MAX_TASK_NUM 4
#define KERNEL_STACK_SIZE 1024*8 //进程控制块
/* CPU-specific state of this task */
struct Thread { //存储ip,sp
unsigned long ip;
unsigned long sp;
};
typedef struct PCB{
int pid; //进程的id
volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
char stack[KERNEL_STACK_SIZE]; //内核堆栈
/* CPU-specific state of this task */
struct Thread thread;
unsigned long task_entry; //指定的入口,平时入口为main函数
struct PCB *next; //进程用链表连接
}tPCB;
void my_schedule(void); //函数,调度器
该头文件包含了对结构体PCB的定义,用来存放进程的各种信息。
2.2修改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类型的数组
tPCB * my_current_task = NULL; //声明当前task的指针
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; //初始化0号进程
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]; //指向自己,系统启动只有0号进程
/*fork more process */
for(i=1;i<MAX_TASK_NUM;i++)
{
memcpy(&task[i],&task[0],sizeof(tPCB));
task[i].pid = i;
task[i].state = -1;
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(%1) 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 ,ret之后0号进程正式启动*/
"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) //循环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);
}
}
}
mymain.c文件首先对0号进程进行了描述,然后建立了多个进程,进程间组成一个环形链表。my_process函数是一个死循环,不断的判断my_need_sched是否为1(是否需要调度)
值得关注的是对0号进程的启动
/* 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(%1) to esp,将0号进程的堆栈指针赋予esp */
"pushl %1\n\t" /* push ebp ,把0号进程的堆栈指针压进0号进程的堆栈 */
"pushl %0\n\t" /* push task[pid].thread.ip ,把0号进程的ip指针(my_process函数的地址)压进0号进程的堆栈 */
"ret\n\t" /* pop task[pid].thread.ip to eip ,ret之后0号进程正式启, 把堆栈中的ip指针弹出给eip,开始进入my_process函数 */
"popl %%ebp\n\t"
:
: "c" (task[pid].thread.ip),"d" (task[pid].thread.sp) /* input c or d mean %ecx/%edx*/
);
2.3修改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,%1f指接下来的标号为1的位置 */
"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;
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 */
"pushl %3\n\t"
"ret\n\t" /* restore eip */
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
}
return;
}
myinterrupt.c中my_timer_hander函数实现了对时间片大小的设置,时间片用完时设置调度的标志(修改my_need_sched)。当执行中断程序myinterrupt.c修改了my_need_sched后,回到mymain.c中的my_process函数,触发调度,转到my_schedule()函数。
在my_schedule()函数中,实现了进程的调度。
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 ,将下一个进程的sp移动到esp*/
"movl $1f,%1\n\t" /* save eip,%1f指接下来的标号为1的位置 ,将$1f保存到prev->thread.ip*/
"pushl %3\n\t" /* 将下一个进程的ip压进下一个进程的堆栈 */
"ret\n\t" /* restore eip ,将堆栈的ip弹出给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;
printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);
/* switch to new process */
asm volatile(
"pushl %%ebp\n\t" /* save ebp ,将当前的epb压进栈*/
"movl %%esp,%0\n\t" /* save esp ,将当前esp压进pre->thread.sp*/
"movl %2,%%esp\n\t" /* restore esp ,将next->thread.sp移动到esp*/
"movl %2,%%ebp\n\t" /* restore ebp ,将next->thread.sp移动到ebp*/
"movl $1f,%1\n\t" /* save eip ,将$1f保存到prev->thread.ip*/
"pushl %3\n\t" /* 将下一个进程的ip压进下一个进程的堆栈 */
"ret\n\t" /* restore eip ,将堆栈的ip弹出给eip */
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
}
return;
}
3.运行结果
4.学习感受
通过这次对一个简单的操作系统内核的分析,我对中断服务程序有了更加直观的感受,并对时间片的应用、进程间的调度有了解,不由得感叹操作系统代码实现的巧妙。另外有一些深入的问题还不了解,比如mymain.c和myinterrupt.c是如何交替运行的,中断信号为什么能够触发myinterrupt.c,为什么中断信号发生后只执行my_timer_handler函数。