【第22期】观点:IT 行业加班,到底有没有价值?

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

原创 2015年07月07日 23:26:47

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

先上代码:

  • 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.luoyuanhang.com) 举报

相关文章推荐

基于时间片的程序调度分析

时间片轮转

linux内核调度算法(2)--CPU时间片如何分配

内核在微观上,把CPU的运行时间分成许多分,然后安排给各个进程轮流运行,造成宏观上所有的进程仿佛同时在执行。双核CPU,实际上最多只能有两个进程在同时运行,大家在top、vmstat命令里看到的正在运...

程序员升职加薪指南!还缺一个“证”!

CSDN出品,立即查看!

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

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

操作系统——进程间通信和调度

  1.  进程间调度 <span style="font-

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

完成一个简单的时间片轮转多道程序内核代码重要汇编代码分析 asm volatile( "movl %1,%%esp\n\t" "pushl %1\n\t" ...

编写、编译、安装、移除一个内核模块的基本过程,不能再简单了的

1.gcc、make、kernel-devel的安装    可以执行rpm查看它们是否已经安装,如下: [root@localhost ~]# rpm -q gccgcc-4.1.0-3[root@localhost ~]# rpm -q makemake-3.80-...
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)