Linux内核分析课程2_操作系统是如何工作的

来源:互联网 发布:java正则去掉html标签 编辑:程序博客网 时间:2024/05/16 17:39

  Linux内核课第二作业。本文在Ubuntu 12.04中完成

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

--------------------------------------------------------分割线----------------------------------------------------------------------------------------------------


一.计算机是如何工作的(小结)

  计算机的工作,一言以蔽之:执行程序的过程;也就是存储程序和程序控制的过程。

  • 存储程序计算机工作模型,计算机系统最最基础性的逻辑结构;

  • 函数调用堆栈,高级语言得以运行的基础;

  • 中断,多道程序操作系统的基点。

二.以例分析

1.一个简单的中断的例子(点击进入)

简要分析mymain.c与myinterrupt.c

  1. void __init my_start_kernel(void)    //mymain.c中主要内容
  2. {
  3.     int i = 0;
  4.     while(1)
  5.     {
  6.         i++;
  7.         if(i%100000 == 0) //每循环十万次打印一次my_start_kernel here
  8.             printk(KERN_NOTICE "my_start_kernel here  %d \n",i);
  9.     }
  10. }
  1. void my_timer_handler(void) //每次时钟中断调用一次    myinterrupt.c中主要内容
  2. {
  3.     printk(KERN_NOTICE "\n>>>>>>>>>>>>>>>>>my_timer_handler here<<<<<<<<<<<<<<<<<<\n\n");
  4. }

  可见,这只是一个很简单的时钟中断演示实验,执行结果如下所示:

  可以清楚的看到,时钟每记数到十万的时候,打印一个my_start_kernel here,时钟中断的时候执行my_time_hander here.

 

2.在第一个的基础上进行时间片轮转多道程序的小os.

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

/ * 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 */

  1. struct Thread {//给任务定义一个eip和esp
  2.     unsigned longip;
  3.     unsigned longsp;
  4. };

  5. typedef struct PCB{
  6.     int pid;//任务编号
  7.     volatile long state;/* -1 unrunnable, 0 runnable, >0 stopped */
  8.     char stack[KERNEL_STACK_SIZE];     //定义栈空间
  9.     /* CPU-specific state of this task */
  10.     struct Thread thread;       //定义进程的结构体thread, 其中有eip和esp
  11.     unsigned longtask_entry;//任务的函数起始处, 也就是任务第一次执行的起始位置
  12.     struct PCB *next;//一个任务链表, 指向下一个任务
  13. }tPCB;

  14. void my_schedule(void);//任务调动函数

 

/*  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"    //引入其中两个结构体表示

  1. tPCB task[MAX_TASK_NUM];//定义两个数组
  2. tPCB * my_current_task = NULL;
  3. volatile int my_need_sched = 0;//定义是否调度, 1则调度, 0则不调度
  4. void my_process(void);
  5. void __init my_start_kernel(void)    //起始函数位置
  6. {
  7.     int pid = 0;
  8.     int i;
  9.     /* Initialize process 0*/
  10.     task[pid].pid = pid;
  11.     task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped */
  12.     task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process;
  13.     task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1];   //0号进程栈在最开始的位置
  14.     task[pid].next = &task[pid];//下一个任务也是自己,在这里,其他任务还没有创建
  15.     
  16.     /*fork more process *///创建多个任务
  17.     for(i=1;i<MAX_TASK_NUM;i++)
  18.     {
  19.         memcpy(&task[i],&task[0],sizeof(tPCB));//复制0号进程的结构形式
  20.         task[i].pid = i;
  21.         task[i].state = -1;//初始的任务(除0号进程外)都设置成未运行
  22.         task[i].thread.sp = (unsigned long)&task[i].stack[KERNEL_STACK_SIZE-1];
  23.         task[i].next = task[i-1].next;//新fork的进程加到进程链表的尾部,  该新建任务的next指向上一个任务的next,也就是自己(最后一个)
  24.         task[i-1].next = &task[i];  //配置上一个任务的next指向这时候新创建的任务
  25.     }
  26.     /* start process 0 by task[0] */
  27.     pid = 0;
  28.     my_current_task = &task[pid];//先让0号进程先执行
  29. asm volatile(
  30.      "movl %1,%%esp\n\t" /* set task[pid].thread.sp to esp */
  31.      "pushl %1\n\t"        /* push ebp ,当前esp=ebp*/
  32.      "pushl %0\n\t"        /* push task[pid].thread.ip */
  33.      "ret\n\t"            /* pop task[pid].thread.ip to eip */
  34.      "popl %%ebp\n\t"
  35.     
  36.      : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)/* input c or d mean %ecx/%edx*/
  37. );
  38. }   
  39. void my_process(void)
  40. {
  41.     int i = 0;
  42.     while(1)
  43.     {
  44.         i++;
  45.         if(i%10000000 == 0)
  46.         {
  47.             printk(KERN_NOTICE "this is process %d -\n",my_current_task->pid);
  48.             if(my_need_sched == 1)//判断是否调度;该值可有itnerrupt.c中的函数来配置
  49.             {
  50.                 my_need_sched = 0;
  51.            my_schedule();//主动调动的机制
  52.         }
  53.         printk(KERN_NOTICE "this is process %d +\n",my_current_task->pid);
  54.         }     
  55.     }
  56. }

 

/*  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

 */

  1. void my_timer_handler(void)
  2. {
  3. #if 1
  4.     if(time_count%1000 == 0 && my_need_sched != 1)//时钟中断1000次的时候,调度一次, 配置调度值为1
  5.     {
  6.         printk(KERN_NOTICE ">>>my_timer_handler here<<<\n");
  7.         my_need_sched = 1;
  8.     } 
  9.     time_count ++ ;  
  10. #endif
  11.     return;  
  12. }
  13. void my_schedule(void)     //调度函数, 核心函数
  14. {
  15.     tPCB * next;//定义两个指针
  16.     tPCB * prev;
  17.     if(my_current_task == NULL //当前进程和下一进程为空, 即没有任务, 返回
  18.         || my_current_task->next == NULL)
  19.     {
  20.      return;
  21.     }
  22.     printk(KERN_NOTICE ">>>my_schedule<<<\n");
  23.     /* 在调度函数中, next指向的是下一个将要被调度的任务, prev指向的是当前正在运行的任务*/
  24.     /* schedule */
  25.     next = my_current_task->next;//把当前进程的下一个进程赋值给next,当前进程赋值给prev
  26.     prev = my_current_task;
  27.     if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */  //如果下一个任务不是第一次被调度, 则执行
  28.     {
  29.      /* switch to next process——这个时候下一个进程有进程上下文 */
  30.      asm volatile(
  31.         "pushl %%ebp\n\t"   /* save 当前进程 ebp */
  32.         "movl %%esp,%0\n\t"   /* save 当前 esp 赋值到prev.thread.sp */
  33.         "movl %2,%%esp\n\t"     /* restore 下一个进程的sp到 esp */
  34.         "movl $1f,%1\n\t"           /* save 当前进程的 eip */
  35.         "pushl %3\n\t"//保存下一个进程eip保存到栈里面
  36.         "ret\n\t"                /* restore  eip */
  37.         "1:\t"                  /* next process start here */
  38.         "popl %%ebp\n\t"
  39.         : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
  40.         : "m" (next->thread.sp),"m" (next->thread.ip)
  41.      ); 
  42.      my_current_task = next; 
  43.      printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);  
  44.     }
  45.     else//下一个进程为第一次运行时,没有进程上下文, 则以下面这种方式来处理
  46.     {
  47.         next->state = 0;
  48.         my_current_task = next;
  49.         printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);
  50.      /* switch to new process */
  51.     asm volatile(
  52.         "pushl %%ebp\n\t"   /* save ebp */
  53.         "movl %%esp,%0\n\t"/* save esp */x`
  54.         "movl %2,%%esp\n\t"     /* restore  esp */
  55.         "movl %2,%%ebp\n\t"     /* restore  ebp */
  56.         "movl $1f,%1\n\t"       /* save eip */
  57.         "pushl %3\n\t" 
  58.         "ret\n\t"           /* restore  eip */
  59.         : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
  60.         : "m" (next->thread.sp),"m" (next->thread.ip)
  61.      );          
  62.     }   
  63.     return;
  64. }
  以新任务切换为例进行堆栈变化分析:
 
  执行结果如下图所示:

 

0 0
原创粉丝点击