《Linux内核分析》第二周:操作系统是如何工作的

杨舒雯 20135324 北京电子科技学院
杨舒雯 原创作品转载请注明出处
《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000

chapter1 知识点

一、三个法宝

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

②函数调用堆栈,高级语言得以运行的基础,只有机器语言和汇编语言的时候堆栈机制对于计算机来说并不那么重要,但有了高级语言及函数,堆栈成为了计算机的基础功能;

③中断,多道程序操作系统的基点,没有中断机制程序只能从头一直运行结束才有可能开始运行其他程序。

二、

chapter2 实验

使用实验楼的虚拟机打开shell
cd LinuxKernel/linux-3.9.4
qemu -kernel arch/x86/boot/bzImage

然后cd mykernel 您可以看到qemu窗口输出的内容的代码mymain.c和myinterrupt.c

使用自己的Linux系统环境搭建过程参见mykernel,其中也可以找到一个简单的时间片轮转多道程序内核代码

实验要求:
•完成一个简单的时间片轮转多道程序内核代码,代码见视频中或从mykernel找。
•详细分析该精简内核的源代码并给出实验截图,撰写一篇署名博客,并在博客文章中注明“真实姓名(与最后申请证书的姓名务必一致) + 原创作品转载请注明出处 + 《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ”,博客内容的具体要求如下:◦题目自拟,内容围绕操作系统是如何工作的进行;
◦博客中需要使用实验截图
◦博客内容中需要仔细分析进程的启动和进程的切换机制
◦总结部分需要阐明自己对“操作系统是如何工作的”理解。

一、实验截图

二、代码分析

mypcb.h

#define MAX_TASK_NUM        4
#define KERNEL_STACK_SIZE   1024*8

/* CPU-specific state of this task */
struct Thread {
    unsigned long       ip;//用于eip的保存
    unsigned long       sp;//用于esp的保存
};

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);//调用了my_schedule,表示调度器#chaper3

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];
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;
    //定义进程0的入口为my_process
    task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1];
    task[pid].next = &task[pid];
    //因为一开始系统里只有进程0,所以这一行代码表示的是pid的next还是指向自己
    /*fork more process */
    //创建更多其他的进程,在初始化这些进程的时候可以直接拷贝0号进程的代码
    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;
    my_current_task = &task[pid];
    asm volatile(
        "movl %1,%%esp
	"     /* set task[pid].thread.sp to esp */
        "pushl %1
	"          /* push ebp */
        "pushl %0
	"          /* push task[pid].thread.ip */
        "ret
	"               /* pop task[pid].thread.ip to eip */
        "popl %%ebp
	"
        : 
        : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)   
        /* input c or d mean %ecx/%edx*/
    );
}
    /*  %0表示参数thread.ip,%1表示参数thread.sp。
        movl %1,%%esp表示把参数thread.sp放到esp中;
        接下来push %1,又因为当前栈为空,esp=ebp,所以等价于push ebp;
        然后push thread.ip;ret等价于pop thread.ip;最后pop ebp  */ 

void my_process(void)//定义所有进程的工作,if语句表示循环1000万次才有机会判断是否需要调度。
{
    int i = 0;
    while(1)
    {
        i++;
        if(i%10000000 == 0)
        {
              printk(KERN_NOTICE "this is process %d -
",my_current_task->pid);
            if(my_need_sched == 1)
            {
                my_need_sched = 0;
                my_schedule();
            }
            printk(KERN_NOTICE "this is process %d +
",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<<<
");
            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<<<
");
        /* schedule */
        next = my_current_task->next;
        prev = my_current_task;
        if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */
        {
            /* 进程切换跳转到下一进程 */
            asm volatile(   
                "pushl %%ebp
	"       /* 保存当前ebp */
                "movl %%esp,%0
	"     /* 保存当前esp */
                "movl %2,%%esp
	"     /* 重新记录要跳转进程的 esp,%2为 next->thread.sp*/
                "movl $1f,%1
	"       /* 保存当前 eip ,%1为prev->thread.ip*/   
                "pushl %3
	" 
                "ret
	"               /* 记录要跳转进程的  eip,%3为 next->thread.ip*/
                "1:	"                  /* 下一个进程开始执行 */
                "popl %%ebp
	"
                : "=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<<<
",prev->pid,next->pid);      
        }
        else
        {
            next->state = 0;
            my_current_task = next;
            printk(KERN_NOTICE ">>>switch %d to %d<<<
",prev->pid,next->pid);
            /* switch to new process */
            asm volatile(   
                "pushl %%ebp
	"       /* 保存当前 ebp */
                "movl %%esp,%0
	"     /* 保存当前 esp */
                "movl %2,%%esp
	"     /* 重新记录要跳转进程的 esp ,%2为 next->thread.sp*/
                "movl %2,%%ebp
	"     /* 重新记录要跳转进程的  ebp,%2为 next->thread.sp */
                "movl $1f,%1
	"       /* 保存当前  eip ,%1为prev->thread.ip,%1f就是指标号1:的代码在内存中存储的地址*/   
                "pushl %3
	" 
                "ret
	"               /* 重新记录要跳转进程的  eip,%3为 next->thread.ip */
                : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
                : "m" (next->thread.sp),"m" (next->thread.ip)
            );          
        }   
        return; 
    }

三、操作系统是如何工作的

操作系统工作的基础:存储程序计算机、堆栈机制、中断机制。操作系统是用户和计算机的接口,同时也是计算机硬件和其他软件的接口。操作系统的功能包括管理计算机系统的硬件、软件及数据资源,控制程序运行,改善人机界面,为其它应用软件提供支持,让计算机系统所有资源最大限度地发挥作用,提供各种形式的用户界面,使用户有一个好的工作环境,为其它软件的开发提供必要的服务和相应的接口等。

原文地址:https://www.cnblogs.com/yswysw/p/5242408.html