第二周 内核进程调度
2015-03-15 17:33
197 查看
1.介绍
操作系统为了实现支持多任务处理的需要,一般都会支持多进程的机制,所以进程的的切换是内核当中一个非常重要的功能模块,内核几个功能模块主要有
处理器管理
内存管理
磁盘管理
输入输出管理
进程管理
本次实验主要是模拟了内核中的进程切换机制,以便加深对内核进程切换的理解。其中本次实验主要涉及以下三个代码文件。
mypcb.h
mymain.c
myiterrupt.c
4. 源代码分析
mypcb.h
mypcb.h文件中主要定义了PCB的数据结构,PCB(Process Control Block)在计算机当中用于存储一个进程当中的相关变量,由以上代码可以看出,地应的PCB中包含了了当前进程的
状态(state,)
堆栈(stack[KERNEL_STACK_SIZE]),
esp,eip(thread),
程序入口(task_entry),
指向的下一个进程的指针(next)。
mymain.c
mymain.c文件主要的主要是初始化相关进程。并引导最初的进程运行。首先
_init是一个宏定义,其宏定义如下,主要实现的功能是将这段代码放在text段中。
29-35行代码主要完成的对process0(最初进程)的初始化工作。然后利用for循环对已经初始化好的process0进行复制,快速的初始化好其它进程的相关信息。
这里注意process0的task_entry设置为my_process,即process0将调用my_process函数。
48-55行内置汇编主要是启动process0 其中首先保存esp eip然后在52行ret指令时进入my_process函数进行执行。
my_process函数主要作用是打印当前进程的信息。并且通过判断 my_need_sched 的值来决定是否进行进程切换,即调用my_schedule();而my_need_sched 这个变量的值则主要通过时间中断来实现改变,见myinterrupt.c代码
myinterrupt.c
my_timer_handler函数主要作用是修改my_need_sched从而实现进程的切换,这里利用time_count进行计数的方式控制触发进程切换条件的时间。可以修改1000实现更大或者更小的进程切换时间
my_schedule则是控制进程切换的函数。首先对要切换的进程进行判断,是否是新进程,若是是runnable则进入if语句中。79-86行主要作用是进程的切换。这里先将esp,ebp压栈(即将当前进程的栈顶,栈底 压栈)。然后将将要调度的进程的sp赋值esp寄存器当中。然后保存当前eip值。在63当中进入下一个进程当中。这里注意$1f指的是65代码的地址,即标号1:的下一行。
这样保存的意义就是让进程返回时接着下一行代码执行。
如果下一个将要调度的进程是个新进程,那么就会进入到else当中,其中与之前的代码很相似,不同在于由于是新进程,ebp,esp没有值,这里要构造相应的ebp,esp。见81, 82行。
[b]5. 总结[/b]
通过实验加深了对内核进程切换的理解,这里进程切换主要借鉴了函数调用的相关思想,将每个进程当作一个函数,在进入进程前先保存当前进程的相关参数,以便下一次调用进程时恢复现场使用。操作系统通过这种方式实现进程的快速切换,虽然一个CPU(单核)只能同时运行一个进程,但是只要时间片设置的足够小,通过这种方式快速切换各个进程,可以实现多任务处理的作用,从而满足日常生活中人们的各种多线作业的需求。
操作系统为了实现支持多任务处理的需要,一般都会支持多进程的机制,所以进程的的切换是内核当中一个非常重要的功能模块,内核几个功能模块主要有
处理器管理
内存管理
磁盘管理
输入输出管理
进程管理
本次实验主要是模拟了内核中的进程切换机制,以便加深对内核进程切换的理解。其中本次实验主要涉及以下三个代码文件。
mypcb.h
mymain.c
myiterrupt.c
[b]2. 实验结果:[/b]
本实验想要实现的结果是通过时间片的方式对几个进程进行切换,其中每一个进程都由一个数字表示。以下列出实验结果截图(为了使实验结果更加清晰,调短了时间片的时间)3. 流程图:
以下给出程序的运行流程图,然后根据流程图分析相关4. 源代码分析
mypcb.h
* Kernel internal PCB types * * Copyright (C) 2013 Mengning * */ #define MAX_TASK_NUM 4 #define KERNEL_STACK_SIZE 1024*8 /* CPU-specific state of this task */ struct Thread { unsigned long ip; unsigned long sp; }; typedef struct PCB{ int pid; 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; void my_schedule(void)
mypcb.h文件中主要定义了PCB的数据结构,PCB(Process Control Block)在计算机当中用于存储一个进程当中的相关变量,由以上代码可以看出,地应的PCB中包含了了当前进程的
状态(state,)
堆栈(stack[KERNEL_STACK_SIZE]),
esp,eip(thread),
程序入口(task_entry),
指向的下一个进程的指针(next)。
mymain.c
/* * 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 * my_current_task = NULL; 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; 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++) { 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 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 */ "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) { 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文件主要的主要是初始化相关进程。并引导最初的进程运行。首先
void __init my_start_kernel(void)
_init是一个宏定义,其宏定义如下,主要实现的功能是将这段代码放在text段中。
#define __init __section__(".init.text")
29-35行代码主要完成的对process0(最初进程)的初始化工作。然后利用for循环对已经初始化好的process0进行复制,快速的初始化好其它进程的相关信息。
这里注意process0的task_entry设置为my_process,即process0将调用my_process函数。
48-55行内置汇编主要是启动process0 其中首先保存esp eip然后在52行ret指令时进入my_process函数进行执行。
my_process函数主要作用是打印当前进程的信息。并且通过判断 my_need_sched 的值来决定是否进行进程切换,即调用my_schedule();而my_need_sched 这个变量的值则主要通过时间中断来实现改变,见myinterrupt.c代码
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) { 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 */ { /* 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) ); 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 */ "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; }
my_timer_handler函数主要作用是修改my_need_sched从而实现进程的切换,这里利用time_count进行计数的方式控制触发进程切换条件的时间。可以修改1000实现更大或者更小的进程切换时间
my_schedule则是控制进程切换的函数。首先对要切换的进程进行判断,是否是新进程,若是是runnable则进入if语句中。79-86行主要作用是进程的切换。这里先将esp,ebp压栈(即将当前进程的栈顶,栈底 压栈)。然后将将要调度的进程的sp赋值esp寄存器当中。然后保存当前eip值。在63当中进入下一个进程当中。这里注意$1f指的是65代码的地址,即标号1:的下一行。
这样保存的意义就是让进程返回时接着下一行代码执行。
如果下一个将要调度的进程是个新进程,那么就会进入到else当中,其中与之前的代码很相似,不同在于由于是新进程,ebp,esp没有值,这里要构造相应的ebp,esp。见81, 82行。
[b]5. 总结[/b]
通过实验加深了对内核进程切换的理解,这里进程切换主要借鉴了函数调用的相关思想,将每个进程当作一个函数,在进入进程前先保存当前进程的相关参数,以便下一次调用进程时恢复现场使用。操作系统通过这种方式实现进程的快速切换,虽然一个CPU(单核)只能同时运行一个进程,但是只要时间片设置的足够小,通过这种方式快速切换各个进程,可以实现多任务处理的作用,从而满足日常生活中人们的各种多线作业的需求。
相关文章推荐
- 每日阅读3之内核设计与实现(第三版)4.5——linux调度实现之进程选择
- 《Linux0.11内核完全注释》读后小结 --- 进程调度与信号
- linux2.6.29内核 进程调度分析
- 把握linux内核设计思想(九):进程调度
- Linux内核——进程管理与调度
- Linux Kernel 2.6内核进程调度复杂度为O(1)的代码分析
- Linux2.6 内核进程调度分析 .
- Linux内核之进程调度
- Linux内核之进程调度
- Linux内核2.6的进程调度
- linux内核进程调度以及定时器实现机制
- 深入理解Linux内核-第七章 进程调度
- 【Linxu内核设计与实现】-第4章 进程调度
- Linux内核2.6的进程调度
- Linux嵌入式 -- 内核 - 进程控制 和 调度
- Linux-0.11内核源码分析系列:进程调度sleep_on()函数分析
- 绕过内核调度链表进程检测
- Linux嵌入式 -- 内核 - 进程控制 和 调度
- Linux用户抢占和内核抢占详解(概念, 实现和触发时机)--Linux进程的管理与调度(二十)
- [转载]NT 内核的进程调度分析笔记