Linux内核分析之时间片轮转调度

编程入门 行业动态 更新时间:2024-10-27 10:30:33

Linux<a href=https://www.elefans.com/category/jswz/34/1769575.html style=内核分析之时间片轮转调度"/>

Linux内核分析之时间片轮转调度

zh.c + 原创作品转载请注明出处 + 《Linux内核分析》MOOC课程

计算机工作中的三个法宝:
  1. 函数调用堆栈:记录调用的路径和参数的空间
  2. 存储程序计算机:冯诺依曼结构
  3. 中断机制:由CPU和内核代码共同实现了保存现场和恢复现场,把ebp,esp,eip寄存器的数据push到内核堆栈中。再把eip指向中断程序的入口,保存现场。
C代码中嵌入汇编代码

内嵌汇编语法

__asm__(
    汇编语句
    :输出部分
    :输入部分
    破坏描述部分(即寄存器改变部分)
    );
操作系统工作中的两把宝剑:
  1. 中断上下文
  2. 进程上下文的切换
时间片轮转调度代码分析

1. 首先分析mypcb.h文件中代码:

#define MAX_TASK_NUM 4   
#define KERNEL_STACK_SIZE 1024*8/* CPU‐specific state of this task */
struct Thread 
{unsigned long ip;  //保存该线程的%eipunsigned long sp;  //保存该线程的%esp
};/*定义了进程管理相关的数据结构*/
typedef struct PCB
{int pid;  //进程idvolatile 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);  //调度器

该文件主要定义了进程和线程管理相关的数据结构

2.接着我们分析mymain.c文件的代码块

#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;  //在内核运行前初始化进程,从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; //进程的入口,从my_process函数开始,========(unsigned long)my_process是传入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; //从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 由于刚开始时内核堆栈中%esp等于%ebp*/"pushl %0\n\t" /* push task[pid].thread.ip 把0进程的eip入栈的目的是为了下一行代码赋值给%eip寄存器(程序员不能直接操作%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*/);
} //my_process()函数是进程所做的工作
void my_process(void)
{int i = 0;while(1){i++;if(i%10000000 == 0){//打印调度前的进程IDprintk(KERN_NOTICE "this is process %d ‐\n",my_current_task‐>pid);if(my_need_sched == 1){my_need_sched = 0;my_schedule();   //主动调度,,但是查看该文件,并没有发现#include "myinterrupt.c"文件,究竟是怎么导入该文件的呢,啊,不解啊。先留着这个坑} //打印调度后的进程ID,其实是原来的进程IDprintk(KERN_NOTICE "this is process %d +\n",my_current_task‐>pid);}}
}

该文件的作用是初始化进程,定义了进程所做的工作,并开始启动进程

3.最后我们分析myinterrupt.c文件的代码块

#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 1if(time_count%1000 == 0 && my_need_sched != 1){printk(KERN_NOTICE ">>>my_timer_handler here<<<\n");my_need_sched = 1;} time_count ++ ;
#endifreturn;
} //进程调度函数,在这个实验中是被mymain.c文件中my_process()函数所调用
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;
}

这个文件主要作用是定义时间片的操作,进程间的切换

操作系统是如何进行工作的

操作系统首先初始化内核相关的进程,然后开始循环运行这些进程,进程间进行切换时,则利用内核堆栈所保存的每个进程的sp,ip即所对应的%esp,%eip寄存器中的值,对当前的进程的sp,ip即对应%esp,%eip寄存器的值进行保存(中断上下文),并用下一个进程的sp,ip的值赋值给%esp,%eip寄存器(进程间切换)。

更多推荐

Linux内核分析之时间片轮转调度

本文发布于:2024-02-17 08:18:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1693306.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内核   时间   Linux

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!