魏昊卿——《Linux内核分析》第二周作业:了解操作系统是怎样工作的
一、实验部分
使用实验楼的虚拟机打开shell
cd LinuxKernel/linux-3.9.4 qemu -kernel arch/x86/boot/bzImage
然后cd mykernel 您可以看到qemu窗口输出的内容的代码mymain.c和myinterrupt.c
使用自己的Linux系统环境搭建过程参见mykernel,其中也可以找到一个简单的时间片轮转多道程序内核代码
mymain.c
myinterrupt.c
二、分析进程的启动和进程的切换机制
- 往往系统都有很多进程比较复杂,我们假定当前系统只有两个进程0和1,第一次调度是从0切换到1,也就是prev=0,next=1,第二次调度正好相反。
- 这时再看myinterrupt.c的汇编代码,保存prev的进程(0)上下文,下次调度是next进程就是0了,反之进程1是next那它肯定之前作为prev被调度出去过。理解进程上下文的保存和恢复极为关键。
- $1f就是指标号1:的代码在内存中存储的地址
- 再来看特殊一点代码切换到一个新的进程,也就是next没有被保存过进程上下文,它从没有被执行过,这时稍特殊一点即else部分的汇编代码
源代码:
mypcb.h
1 /* 2 * linux/mykernel/mypcb.h 3 * 4 * Kernel internal PCB types 5 * 6 * Copyright (C) 2013 Mengning 7 * 8 */ 9 10 #define MAX_TASK_NUM 4 11 #define KERNEL_STACK_SIZE 1024*8 12 13 /* CPU-specific state of this task */ 14 struct Thread { 15 unsigned long ip; 16 unsigned long sp; 17 }; 18 19 typedef struct PCB{ 20 int pid; 21 volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */ 22 char stack[KERNEL_STACK_SIZE]; 23 /* CPU-specific state of this task */ 24 struct Thread thread; 25 unsigned long task_entry; 26 struct PCB *next; 27 }tPCB; 28 29 void my_schedule(void);
详细解析:
1.
struct Thread { unsigned long ip; unsigned long sp; };
用来存储ip和sp
2.
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; 进程入口 struct PCB *next; 进程链表 }tPCB;
定义进程管理相关的数据结构,具体内容如上
3.
void my_schedule(void);
调度器
mymain.c
1 /* 2 * linux/mykernel/mymain.c 3 * 4 * Kernel internal my_start_kernel 5 * 6 * Copyright (C) 2013 Mengning 7 * 8 */ 9 #include <linux/types.h> 10 #include <linux/string.h> 11 #include <linux/ctype.h> 12 #include <linux/tty.h> 13 #include <linux/vmalloc.h> 14 15 16 #include "mypcb.h" 17 18 tPCB task[MAX_TASK_NUM]; 19 tPCB * my_current_task = NULL; 20 volatile int my_need_sched = 0; 是否需要调度 21 22 void my_process(void); 23 24 25 void __init my_start_kernel(void) 26 { 27 int pid = 0; 28 int i; 29 /* Initialize process 0*/初始化0号进程 30 task[pid].pid = pid; 进程id 31 task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped */进程状态 32 task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process; 进程入口 33 task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1]; 栈顶 34 task[pid].next = &task[pid]; 35 /*fork more process */ 生成其他进程 36 for(i=1;i<MAX_TASK_NUM;i++) 新进程的相关信息 37 { 38 memcpy(&task[i],&task[0],sizeof(tPCB)); 39 task[i].pid = i; 40 task[i].state = -1; 41 task[i].thread.sp = (unsigned long)&task[i].stack[KERNEL_STACK_SIZE-1]; 42 task[i].next = task[i-1].next; 43 task[i-1].next = &task[i]; 44 } 45 /* start process 0 by task[0] */ 启动0号进程 46 pid = 0; 47 my_current_task = &task[pid]; 48 asm volatile( 49 "movl %1,%%esp\n\t" /* set task[pid].thread.sp to esp */ 50 "pushl %1\n\t" /* push ebp */ 51 "pushl %0\n\t" /* push task[pid].thread.ip */ 52 "ret\n\t" /* pop task[pid].thread.ip to eip */ 53 "popl %%ebp\n\t" 54 : 55 : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp) /* input c or d mean %ecx/%edx*/ 56 ); 57 } 58 void my_process(void) 59 { 60 int i = 0; 61 while(1) 62 { 63 i++; 64 if(i%10000000 == 0) 65 { 66 printk(KERN_NOTICE "this is process %d -\n",my_current_task->pid); 67 if(my_need_sched == 1) 68 { 69 my_need_sched = 0; 70 my_schedule(); 71 } 72 printk(KERN_NOTICE "this is process %d +\n",my_current_task->pid); 73 } 74 } 75 }
详细解析:
1.分析void __init my_start_kernel(void)
/* Initialize process 0*/初始化0号进程 task[pid].pid = pid; 进程id 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++) 新进程的相关信息,复制0号进程 { 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] */ 启动0号进程,采用嵌入式汇编,构建了CPU的运行环境 pid = 0; my_current_task = &task[pid]; asm volatile( "movl %1,%%esp\n\t" /* set task[pid].thread.sp to esp */ 将sp的值放到esp中 "pushl %1\n\t" /* push ebp */ ebp等于esp,将ebp压栈 "pushl %0\n\t" /* push task[pid].thread.ip */ eip压栈 "ret\n\t" /* pop task[pid].thread.ip to eip */ 返回process函数的头部 "popl %%ebp\n\t" ebp出栈 : : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp) /* input c or d mean %ecx/%edx*/ );
2.void my_process(void) 所有进程的代码相同,主动调度机制
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); } } }
myinterrupt.c
1 /* 2 * linux/mykernel/myinterrupt.c 3 * 4 * Kernel internal my_timer_handler 5 * 6 * Copyright (C) 2013 Mengning 7 * 8 */ 9 #include <linux/types.h> 10 #include <linux/string.h> 11 #include <linux/ctype.h> 12 #include <linux/tty.h> 13 #include <linux/vmalloc.h> 14 15 #include "mypcb.h" 16 17 extern tPCB task[MAX_TASK_NUM]; 18 extern tPCB * my_current_task; 19 extern volatile int my_need_sched; 20 volatile int time_count = 0; 21 22 /* 23 * Called by timer interrupt. 24 * it runs in the name of current running process, 25 * so it use kernel stack of current running process 26 */ 27 void my_timer_handler(void) 28 { 29 #if 1 30 if(time_count%1000 == 0 && my_need_sched != 1) 31 { 32 printk(KERN_NOTICE ">>>my_timer_handler here<<<\n"); 33 my_need_sched = 1; 34 } 35 time_count ++ ; 36 #endif 37 return; 38 } 39 40 void my_schedule(void) 41 { 42 tPCB * next; 43 tPCB * prev; 44 45 if(my_current_task == NULL 46 || my_current_task->next == NULL) 47 { 48 return; 49 } 50 printk(KERN_NOTICE ">>>my_schedule<<<\n"); 51 /* schedule */ 52 next = my_current_task->next; 53 prev = my_current_task; 54 if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */ 55 { 56 /* switch to next process */ 57 asm volatile( 58 "pushl %%ebp\n\t" /* save ebp */ 59 "movl %%esp,%0\n\t" /* save esp */ 60 "movl %2,%%esp\n\t" /* restore esp */ 61 "movl $1f,%1\n\t" /* save eip */ 62 "pushl %3\n\t" 63 "ret\n\t" /* restore eip */ 64 "1:\t" /* next process start here */ 65 "popl %%ebp\n\t" 66 : "=m" (prev->thread.sp),"=m" (prev->thread.ip) 67 : "m" (next->thread.sp),"m" (next->thread.ip) 68 ); 69 my_current_task = next; 70 printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid); 71 } 72 else 73 { 74 next->state = 0; 75 my_current_task = next; 76 printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid); 77 /* switch to new process */ 78 asm volatile( 79 "pushl %%ebp\n\t" /* save ebp */ 保存当前进程ebp 80 "movl %%esp,%0\n\t" /* save esp */ 保存eso 81 "movl %2,%%esp\n\t" /* restore esp */ 下一进程的esp放入esp中 82 "movl %2,%%ebp\n\t" /* restore ebp */ 83 "movl $1f,%1\n\t" /* save eip */ 保存eip 84 "pushl %3\n\t" 将下一个进程的eip保存在堆栈中 85 "ret\n\t" /* restore eip */ 86 : "=m" (prev->thread.sp),"=m" (prev->thread.ip) 87 : "m" (next->thread.sp),"m" (next->thread.ip) 88 ); 89 } 90 return; 91 }
详细分析:
1.void my_timer_handler(void)设置时间片的大小,时间片用完时设置一下调度标志
2.void my_schedule(void)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 prev = my_current_task; if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */如果下一个进程状态为0,则切换进程 { /* 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 */ 将下一进程的esp放入esp寄存器中 "movl $1f,%1\n\t" /* save eip */ "pushl %3\n\t" 保存eip "ret\n\t" /* restore eip */ 将下一进程的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) ); my_current_task = next; printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid); } 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 */ 保存ebp "movl %%esp,%0\n\t" /* save esp */ 保存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; }
三、总结这个星期的学习让我了解了linux操作系统的相关知识,并且老师通过对代码的讲解,着重让我知道了进程切换机制。在时间片轮转或者其他方式下,进程之间形成一个链表,一个进程运行到一定时间或者达到一定条件的时候,就切换成下一个程序运行,进程切换伴随着堆栈切换。 魏昊卿 原创作品转载请注明出处 《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000
时间: 2024-10-26 12:53:52