• 2019-2020-1 20209310《Linux内核原理与分析》第三周作业


    2019-2020-1 20209310《Linux内核原理与分析》第三周作业

    输入以下命令:

    rm -rf mykernel
    patch -p1 < ../mykernel_for_linux3.9.4sc.patch
    make allnoconfig
    make
    qemu -kernel arch/x86/boot/bzImage
    

    得到以下结果:

    1. mymain.c代码
    #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;
        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];
    
        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];
        }
        pid = 0;
        my_current_task = &task[pid];
        asm volatile(
            "movl %1,%%esp
    	"   
            "pushl %1
    	"          
            "pushl %0
    	"        
            "ret
    	"      
            "popl %%ebp
    	"
            : 
            : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)   
        );
    }
    
    
    void my_process(void)
    {
        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);
            }     
        }
    }
    
    1. myinterrput.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 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].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 rsp */
        	"pushl %1
    	" 	        /* push rbp */
        	"pushl %0
    	" 	        /* push task[pid].thread.ip */
        	"ret
    	" 	            /* pop task[pid].thread.ip to rip */
        	: 
        	: "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)	/* input c or d mean %ecx/%edx*/
    	);
    } 
    
    int i = 0;
    
    void my_process(void)
    {    
        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);
            }     
        }
    }
    

    3.mypcb.h
    用于存放头文件、PCB的定义和结构

    /* CPU-specific state of this task */
    struct Thread {
        unsigned long		ip;
        unsigned long		sp;
    };
    
    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);
    
  • 相关阅读:
    二分搜索
    Shell 字符串处理、获取文件名和后缀名
    sqlldr使用说明
    Linux cached过高问题
    算法时间复杂度
    #if,#ifdef,#ifndef的区别
    memcpy momove strcmp源码实现
    怎么解决/bin/sh: arm-linux-gcc: not found make
    性能文章
    linux
  • 原文地址:https://www.cnblogs.com/bestYZF/p/13875835.html
Copyright © 2020-2023  润新知