2_进程切换_一个简单的时间片轮转多道程序内核代码

本文介绍了在Linux环境下,使用C嵌汇编代码模拟时间片轮转的进程切换。通过分析和解释代码,展示了如何初始化进程控制块、如何执行进程间切换,以及如何利用中断进行调度。实验帮助理解操作系统中进程切换的基本原理,包括存储程序、函数调用堆栈和中断在多道程序操作系统中的作用。
摘要由CSDN通过智能技术生成

这周的实验还是在实验楼的linux环境中,用孟宁老师为我们搭建好的环境里用c嵌汇编的代码,模拟操作系统最基本的功能--进程间的切换。现在的操作系统都是多道程序运行,可以采用时间片轮转的方式进行。

首先进入实验楼环境(https://www.shiyanlou.com/courses/195)进入实验2后敲入:

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

可以看见如下图的运行效果:交替执行my_start_kernel和my_timer_handler。

然后ls查看到mykernel下有mymain.c   myinterrupt.c两个重要文件,如下图:

直接查看代码mymain.c:myinterrupt.c的代码:

可以看到这两个源文件里面很简单,各自就两个函数,直接输出打印内容。这说明,系统启动后从my_start_kernel函数执行,一段时间后(100000次)调用中断函数my_timer_handler执行,如此交替往复。

接下来,在上面的基础上更改mypcb.c mymain.c myinterrupt.c实现一个简单的进程间切换的多道程序。

从github.com/mengning/mykernel上下载源码后,敲入命令:

make allnoconfig   
make   
qemu -kernel arch/x86/boot/bzImage

运行如下图:

下面分析源码:

<span style="font-size:18px;">/*
 *  linux/mykernel/mypcb.h
 *
 *  Kernel internal PCB types
 *
 *  Copyright (C) 2013  Mengning
 *
 */
#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);</span>

分析:以上代码自定义了一个进程控制块的结构,其中

pid用来存放进程ID号;

state用来存放进程的状态:0表示可运行,大于0表示停止运行,-1表示不可运行;

stack数组用来存放内核栈;

thread是个自定义的结构体,里面存放两个关键的参数,一个是ip另一个是sp;

task_entry用来表示函数执行入口;

*next指向下一个进程控制块,形成链表结构。

函数My_schedule()的具体实现在myinterrupt.c中。

<span style="font-size:18px;">/*
 *  linux/mykernel/mymain.c
 *
 *  Kernel internal my_start_kernel
 *
 *  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"

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);
        }     
    }
}</span>

分析:以上代码做了4件事:

1.初始化一个0号进程。让它的next指向自己,构成循环链表

2.分配其他的子进程。先将0号进程memorycopy进数组,再根据MAX_TASK_NUM值初始化剩余的进程并放入数组中

3.让0号进程跑起来。通过内嵌的汇编代码切换eip就可以让0号进程运行

4.定义了所有进程都会执行的相同的函数体。所有进程都执行相同的操作,操作中会通过一个全局变量my_need_sched的值来判断是否执行函数my_schedule()。

<span style="font-size:18px;">/*
 *  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.
 * 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<<<\n");
        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<<<\n");
    /* 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; 
}</span>

分析:以上代码有两个函数:

函数my_timer_handler()由系统周期性调用,每1000次输出一次并且把在mymain.c中的函数my_process()里会用到的全局变量my_need_sched的值置为1。 

函数my_schedule()的功能就是进程间切换。先用2个指针变量prev和next指向当前进程和当前进程的下一个进程。然后逻辑处理分2类:

1.当切换的进程是已经挂起的进程时。   

    -将当前ebp压栈;

    -保存当前esp,要切换进程的esp放入cpu中的esp;

    -保存当前eip,要切换进程的eip弹入cpu中的eip;

    -弹出栈中ebp

此时切换进程已经开始运,最后将切换的进程记录为当前

2.当切换的进程是新的未曾创建的进程时。    

先将进程状态置为0表示已经已经运行。再将切换的进程记录为当前

    -将当前ebp压栈;

    -保存当前esp,要切换进程的esp放入cpu中的esp;

    -切换进程的esp放入cpu中的ebp;

    -保存当前eip,要切换进程的eip弹入cpu中的eip

此时切换进程已经开始运行

总结:通过本周对于精简的系统进程间切换实验的学习,用实际的c嵌汇编的代码具体分析了进程切换的过程,有助于对于更加复杂的linux系统的理解。对于计算机系统如何工作的主要3点:

1.存储程序,计算机系统最基本的特性;

2.函数调用堆栈,高级语言得以运行的基础;

3.中断,多道程序操作系统的基点。

对于操作系统如何工作的,在操作系统原理中描述为主要对进程管理、存储管理、I/O管理、文件管理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值