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

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

 

本次实现是实验模拟操作系统是如何工作的。 mykernel 实验使用linux-3.9.4进行模拟。模拟操作系统如何进行进程管理以及中断处理。

Process Control Block 即PCB(进程控制块)

PCB由操作系统创建和管理。同时PCB包含了足够充分的信息,这样就可以中断一个进程,并且在恢复执行该进程时就好像未被中断过一样。(《Operating Systems Internals and Design Principles》)

在mykernel中mypcb.h既是声明PCB数据结构头的文件。

PCB中定义了pid,state,stack,thread,task_entry,*next结构成员。

在定义了PCB类型的数据结构后,定义了实例变量tPCB.

在声明了PCB数据结构类型之后,Linux内核开始运作。

实验代码:

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 没有运行, 0 已运行, >0 停止*/
    char stack[KERNEL_STACK_SIZE];
    /* CPU-specific state of this task */
    struct Thread thread;
    unsigned long    task_entry; /*保存进程的入口函数地址*/
    struct PCB *next; /*指向下一个PCB struct*/
}tPCB;

void my_schedule(void);

mymain.c

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*/
    task[pid].pid = pid;
    task[pid].state = 0;/* -1 没运行, 0 已运行, >0 停止*/
    task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process;/*把第一个进程的入口定义为my_process*/
    task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1];/*获取进程栈的基地址*/
    task[pid].next = &task[pid];
    /*循环建立多个进程 */
    for(i=1;i<MAX_TASK_NUM;i++)
    {
        memcpy(&task[i],&task[0],sizeof(tPCB));/*把进程0的数据拷贝给进程i,意味着进程i的入口函数也是my_process*/
        task[i].pid = i;
        task[i].state = -1;/*设置进程i的状态为未运行*/
        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];/*以上两句是把新的task加到链表后面,并且让最后一个task的next指向第一个task 形成回路链表*/
    }
    /*从第0个进程开始*/
    pid = 0;
    my_current_task = &task[pid];/*设置当前进程的task指针赋给  my_current_task*/
   /*一下的asm中的内容用汇编代码编写 主要是实现调用第一个进程初始化堆栈和eip*/
    asm volatile(
        "movl %1,%%esp\n\t"     /* 把task[pid].thread.sp赋予esp寄存器 */
        "pushl %1\n\t"             /*  把task[pid].thread.sp压到栈中,以便以后使用*/
        "pushl %0\n\t"             /* 把task[pid].thread.ip压到栈中 */
        "ret\n\t"                 /* 把task[pid].thread.ip出栈 赋予eip 以实现设置eip的效果 因为eip不能直接赋值 */
        "popl %%ebp\n\t"/*把task[pid].thread.sp出栈,赋予ebp*/
        : 
        : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)    /*  c , d 的意思是 %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)/*1的意思是需要调度,0是不需要调度*/
            {
                my_need_sched = 0;
                my_schedule();/*调度函数*/
            }
            printk(KERN_NOTICE "this is process %d +\n",my_current_task->pid);
        }     
    }
}

myinterrupt.c

extern tPCB task[MAX_TASK_NUM];
extern tPCB * my_current_task;
extern volatile int my_need_sched;
volatile int time_count = 0;

void my_timer_handler(void)
{
#if 1
    if(time_count%1000 == 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)/* 判断下个进程是不是运行过 */
    {
        /* 当下一个进程运行过时,使用下面asm中的汇编代码就可以把进程切换到下一个进程 */
        asm volatile(    
            "pushl %%ebp\n\t"         /* 把当前进程的当前的ebp压入当前进程的栈 */
            "movl %%esp,%0\n\t"     /*保存当前的esp到prev->thread.sp指向的内存中*/
            "movl %2,%%esp\n\t"     /*重置esp,把下个进程的next->thread.sp赋予esp */
            "movl $1f,%1\n\t"       /* 把1:的代码在内存中存储的地址保存到prev->thread.ip中 */    
            "pushl %3\n\t" 
            "ret\n\t"                 /* 这两步是为了把eip设置成下一个进程的eip,以便cpu运行下一个进程的代码 */
            "1:\t"         /*当前进程下一次从这里开始,*/        
            "popl %%ebp\n\t"
            : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
            : "m" (next->thread.sp),"m" (next->thread.ip)
        ); 
        /*程序运行到这时当前的进程已经以前被运行过,现在是第N次运行N>1*/
        my_current_task = next; /*这里的my_current_task原值指向的是上一个进程,next指向的是当前运行的进程*/
        printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);       /*这里的prev指向的是上一个进程*/
    }
    else
    {
        next->state = 0;
        my_current_task = next;
        printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);
        /*以下的asm 实现的效果是切换到新的进程 */
        asm volatile(    
            "pushl %%ebp\n\t"            /* 把当前进程的当前的ebp压入当前进程的栈 */
            "movl %%esp,%0\n\t"     /*保存当前的esp到prev->thread.sp指向的内存中*/
            "movl %2,%%esp\n\t"      /*重置esp,把下个进程的next->thread.sp赋予esp */
            "movl %2,%%ebp\n\t"     /*重置esp,把下个进程的next->thread.sp赋予ebp,因为是新的进程需要模拟新的堆栈 */
            "movl $1f,%1\n\t"       /* 把1:的代码在内存中存储的地址保存到prev->thread.ip中 */
            "pushl %3\n\t" 
            "ret\n\t"                /* 这两步是为了把eip设置成下一个进程的eip,以便cpu运行下一个进程的代码 */
            : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
            : "m" (next->thread.sp),"m" (next->thread.ip)
        );          
    }   
    return;    
}

 

可以说进程是由程序代码和相关数据还有PCB组成的。对于一个但处理器计算机,在任何时候最多只有一个进程在执行。而在运行的这个进程状态为运行态。

 

posted on 2016-03-04 08:52  20135230潘俊洋  阅读(406)  评论(0编辑  收藏  举报