zoukankan      html  css  js  c++  java
  • 2020-2021-1 20209308《Linux内核原理与分析》第三周作业

    Linux 基础

    实验二

    一.实验过程

    1.执行原始内核程序
    

    2.执行时间片轮转内核程序
    


    二.实验代码分析

    1.代码过程
    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 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);
    
    

    在mypcb.h中,定义了一个结构体Thread,其中有ip,sp两个变量。结构PCB是进程管理块,包括进程的标识符pid,进程状态state, stack,thread,入口函数task_entry,一个next指针。
    pid:进程号
    state:进程状态,在模拟系统中,所有进程控制块信息都会被创建出来,其初始化值就是-1,如果被调度运行起来,其值就会变成0
    stack:进程使用的堆栈
    thread:当前正在执行的线程信息
    task_entry:进程入口函数
    next:指向下一个PCB,模拟系统中所有的PCB是以链表的形式组织起来的。

    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;
    		/* 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
    	" 	/* set task[pid].thread.sp to esp */
    			"pushl %1
    	" 	        /* push ebp */
    			"pushl %0
    	" 	        /* push task[pid].thread.ip */
    			"ret
    	" 	            /* pop task[pid].thread.ip to eip */
    			"popl %%ebp
    	"
    			: 
    			: "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 -
    ",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);
    			}     
    		}
    	}
    

    my_start_kernel函数:
    1.初始化进程0,使pcb的指针指向进程0
    2.初始化其余的进程
    3.开始运行0号进程
    (从第一个进程开始)
    my_process函数:循环并输出语句以及切换进程。

    myinterrupt.c

    #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.
     * it runs in the name of current running process,
     * so it use kernel stack of current running process
     */
    void my_timer_handler(void)
    {
    #if 1
        if(time_count%1000 == 0 && my_need_sched != 1)
        {
            printk(KERN_NOTICE ">>>my_timer_handler here<<<
    ");
            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<<<
    ");
        /* schedule */
        next = my_current_task->next;
        prev = my_current_task;
        if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */
        {        
            my_current_task = next; 
            printk(KERN_NOTICE ">>>switch %d to %d<<<
    ",prev->pid,next->pid);  
            /* switch to next process */
            asm volatile(   
                "pushl %%ebp
    	"       /* save ebp */
                "movl %%esp,%0
    	"     /* save esp */
                "movl %2,%%esp
    	"     /* restore  esp */
                "movl $1f,%1
    	"       /* save eip */ 
                "pushl %3
    	" 
                "ret
    	"               /* restore  eip */
                "1:	"                  /* next process start here */
                "popl %%ebp
    	"
                : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
                : "m" (next->thread.sp),"m" (next->thread.ip)
            ); 
        }  
        return; 
    }
    

    my_time_handler函数:用于每1000毫秒产生一次中断,发生中断时,将my_need_sched = 1并输出语句,my_need_sched用于mymain.c中进程的切换。
    1.初始化两个pcb结构next,prev
    2.判断下一个进程的state状态,为0则切换到下一个进程。
    3若不为0,执行新的进程。
    以上函数实现了简单的时间片轮转算法,给每个进程分配一个时间片,时间片结束后运行下一个进程,而当进程在其所在时间片结束前就结束或者阻塞时,立即运行新的进程,这就是时间片轮转算法的原理。


    三.遇到的问题

    • 进程的启动与切换机制:

      • 操作系统对进程的调度主要是通过控制块PCB,在内核启动时,init_start_kernel()函数生成若干进程,再初始化第一个进程,进入各自进程后再根据schedule函数判定进程的运行状态,并且根据此来调度进程,这个状态由timer_handler函数根据计时情况进行设置,一旦时间满足便产生中断,运行下一进程,如果下一个进程的状态不为可执行,那么将其设置为可执行状态准备接受调度,保存自己的当前执行环境并移至进程队列尾部并返回。然后0号进程调度进程队列中的可执行进程执行。
    • 进程调度的时机:

      • 中断处理过程(包括时钟中断、I/O中断、系统调用和异常)中,直接调用schedule(),或者返回用户态时根据need_resched标记调用schedule();
      • 内核线程可以直接调用schedule()进行进程切换,也可以在中断处理过程中进行调度,也就是说内核线程作为一类的特殊的进程可以主动调度,也可以被动调度;
      • 用户态进程无法实现主动调度,仅能通过陷入内核态后的某个时机点进行调度,即在中断处理过程中进行调度。
  • 相关阅读:
    eclipse中的项目如何打成war包
    【SVN】Please execute the 'Cleanup' command.
    2021.06.02模拟赛DP2
    2021.05.26模拟赛 DP1
    状压DP
    高斯消元
    矩阵快速幂
    2021.05.10讲题
    Luogu P2152[SDOI 2009]Super GCD
    Tarjan
  • 原文地址:https://www.cnblogs.com/ppswaggy/p/13869382.html
Copyright © 2011-2022 走看看