秦鼎涛 《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000
一、实验二:完成一个简单的时间片轮转多道程序内核代码(实验楼截图)
二、进程的启动和进程的切换机制
1、多道进程的处理采用了中断机制,利用cpu和内核代码来实现保存现场和回复现场。本次课程的实验平台模拟了一个时间片轮转多道程序的系统。
2、时间片轮转多道程序代码:
| /* | |
| * linux/mykernel/myinterrupt.c | |
| * | |
| * Kernel internal my_timer_handler | |
| * | |
| * Copyright (C) 2013 Mengning | |
| * | |
| */ | |
| > | |
| > | |
| > | |
| > | |
| > | |
| 0; | |
| /* | |
| * Called by timer interrupt. | |
| * it runs in the name of current running process, | |
| * so it use kernel stack of current running process | |
| */ | |
| void) | |
| { | |
| 1 | |
| 1) | |
| { | |
| 1; | |
| } | |
| time_count ++ ; | |
| endif | |
| return; | |
| } | |
| void) | |
| { | |
| tPCB * next; | |
| tPCB * prev; | |
| NULL | |
| NULL) | |
| { | |
| return; | |
| } | |
| /* schedule */ | |
| next = my_current_task->next; | |
| prev = my_current_task; | |
| /* -1 unrunnable, 0 runnable, >0 stopped */ | |
| { | |
| /* switch to next process */ | |
| volatile( | |
| /* save ebp */ | |
| /* save esp */ | |
| /* restore esp */ | |
| /* save eip */ | |
| /* restore eip */ | |
| /* next process start here */ | |
| ip) | |
| ip) | |
| ); | |
| my_current_task = next; | |
| } | |
| else | |
| { | |
| 0; | |
| my_current_task = next; | |
| /* switch to new process */ | |
| volatile( | |
| /* save ebp */ | |
| /* save esp */ | |
| /* restore esp */ | |
| /* restore ebp */ | |
| /* save eip */ | |
| /* restore eip */ | |
| ip) | |
| ip) | |
| ); | |
| } | |
| return; | |
| } | |
三、总结:
课程中提到了计算机工作的三大法宝:存储程序计算机工作模型、堆栈、中断。
-
存储程序计算机工作模型,简单的说就是CPU解释并执行计算机指令,Memory用来存储数据和程序。
-
堆栈机制(函数调用堆栈),在机器语言和汇编语言的时候并不那么重要,后来有了高级语言,尤其是函数调用使得堆栈成为计算机工作的重要基础;
-
中断,引入中断机制使内核可以处理硬件外设I/O。中断来源有I/O请求、时钟以及系统调用。中断可以使计算机同时处理多个程序,提高了计算机效率。