码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-07-08 09:37:17      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:内核   操作系统   调度   

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

先上代码:

  • myPCB.h

    /*
     *  linux/mykernel/mypcb.h
     *
     *  describe PCB
     *
     *  by Yuanhang Luo
     *
     */  
    
    
    #define MAX_TASK_NUM 4
    #define KERNEL_STACK_SIZE 1024*8
    
    struct Thread{
        unsigned long ip; /* save the state of ip */
        unsigned long sp; /* save the state of sp */
    };
    
    typedef struct PCB{
        int pid;
        volatile long state;
        char stack[KERNEL_STACK_SIZE];
        struct Thread thread;
        unsigned long task_entry;
        struct PCB *next;
    }tPCB;
    
    void my_schedule(void);
    
  • mymain.c

    /*
     *  linux/mykernel/mymain.c
     *
     *  Kernel internal my_start_kernel
     *
     *  by  Yuanhang Luo
     *
     */
    
    #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*/
        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];
        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[i-1].next = &task[i];
        }
        /* start process 0 by task[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();
            }
            printk(KERN_NOTICE "this is process %d +\n",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.
     */
    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 *prev;
        tPCB *next;
    
        if(my_current_task == NULL || my_current_task->next == NULL)
        {
            return;
        }
    
        printk(KERN_NOTICE ">>>MY 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 */
            "movl %%esp,%0\n\t"     /* save esp */
            "movl %2,%%esp\n\t"     /* restore  esp */
            "movl $1f,%1\n\t"       /* save eip */ 
            "pushl %3\n\t" 
            "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)
                ); 
                my_current_task = next; 
                printk(KERN_NOTICE ">>>switch %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 */
            "movl %2,%%esp\n\t"     /* restore  esp */
            "movl %2,%%ebp\n\t"     /* restore  ebp */
            "movl $1f,%1\n\t"       /* save eip */ 
            "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)
                );          
        }   
        return; 
    
    }
    

总结

  • CPU 和内核代码共同完成保存现场和恢复现场

  • 操作系统『两把剑』

    • 中断上下文切换
    • 进程上下文切换


原文地址:
http://blog.luoyuanhang.cn/2015/07/07/%E5%AE%8C%E6%88%90%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E6%97%B6%E9%97%B4%E7%89%87%E8%BD%AE%E8%BD%AC%E5%A4%9A%E9%81%93%E7%A8%8B%E5%BA%8F%E5%86%85%E6%A0%B8%E4%BB%A3%E7%A0%81/

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:内核   操作系统   调度   

原文地址:http://blog.csdn.net/luoyhang003/article/details/46795899

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!