Linux内核分析——操作系统进程调度

来源:互联网 发布:Windows mr 编辑:程序博客网 时间:2024/06/18 07:00

pianogirl 原创作品转载请注明出处 《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000

一、函数与堆栈
事实证明,上一篇博客中对函数与堆栈的理解是正确的。每一个函数都有一个框架,独立的栈空间。即,压入栈中的每一个函数都有一个帧栈,每个帧栈都以ebp为分界线。经过了三个步骤:
1)建立被调用函数的堆栈框架
enter:pushl %ebp
movl %esp,%ebp
2)执行函数
3)拆除该框架
leave :movl %ebp,%esp
popl %ebp
ret
参数的传递:调用者负责将用到的参数压栈,以eip封顶。调用者通过计算偏移(%ebp+xxx)获取到这些值。用完后由调用者负责清除这些临时变量。借用一下同学的图:
这里写图片描述
注:函数调用约定

函数调用约定包括传递参数的顺序,谁负责清理参数占用的堆栈等。

函数调用约定 参数传递顺序 负责清理参数占用的堆栈
__pascal 从左到右 调用者
__stdcall 从右到左 被调函数
__cdecl 从右到左 调用者

调用函数的代码和被调函数必须采用相同的函数的调用约定,程序才能正常运行。Windows中C/C++程序的缺省函数调用约定是__cdecllinux中gcc默认用的规则是__stdcall编译器在进入函数时,会将寄存器里的参数存入堆栈指定位置。参数和局部变量一样在堆栈中有一席之地。参数可以被理解为由调用函数指定初值的局部变量。

二、操作系统进程调度
这里有一个简易的模拟操作系统进程调度的例子mykernel:
定义进程总数为4,创建成一个循环链表。0 -> 1 -> 2 ->3 ->0 。。。
每一个进程的任务就是打印,打印,打印。。。
用void my_timer_handler(void) 设置一个时间片,即系统时间每过100就打断一下,切换到下一个进程
学习的重难点在于进程上下文的保存和切换。
执行效果如图(网太差,借的别人的图):
这里写图片描述

先看头文件的定义mypcb.h:

#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);

该文件里对进程控制块(Processing Control Block,PCB)做出了简单的结构定义。定义进程管理的相关数据结构:进程ID;进程状态;堆栈;线程(ip和sp);入口;下一个PCB。
再看主函数mymain.c:

#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*/  //初始化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_entry 和 ip 意义一样???为什么用两个?void类型怎么强转??    task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1]; //sp为什么是这个???(刚开始栈空,头尾指针相同)    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链表        task[i-1].next = &task[i];    }    /* start process 0 by task[0] 启动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();//调度一下,切换进程            }        }         }}

以及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 1   //这是什么???    if(time_count%100 == 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 */  //保存当前进程栈底信息ebp            "movl %%esp,%0\n\t"     /* save esp */   //将“进程prev”的sp移动到栈顶            "movl %2,%%esp\n\t"     /* restore  esp */  //esp跳到“进程next”的栈顶指针处            "movl $1f,%1\n\t"       /* save eip */ //记录“进程prev”的入口(又用到flag了,简直晕!!)            "pushl %3\n\t"             "ret\n\t"               /* restore  eip */  //这两句等效为:修改eip的值,使它为“进程next”的入口值,为执行“进程next”做准备                //然后“进程next”就在执行了,不断打印,打印。。。。。。            "1:\t"                  /* next process start here */            "popl %%ebp\n\t"  //ebp又回到了一开始的地方,esp=ebp。感觉还差一步,栈都空了,sp怎么还不归位??            : "=m" (prev->thread.sp),"=m" (prev->thread.ip)            : "m" (next->thread.sp),"m" (next->thread.ip)        );         my_current_task = next;         printk(KERN_NOTICE ">>>switch process %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 */        //记录好前一个进程的栈底信息,并将sp移动到“进程prev”的堆栈顶端            "movl %2,%%esp\n\t"     /* restore  esp */            "movl %2,%%ebp\n\t"     /* restore  ebp */        //esp、ebp跳到下一个进程去,并且“进程next”的堆栈当前为空            "movl $1f,%1\n\t"       /* save eip */ //将前一个进程的ip记录为flag,然而不太明白,flag究竟怎么用            "pushl %3\n\t"                "ret\n\t"               /* restore  eip */  //这两句等效为:设置eip的值为“进程next”的ip值            : "=m" (prev->thread.sp),"=m" (prev->thread.ip)            : "m" (next->thread.sp),"m" (next->thread.ip)        );              }       return; }

myinterrupt.c 描述了进程切换时寄存器的具体操作。每一步的分析在注释中(不一定正确,求指正)。每一个进程都对应着自己独立的堆栈空间。当一个正在运行的进程被打断时,需要做一下现场处理,把“进程命令执行到哪儿”“入口在哪儿”“栈底在哪儿”等信息存入自己的堆栈中,方便回来时继续处理。如图:
这里写图片描述

三、思考
1、操作系统是这样调度的
保存当前pcb的ebp,esp,把下一个进程的esp赋予esp寄存器,把下一个进程的eip赋给pc寄存器(不能直接赋值,需要一定的处理,即push加ret)来实现中断处理和进程切换。
2、疑问
这次课有点晕,自己画的图也不知道正确与否,还有几个问题:
1)mymain.c中:

    task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process; 

task_entry 和 ip 意义一样???为什么用两个?void类型怎么强转??入口究竟是个什么东西??
2)myinterrupt.c中:

void my_timer_handler(void)  

哪里会调用到这个函数???系统是怎么识别的??不懂
3)flag究竟怎么用?类似C中的goto吗?

参考资料:http://blog.csdn.net/u010634758/article/details/50810986
http://www.cnblogs.com/hyq20135317/p/5243754.html

0 0
原创粉丝点击