一个简单的时间片轮转多道程序内核操作系统工作流程

一.操作系统工作概述

  1. 存储程序计算机工作模型,计算机系统最最基础性的逻辑结构;
  2. 函数调用堆栈,高级语言得以执行的基础;
  3. 中断。多道程序操作系统的基点。

二.代码分析

在上一篇博文《搭建OS
kernel环境方法
》的基础上进行时间片轮转多道程序的小os.

主要对mypcb.h,  mymain.c 和myinterrupt.c这三个文件进行分析。

<pre name="code" class="cpp"><span style="font-size:12px;">//mypcb.h
</span>
<span style="font-size:12px;">#define MAX_TASK_NUM        4
#define KERNEL_STACK_SIZE   1024*8
/* CPU-specific state of this task */
struct Thread {//给任务定义一个eip和esp
    unsigned longip;
    unsigned longsp;
};
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;       //定义进程的结构体thread, 当中有eip和esp
    unsigned longtask_entry;//任务的函数起始处, 也就是任务第一次运行的起始位置
    struct PCB *next;//一个任务链表, 指向下一个任务
}tPCB;</span>

//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;//定义是否调度, 1则调度, 0则不调度
void my_process(void);
void __init my_start_kernel(void)    //起始函数位置
{
    int pid = 0;
    int i;
    <strong>/* Initialize process 0*/</strong>
    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];   <strong>//0号进程栈在最開始的位置</strong>
    task[pid].next = &task[pid];

   <strong> /*fork more process */</strong>
    for(i=1;i<MAX_TASK_NUM;i++)
    {
        memcpy(&task[i],&task[0],sizeof(tPCB));//复制0号进程的结构形式
        task[i].pid = i;
        task[i].state = -1;//初始的任务(除0号进程外)都设置成未运行
        task[i].thread.sp = (unsigned long)&task[i].stack[KERNEL_STACK_SIZE-1];
        task[i].next = task[i-1].next;<strong>//新fork的进程加到进程链表的尾部,  该新建任务的next指向上一个任务的next,也就是自己(最后一个)</strong>
        task[i-1].next = &task[i];  <strong>//配置上一个任务的next指向这时候新创建的任务</strong>
    }
    /* start process 0 by task[0] */
    pid = 0;
    my_current_task = &task[pid];//先让0号进程先运行
  <strong>  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 */
      "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*/
      );</strong>
}
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)//推断是否调度。该值可有itnerrupt.c中的函数来配置
            {
                my_need_sched = 0;
                my_schedule(); //主动调动的机制
           }
           printk(KERN_NOTICE "this is process %d +\n",my_current_task->pid);
        }
    }
}
//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%1000 == 0 && my_need_sched != 1)//时钟中断1000次的时候,调度一次, 配置调度值为1
    {
        printk(KERN_NOTICE ">>>my_timer_handler here<<<\n");
        my_need_sched = 1;
    }
    time_count ++ ;
#endif
    return;
}
void my_schedule(void)     //<span style="color:#ff0000;">调度函数, 核心函数</span>
{
    tPCB * next;//定义两个指针
    tPCB * prev;
    if(my_current_task == NULL //当前进程和下一进程为空, 即没有任务, 返回
        || my_current_task->next == NULL)
    {
      return;
    }
    printk(KERN_NOTICE ">>>my_schedule<<<\n");
    <strong><span style="color:#ff0000;">/* 在调度函数中, next指向的是下一个将要被调度的任务, prev指向的是当前正在运行的任务*/</span></strong>
    /* schedule */
    next = my_current_task->next;//把当前进程的下一个进程赋值给next。当前进程赋值给prev
    prev = my_current_task;
    if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */
    {   //<strong>假设下一个任务不是第一次被调度, 则运行,下一个进程<span style="color:#ff0000;">有进程上下文</span></strong>
      	/* switch to next process */
     	<span style="color:#ff0000;">asm volatile(
        	"pushl %%ebp\n\t"       /* save 当前进程 ebp */
       		"movl %%esp,%0\n\t"     /* save 当前 esp 赋值到prev.thread.sp */
        	"movl %2,%%esp\n\t"     /* restore 下一个进程的sp到 esp */
        	"movl $1f,%1\n\t"       /*<strong> save 当前进程的 eip =[ 1:]处地址,即下一次从[ 1:]处開始继续运行</strong> */

		/* 启动下一个进程*/
		"pushl %3\n\t"          /*保存下一个进程eip保存到栈里面*/
        	"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)
      ); </span>
      my_current_task = next;
      printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);
    }
    else
    {  <strong> //下一个进程为第一次运行时,<span style="color:#ff0000;">没有进程上下文</span>, 则以以下这样的方式来处理</strong>
        next->state = 0;
        my_current_task = next;
        printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);
        /* switch to new process */
    	<span style="color:#ff0000;">asm volatile(
        	"pushl %%ebp\n\t"       /* save ebp */
       		"movl %%esp,%0\n\t"     /* save esp */x`
        	"movl %2,%%esp\n\t"     /* restore  esp */
        	"movl %2,%%ebp\n\t"     /* restore  ebp */
        	"movl $1f,%1\n\t"       /*<strong> save 当前进程的 eip =[ 1:]处地址,即下一次从[ 1:]处開始继续运行</strong> */

		/* 启动下一个进程*/
        	"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)
          );          </span>
    }
    return;
}

借用还有一篇博文,以新任务切换为例进行堆栈变化分析:

author: 于凯

參考课程:《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000

时间: 2024-12-25 18:31:30

一个简单的时间片轮转多道程序内核操作系统工作流程的相关文章

对一个简单的时间片轮转多道程序内核代码的浅析

这周在网易云课堂上学习了<Linux内核分析>——操作系统是如何工作的.本周学习内容有利用 mykernel 实验模拟计算机平台和利用 mykernel 实验模拟计算机硬件平台两部分内容. 这是实验楼中 mykernel 平台运行的结果: 下面是一段一个简单的时间片轮转多道程序内核代码: 1 /* 2 * linux/mykernel/myinterrupt.c 3 * 4 * Kernel internal my_timer_handler 5 * 6 * Copyright (C) 201

Linux内核分析—完成一个简单的时间片轮转多道程序内核代码

---恢复内容开始--- 20135125陈智威 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” 实验要求: mykernel实验指导(操作系统是如何工作的) 运行并分析一个精简的操作系统内核,理解操作系统是如何工作的 使用实验楼的虚拟机打开shell cd LinuxKernel/linux-3.9.4 qemu -kernel arch/x86/boot/bzImage 然后cd

Linux内核分析:完成一个简单的时间片轮转多道程序内核代码

PS.贺邦   原创作品转载请注明出处  <Linux内核分析>MOOC课程    http://mooc.study.163.com/course/USTC-1000029000 1.mykernel实验指导(操作系统是如何工作的) 使用实验楼虚拟机打开shell输入下列代码 1 cd LinuxKernel/linux-3.9.4 2 qemu -kernel arch/x86/boot/bzImage 可以看到初始的内核运行情况如下: 内核不停的执行my_start_kernel(),每

完成一个简单的时间片轮转多道程序内核代码(二)

完成一个简单的时间片轮转多道程序内核代码 重要汇编代码分析 asm volatile( "movl %1,%%esp\n\t" "pushl %1\n\t" "pushl %0\n\t" "ret\n\t" "popl %%ebp\n\t" : : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp) ); 保存恢复进

基于mykernel的一个简单的时间片轮转多道程序内核代码分析

学号023作品 本实验资源来源: https://github.com/mengning/linuxkernel/ 一.观察简易操作系统 此处使用实验楼的虚拟机打开终端 cd LinuxKernel/linux-3.9.4 rm -rf mykernel patch -p1 < ../mykernel_for_linux3.9.4sc.patch make allnoconfig make #编译内核请耐心等待 qemu -kernel arch/x86/boot/bzImage 在QEMU窗口

一个简单的时间片轮转多道程序内核代码分析 (学号后三位418)

一.基于mykernel的基本Linux内核分析 1.我们按照老师在github上给出的步骤在实验楼上启动最高小内核,可以看到如下现象 在窗口中我们可以看到一个内核以及运行起来了,比较简单的内核,只时不停的输出一些字符串,>>>>>>my_time_handler here<<<<<<<和my_start_kernel here和一些计数.这时因为我们并没有加入其他的代码,再次基础上我们可以加入我们主机要实现的功能. 在myin

一个简单的时间片轮转多道程序内核

朱宇轲 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 本次课程老师为我们演示了一个简单时间片轮转多道程序内核代码,今天我们讲对它进行运行和分析. 实验截图 需要到github上下载mykernel的源代码并加载到Linux系统中.这里需要注意自己的Linux版本,我之前用的是CentOS,不能执行老师提供的命令,重装了ubuntu之后才终于解决了问题,神坑无比TAT 配置完环境

完成一个简单的时间片轮转多道程序内核代码

王康 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 " 分别是1 存储程序计算机工作模型,cpu执行程序的基础流程: 2 函数调用堆栈:各种寄存器和存储主要是为了指令的传取值,通过eip,esp,eax,ebp和程序内存的分区,搭配push pop call return leave等一系列指令完成函数调用操作. 3 中断:多道批程序! 在复习一下上一讲的几个重要指令

第二周:一个简单的时间片轮转多道程序内核代码及分析

吕松鸿+ 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.函数调用堆栈 1. 计算机工作的三个法宝 存储程序计算机工作模型,计算机系统最最基础性的逻辑结构: 函数调用堆栈,高级语言得以运行的基础,只有机器语言和汇编语言的时候堆栈机制对于计算机来说并不那么重要,但有了高级语言及函数,堆栈成为了计算机的基础功能: enter pushl %ebp movl %esp,%ebp lea