• 2017-2018-1 20179215《Linux内核原理与分析》第三周作业


    本次作业分为两部分:第一部分为实验。主要目的是进行基于MYKERNEL的一个简单的时间片轮转多道程序内核代码分析。第二部分为阅读教材,了解LINUX进程调度等。

    一、实验部分

    实验过程如过程所述:使用实验楼的虚拟机打开shell,输入下面的两条命令,即可以启动mykernel:

     cd LinuxKernel/linux-3.9.4
     qemu -kernel arch/x86/boot/bzImage
    

    实验截图如下:

    在QEMU窗口,我们可以看到一个简单的操作系统已经跑起来了,当然这个系统很简单,只是不停的输出一些字符串:>>>>>my_timer_handler here <<<<< 和 *my_start_kernel here *。然后关闭QEMU窗口,cd mykernel ,我们可以找到输出这些字符串的源代码mymain.c和myinterrupt.c打开这两个文件,如实验截图所示:

    查看mymain.c和myinterrupt.c代码:

    我们可以看到,在mymain.c的my_start_kernel函数中有一个循环,不停的输出 my_start_kernel here。

    在myinterrupt.c中,可以看到一个会被时钟中断周期调用的函数my_timer_handler ,在这个函数里,每次调用都会输出类似>>>>>my_timer_handler here <<<<< 的字符串。

    从上面我们可以知道:my_start_kernel()是操作系统的入口,myinterrupt.c中可以完成中断程序调用,即可完成进程的上下文切换。

    下面我们扩展了my_start_kernel和my_timer_handler函数,模拟了一个基于时间片轮转的多道程序。包含三个文件:mymain.c,myinterrupt.c,mypcb.h,具体代码如下:

    mypcb.h代码如下:

    #define MAX_TASK_NUM4
    #define KERNEL_STACK_SIZE   1024*2 # unsigned long
    /* CPU-specific state of this task */
    struct Thread {
    unsigned long		ip;
    unsigned long		sp;
    };
    
    typedef struct PCB{
    int pid;
    volatile long state;	/* -1 unrunnable, 0runnable, >0 stopped */
    unsigned long 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);
    

    这段代码主要进行了进程控制块PCB结构体的定义,包括:

    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].stack[KERNEL_STACK_SIZE-1] - 1) = (unsigned long)&task[i].stack[KERNEL_STACK_SIZE-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 */
    	: 
    	: "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);
        }     
    }
    }
    

    这里的函数 my_start_kernel是系统启动后,最先调用的函数,在这个函数里完成了0号进程的初始化和启动,并创建了其它的进程PCB,以方便后面的调度。在模拟系统里,每个进程的函数代码都是一样的,即 my_process函数,my_process函数在执行的时候会打印出当前进程的id号,方便我们知道是哪个进程在运行。而且在该函数中还定义了my need sched变量,若它的值为1,就调用my schedule()来完成进程的调度

    0号线程的启动,采用了内联汇编代码完成:

    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*/  
    );  
    

    由于开始栈为空,所以esp,ebp指向同一位置,之后esp,eip依次压栈,pop eip进程0开始启动,之后清空栈,指针esp,ebp又同时指向栈顶(也是栈底,空栈)。

    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;	
    }
    

    myinterrupt.c同样包含my_timer_handler和my_schedule两个函数。 my_timer_handler每隔1000次将my_need_sched置1,调用进程的调度函数。 my_schedule保存恢复进程上下文。

    二、课本部分

    1、什么是进程呢?

    通俗的来说进程是运行起来的程序。唯一标示进程的是进程描述符(PID),在linux内核中是通过task_struck和task_list来定义和管理进程的。
    

    2、进程的分类

    1)根据在linux不同模式下运行分为:

    核心态:这类进程运行在内核模式下,执行一些内核指令(Ring 0)。
    
    用户态:这类进程工作在用户模式下,执行用户指令(Ring 3)。
    
    如果用户态的进程要执行一些核心态的指令,此时就会产生系统调用,系统调用会请求内核指令完成相关的请求,就执行的结果返回给用户态进程。
    

    2)按照进程的状态可分为:

    运行态:running 正在运行的进程
    
    可中断睡眠态:进程处于睡眠状态,但是可以被中断
    
    不可中断的睡眠态:进程处于睡眠状态,但是不可以被中断
    
    停止态:stoped 不会被内核调度
    
    僵死态:zombie产生的原因是进程结束后,它的父进程没有wait它,所导致的。
    

    3)按照操作的密集程度

    CPU密集型:进程在运行时,占用CPU时间较多的进程。
    
    I/O密集型:进程在运行时,占用I/O时间较多的进程。
    
    通常情况下,I/O密集型的优先级要高于CPU密集型。
    

    4)按照进程的处理方式

    批处理进程:
    
    交互式进程:
    
    实时进程:
    

    3、进程的优先级

    进程的有优先级,是用0-139数字来表示的,数字优先级从小到大依次是:0-99,139-100。
    
    优先级分为2类:
    
    实时优先级:0-99,是由内核维护的
    
    静态优先级:100-139,可以使用nice来调整,nice值的取值范围是[-20,19),分别对应100到139。nice默认值是0。
    
    动态优先级:由内核动态维护,动态调整。 
    

    4、Linux操作系统包括如下三种不同类型的进程,每种进程都有其自己的特点和属性:

    交互进程:由一个shell启动的进程。交互进程既可以在前台运行,也可以在后台运行。
    批处理进程:这种进程和终端没有联系,是一个进程序列。
    守护进程:Linux系统启动时启动的进程,并在后台运行。
    

    上述三种进程各有各的作用,使用场合也有所不同。Linux系统提供了who、w、ps和top等察看进程信息的系统调用,通过结合使用这些系统调用,我们可以清晰地了解进程的运行状态以及存活情况,从而采取相应的措施,来确保Linux系统的安全。

    5、进程创建及销毁

    在linux系统中,通常通过调用fork()系统创建进程。该系统通过复制一个现有进程来创建一个全新的进程。调用fork()的进程为父进程,新产生的进程为子进程。通常新的进程都是为了立即执行新的、不同的程序,而接着调用exec()这组函数就可以创建新的地址空间,并把新的程序载入其中。最终程序通过exit()系统调用退出执行。无论在程序中的什么位置,只要执行到exit系统调用,进程就会停止剩下的所有操作,清除包括PCB在内的各种数据结构,并终止本进程的运行。

  • 相关阅读:
    常见压缩/解压缩及打包命令
    黑盒测试和白盒测试
    基础命令的操作

    转]DLL-多个进程间共享动态链接库的原理
    Ansi UNICODE,GBK,UTF-8区别
    Cppunit 源码 SynchronizedObject
    二分查找实现
    Androdi 开发学习四 Activity和Intent
    Android开发学习三:adb启动失败
  • 原文地址:https://www.cnblogs.com/yl-930/p/7674781.html
Copyright © 2020-2023  润新知