linux时间片轮转小程序
2016-03-06 20:42
435 查看
王涵宇 原创作品转载请注明出处 《Linux内核分析》MOOC课程 http://mooc.study.163.com/course/USTC-1000029000
1.
一个简化了的时间片轮转程序,先运行实验楼linux自带的时间片轮转程序,结果如下:
2.代码
mymain.c
myinterrupt.c
3.分析
4.总结:
通过代码能够得到时间片轮转的成功原因:
a.通过栈保存上下文信息;
b.ret指令弹出栈存储的ip(程序入口)到eip寄存器,使得程序能顺利拿到下一条指令
c.得益于pcb结构体通过指针相连
d.state记录状态,-1表示不可运行,0表示可以运行
1.
一个简化了的时间片轮转程序,先运行实验楼linux自带的时间片轮转程序,结果如下:
2.代码
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]; //pcb数组 tPCB * my_current_task = NULL;//指向pcb的指针 volatile int my_need_sched = 0;//是否需要调度 void my_process(void); //进程的层次结构PCB->thread->ip,sp,每一个线程有自己的栈,程序入口my_process //task数组记录了N个线程结构体,入口是myprocess void __init my_start_kernel(void) { //0号进程的初始化 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];//栈顶是定义的stack task[pid].next = &task[pid];//指针指向自己,因为启动时候没有其他进程 /*fork more process */ //创建更多的进程 for(i=1;i<MAX_TASK_NUM;i++) { memcpy(&task[i],&task[0],sizeof(tPCB)); //把0号进程的状态赋值N个 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];//新fork的进程加到列表尾部 } /* start process 0 by task[0] */ //0号进程开始执行 pid = 0; my_current_task = &task[pid]; //保存上一个eip,到新的eip执行 asm volatile( "movl %1,%%esp\n\t" /* set task[pid].thread.sp to esp */ "pushl %1\n\t" /* push ebp */ //由于esp=ebp的原因,相当于push ebp "pushl %0\n\t" /* push task[pid].thread.ip */ //把当前eip压栈 "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_schedule { my_need_sched = 0; a19a my_schedule(); } printk(KERN_NOTICE "this is process %d +\n",my_current_task->pid); } } }
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; }
3.分析
4.总结:
通过代码能够得到时间片轮转的成功原因:
a.通过栈保存上下文信息;
b.ret指令弹出栈存储的ip(程序入口)到eip寄存器,使得程序能顺利拿到下一条指令
c.得益于pcb结构体通过指针相连
d.state记录状态,-1表示不可运行,0表示可以运行
相关文章推荐
- 神器SystemTap
- 开源操作系统和必备工具网站收集
- 解读Linux安全机制之栈溢出保护
- 簡單設定 kernel 選項在使用 iptables 前
- Ubuntu12.04内核升级出了问题
- [Linux学习笔记] Linux系统引导流程(一)
- 更新Debian内核e1000e驱动模块
- kernel: printk: 2 messages suppressed.
- linux 内核 hash table 的使用
- g++编译 参数 .
- 函数调用时函数栈状态分析
- Linux Kernel Panic报错解决思路
- kernel: TCP: time wait bucket table overflow错误的解决办法
- linux安全相关
- 内核的主要配置文件的详细说明
- Linux Kernel 2.6.32 Local Root Exploit (x86_64)
- LINUX ulimit命令详解
- Linux Kernel kNFSd 整数溢出 拒绝服务漏洞 .
- 汇编调用c函数为什么要设置栈
- 基于busybox和linux kernel制作小linux