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

在实验楼上按步骤

cd LinuxKernel/linux-3.9.4
rm -rf mykernel
patch -p1 < ../mykernel_for_linux3.9.4sc.patch
make allnoconfig
make #编译内核请耐心等待
qemu -kernel arch/x86/boot/bzImage
copy

可获得一个不断打印my_start_kernel here %d的运行结果。 进入到mykernel下新建文件mypcb.h定义进程结构或者点mykernel下载

#define MAX_TASK_NUM 10 // max num of task in system
#define KERNEL_STACK_SIZE 1024*8

/* CPU-specific state of this stask */
struct Thread {
    unsigned long ip;//point to cpu run address
    unsigned long sp;//point to the thread statck's top address
};

//PCB Struct
typedef struct PCB{
    int pid; //pcb id
    volatile long state; 
    char stack[KERNEL_STACK_SIZE];// each pcb stack is 1024*8
    /* CPU-specific state of this task */
    struct Thread thread;
    unsigned long task_entry;//the task execute entry memory address
    struct PCB *next;//pcb is a circular linked list
}tPCB;

void my_schedule(void)
copy

修改之前的mymain.c

//之前引入的同文件不变
#include "mypcb.h"

tPCB task[MAX_TASK_NUM];
tPCB * my_current_task = NULL;

void my_process(void);

void __init my_start_kernel(void)
{
    int pid = 0;
    /* Initialize process 0 */
    task[pid].pid = pid;
    task[pid].state = 0;
    //set task 0 execute entry to my_process
    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(pid=1;pid<MAX_TASK_NUM;pid++)
    {
        memcpy(&task[pid],&task[0],sizeof(tPCB));
        task[pid].pid = pid;
        task[pid].state = -1;
        task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1];
    }
    task[MAX_TASK_NUM-1].next = &task[0];
    printk(KERN_NOTICE "\n\n\n\n\n\n            system begin :>>> process 0 runing!!1  <<\n\n")
    /* 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)
    );
}
void my_process(void)
{
    int i = 0;
    while(1){
        i++;
        if(i%100000000 ==0)
        {
            if(my_need_sched == 1){
                my_need_sched =0;
                my_schedule();
            }
        }
    }
}
copy

修改myinterrupt.c

//其他头文件不动加入
#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)
{

    //make sure need schedule after system circle 2000 times.
    if(time_count%2000 == 0 && my_need_sched !=1)
    {
        my_need_sched = 1;
    }
    time_count++;
    return;
}

void my_schedule(void)
{
    tPCB * next;
    tPCB * prev;
    int n;
    // if there no task running or only a task ,it shouldn't need schedule
    if(my_current_task == NULL || my_current_task->next ==NULL)
    {
    printk(KERN_NOTICE "            time out!!!but no more than 2 task,need no schedule\n");
    return;
    }
    /* schedule */
    
    //next = my_current_task->next;
    prev = my_current_task;
    n = prev->pid + 1
    if(n>=MAX_TASK_NUM)
        next = &task[0]
    else next = &task[n]
    printk(KERN_NOTICE "          the next task is %d\n",next->pid);
    if(next->state == 0)
    {
     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 from %d process to %d process\n                >>>process %d running!!!<<<\n\n",prev->pid,next->pid,next->pid);
    }
    else{
        next->state = 0;
        my_current_task = next;
        printk(KERN_NOTICE "                switch from %d process to %d process\n                >>>process %d running!!!<<<\n\n\n",prev->pid,next->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;
}
copy

去掉了优先级,去掉了输出。 实验楼 实验楼

最新评论
暂无评论~