linux 进程调度程序分析 基于Mykernel
来源:互联网 发布:期货交易数据下载 编辑:程序博客网 时间:2024/05/16 16:16
由于时间仓促,难免有遗漏 错误地方 敬请指出!
实验环境;虚拟机kali linux 3.18
编译器:gcc
linux源代码:C语言 汇编
编辑器:vim
首先贴出mykernel的源码 以下会对一些源码做出解释 一共三个文件mymain.c myintterrupt.c mypcb.h
<pre name="code" class="cpp">/* * 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); } }}
/*
* linux/mykernel/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);
</pre><pre name="code" class="cpp"><pre name="code" class="cpp">/* * 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;}
mykernel,它是提供初始化好的CPU从my_start_kernel开始执行,并提供了时钟中断机制周期性性执行my_time_handler中断处理程序,执行完后中断返回总是可以回到my_start_kernel中断的位置继续执行。以上是需要知道的前提知识 这点对于理解代码至关重要。 让我们首先从my_start_kernel函数开始分析。首先创建了一个tPCB类型链表 task,然后用了my_current_task指定当前进程。(tPCB内各个元素含义详见 mypcb.h)task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process; 中的myprocess需要知道代表的是函数内存地址。 for循环并不是很难,也就是通过memcpy()把task[0]进程复制到各个进程 进程总个数由MAX_TASK_NUM决定。my_current_task = &task[pid];这句话和刚才一样,就不解释了。而mymain.c中的内嵌汇编也就是保存当前上下文。为后面进程切换做好准备。而myinterrupt.c是由time interrupt调用,所以我们在mymain.c中未见有调用myinterrupt.c的代码. 而其中的两段内联汇编仔细一看 也是一样的 所以也没有继续分析的必要 通过这个 我们就成功的实现了一个进程调度程序。
---张振海 MOOC
- linux 进程调度程序分析 基于Mykernel
- 从mykernel分析linux任务调度
- 基于一个简单内核mykernel的Linux内核时间调度的分析
- 基于mykernel实现的时间片轮转调度代码分析
- 基于Linux的进程调度模拟程序
- 基于Linux系统中进程调度分析
- Linux 进程调度分析
- linux进程调度分析
- Linux进程调度分析
- 基于mykernel的一个简单的时间片轮转多道程序内核代码分析
- 基于mykernel的时间片轮转调度的实现
- 《Linux内核分析》MOOC课-mykernel实验
- Linux进程调度算法分析
- linux深入分析进程调度
- Linux进程调度(3):进程切换分析
- 分析linux进程调度与进程切换
- [网易云课堂]Linux内核分析(二)—— mykernel内核部署及简单时间片轮转程序分析
- 基于时间片的程序调度分析
- PCLint使用介绍
- 2.3 Listen系统调用
- myNote
- set
- Raw Socket编程
- linux 进程调度程序分析 基于Mykernel
- Spring源码研究--下载-编译-导入eclipse-验证
- BestCoder Round #33 C
- css背景,文本,字体
- RDIFramework.NET 框架之组织机构权限设置
- 求小球自由落体反弹十次总路程及最后一次反弹高度的c代码
- iOS越狱和沙盒机制的关系
- Mac的内存管理
- 【Java集合源码剖析】LinkedHashmap源码剖析