[自制操作系统] 第15回 实现内核线程

目录
一、前景回顾
二、线程的实现
三、线程的切换
四、运行测试

 

一、前景回顾

  上一回我们实现了内存管理系统,说实话代码还是比较多,看起来还是比较头疼的,不过为了知识这都是小事。这一节终于可以来实现我们的线程了,以前学操作系统的时候,听到的最多的就是什么线程,进程等,这一回我们来自己动手实现一下,加深对线程的理解。

二、线程的实现

  我相信能认真去看这篇博客的同学,不会是零基础。所以我也就不再深入地去讲解进程和线程的区别。这里我引入书中的话:

  线程是什么?具有能动性、执行力、独立的代码块。

  进程是什么?进程=线程+资源。

  先贴上代码,在project/kernel目录下新建list.c、list.h以及thread.c和thread.h文件。

  1 #include "list.h"
  2 #include "interrupt.h"
  3 #include "print.h"
  4 
  5 void list_init(struct list*list)
  6 {
  7     list->head.prev = NULL;
  8     list->head.next = &list->tail;
  9     list->tail.prev = &list->head;
 10     list->tail.next = NULL;
 11 }
 12 
 13 /*将链表元素elem插入到元素before之前*/
 14 void list_insert_before(struct list_elem *before, struct list_elem *elem)
 15 {
 16     enum intr_status old_state = intr_disable();
 17     before->prev->next = elem;
 18     elem->prev = before->prev;
 19     
 20     elem->next = before;
 21     before->prev = elem;
 22     intr_set_status(old_state);
 23 }
 24 
 25 /*添加元素到列表队首,类似栈push操作*/
 26 void list_push(struct list *plist, struct list_elem *elem)
 27 {
 28     list_insert_before(plist->head.next, elem);//在队头插入elem
 29 }
 30 
 31 /*追加元素到链表队尾,类似队列的先进先出*/
 32 void list_append(struct list *plist, struct list_elem *elem)
 33 {
 34     list_insert_before(&plist->tail, elem);
 35 }
 36 
 37 /*使元素pelem脱离链表*/
 38 void list_remove(struct list_elem *elem)
 39 {
 40     enum intr_status old_state = intr_disable();
 41     elem->prev->next = elem->next;
 42     elem->next->prev = elem->prev;
 43     intr_set_status(old_state);
 44 }
 45 
 46 
 47 /*将链表第一个元素弹出并返回,类似栈的pop操作*/
 48 struct list_elem *list_pop(struct list *plist) 
 49 {
 50     struct list_elem *elem = plist->head.next;
 51     list_remove(elem);
 52     return elem;
 53 }
 54 
 55 /*从链表中查找元素obj_elem,成功返回true,失败返回false*/
 56 bool elem_find(struct list *plist, struct list_elem *obj_elem)
 57 {
 58     struct list_elem *elem = plist->head.next;
 59     while (elem != &plist->tail) {
 60         if (elem == obj_elem) {
 61             return true;
 62         }
 63         elem = elem->next;
 64     }
 65     return false;
 66 }
 67 
 68 /*返回链表长度*/
 69 uint32_t list_len(struct list *plist)
 70 {
 71     struct list_elem *elem = plist->head.next;
 72     uint32_t length = 0;
 73     while (elem != &plist->tail) {
 74         length++;
 75         elem = elem->next;
 76     }
 77     return length;
 78 }
 79 
 80 /*判断链表是否为空,空时返回true,否则返回false*/
 81 bool list_empty(struct list *plist)
 82 {
 83     return (plist->head.next == &plist->tail ? true : false);
 84 }
 85 
 86 
 87 /*把列表plist中的每个元素elem和arg传给回调函数func*/
 88 struct list_elem *list_traversal(struct list *plist, function func, int arg)
 89 {
 90     struct list_elem *elem = plist->head.next;
 91     //如果队列为空,就必然没有符合条件的节点,直接返回NULL
 92     if (list_empty(plist)) {
 93         return NULL;
 94     }
 95 
 96     while (elem != &plist->tail) {
 97         if (func(elem, arg)) {
 98             return elem;
 99         }
100         elem = elem->next;
101     }
102     return NULL;
103 }
list.c
 1 #ifndef  __LIB_KERNEL_LIST_H
 2 #define  __LIB_KERNEL_LIST_H
 3 #include "stdint.h"
 4 
 5 #define offset(struct_type, member) (int)(&((struct_type *)0)->member)
 6 #define elem2entry(struct_type, struct_member_name, elem_ptr) \
 7         (struct_type *)((int)elem_ptr - offset(struct_type, struct_member_name))
 8 
 9 struct list_elem {
10     struct list_elem *prev;  //前驱节点
11     struct list_elem *next;  //后继节点
12 };
13 
14 struct list {
15    struct list_elem head;  
16    struct list_elem tail;
17 };
18 
19 typedef bool function(struct list_elem *, int arg);
20 
21 struct list_elem *list_traversal(struct list *plist, function func, int arg);
22 bool list_empty(struct list *plist);
23 uint32_t list_len(struct list *plist);
24 bool elem_find(struct list *plist, struct list_elem *obj_elem);
25 struct list_elem *list_pop(struct list *plist) ;
26 void list_remove(struct list_elem *elem);
27 void list_append(struct list *plist, struct list_elem *elem);
28 void list_push(struct list *plist, struct list_elem *elem);
29 void list_insert_before(struct list_elem *before, struct list_elem *elem);
30 void list_init(struct list*list);
31 
32 #endif
list.h
#include "thread.h"
#include "string.h"
#include "memory.h"
#include "list.h"
#include "interrupt.h"
#include "debug.h"
#include "print.h"
#include "stddef.h"


struct task_struct *main_thread;         //主线程PCB
struct list thread_ready_list;           //就绪队列
struct list thread_all_list;             //所有任务队列
static struct list_elem *thread_tag;     //用于保存队列中的线程节点
extern void switch_to(struct task_struct* cur, struct task_struct* next);


/*获取当前线程PCB指针*/
struct task_struct *running_thread(void)
{
    uint32_t esp;
    asm volatile ("mov %%esp, %0" : "=g" (esp));

    /*取esp整数部分,即PCB起始地址*/
    return (struct task_struct *)(esp & 0xfffff000);
}

/*由kernel_thread去执行function(func_arg)*/
static void kernel_thread(thread_func *function, void *func_arg)
{
    /*执行function前要开中断,避免后面的时钟中断被屏蔽,而无法调度其他线程*/
    intr_enable();
    function(func_arg);
}

/*初始化线程PCB*/
void init_thread(struct task_struct *pthread, char *name, int prio)
{
    memset(pthread, 0, sizeof(*pthread));
    strcpy(pthread->name, name);

    /*由于main函数也封装成了一个线程,并且他是一直在运行的,所以将其直接设置为TASK_RUNNING*/
    if (pthread == main_thread) {
        pthread->status = TASK_RUNNING;
    } else {
        pthread->status = TASK_READY;
    }
    //pthread->status = TASK_RUNNING;
    pthread->priority = prio;
    pthread->ticks = prio;
    pthread->elapsed_ticks = 0;
    pthread->pgdir = NULL;
    pthread->self_kstack = (uint32_t *)((uint32_t)pthread + PG_SIZE);
    pthread->stack_magic = 0x19870916;
}

void thread_create(struct task_struct *pthread, thread_func function, void *func_arg)
{
    pthread->self_kstack -= sizeof(struct intr_stack);
    pthread->self_kstack -= sizeof(struct thread_stack);

    //初始化线程栈
    struct thread_stack *kthread_stack = (struct thread_stack *)pthread->self_kstack;
    kthread_stack->eip = kernel_thread;
    kthread_stack->function = function;
    kthread_stack->func_arg = func_arg;
    kthread_stack->ebp = kthread_stack->ebx = kthread_stack->edi = kthread_stack->esi = 0;
}

/*创建一个优先级为prio的线程,线程名字为name,线程所执行的函数为function(func_arg)*/
struct task_struct *thread_start(char *name, int prio, thread_func function, void *func_arg)
{
    /*创建线程的pcb,大小为4kb*/
    struct task_struct *thread = get_kernel_pages(1);
    init_thread(thread, name, prio);
    thread_create(thread, function, func_arg);

    /*确保之前不在队列中*/
    ASSERT(!elem_find(&thread_ready_list, &thread->general_tag));

    /*加入就绪线程队列*/
    list_append(&thread_ready_list, &thread->general_tag);

    /*确保之前不在队列*/
    ASSERT(!elem_find(&thread_all_list, &thread->all_list_tag));
    
    /*加入全部线程队列*/
    list_append(&thread_all_list, &thread->all_list_tag);

    return thread;
}

static void make_main_thread(void)
{
    main_thread = running_thread();
    init_thread(main_thread, "main", 31);

    /*main函数是当前线程,当前线程不在thread_ready_list,所以只能将其加在thread_all_list*/
    ASSERT(!elem_find(&thread_all_list, &main_thread->all_list_tag));
    list_append(&thread_all_list, &main_thread->all_list_tag);
}

/*实现任务调度*/
void schedule(void)
{
    //put_str("schedule\n");
    ASSERT(intr_get_status() == INTR_OFF);
    struct task_struct *cur = running_thread();
    if (cur->status == TASK_RUNNING) {
        ASSERT(!elem_find(&thread_ready_list, &cur->general_tag));
        list_append(&thread_ready_list, &cur->general_tag);
        cur->ticks = cur->priority;
        cur->status = TASK_READY;
    } else {
        /*阻塞等其他情况*/
    }

    ASSERT(!list_empty(&thread_ready_list));
    thread_tag = NULL;
    thread_tag = list_pop(&thread_ready_list);
    
    struct task_struct *next = elem2entry(struct task_struct, general_tag, thread_tag);
    next->status = TASK_RUNNING;
    switch_to(cur, next);
}

/*初始化线程环境*/
void thread_init(void)
{
    put_str("thread_init start\n");
    list_init(&thread_ready_list);
    list_init(&thread_all_list);
    /*将当前main函数创建为线程*/
    make_main_thread();
    put_str("thread_init done\n");
}
thread.c
#ifndef  __KERNEL_THREAD_H
#define  __KERNEL_THREAD_H
#include "stdint.h"
#include "list.h"
#include "memory.h"

/*自定义通用函数类型,它将在很多线程函数中作为形参类型*/
typedef void thread_func (void *);
#define PG_SIZE 4096
/*进程或线程的状态*/
enum task_status {
    TASK_RUNNING,
    TASK_READY,
    TASK_BLOCKED,
    TASK_WAITING,
    TASK_HANGING,
    TASK_DIED
};

/****************中断栈intr_stack****************/
struct intr_stack {
    uint32_t vec_no;
    uint32_t edi;
    uint32_t esi;
    uint32_t ebp;
    uint32_t esp_dummy;
    uint32_t ebx;
    uint32_t edx;
    uint32_t ecx;
    uint32_t eax;
    uint32_t gs;
    uint32_t fs;
    uint32_t es;
    uint32_t ds;

/*以下由cpu从低特权级进入高特权级时压入*/
    uint32_t err_code;
    void (*eip)(void);
    uint32_t cs;
    uint32_t eflags;
    void *esp;
    uint32_t ss;
};

/***************线程栈thread_stack**********/
struct thread_stack 
{
    uint32_t ebp;
    uint32_t ebx;
    uint32_t edi;
    uint32_t esi;

    void (*eip) (thread_func *func, void *func_arg);
    void (*unused_retaddr);
    thread_func *function;
    void *func_arg;
};

/************进程或者线程的pcb,程序控制块**********/
struct task_struct
{
    uint32_t *self_kstack;    //每个内核线程自己的内核栈
    enum task_status status;
    uint8_t priority;
    
    char name[16];
    uint8_t ticks;            //每次在处理器上执行的时间滴答数

    /*此任务自从上CPU运行至今占用了多少滴答数,也就是这个任务执行了多久时间*/
    uint32_t elapsed_ticks;

    /*general_tag的作用是用于线程在一般的队列中的节点*/
    struct list_elem general_tag;

    /*all_list_tag的作用是用于线程thread_all_list的节点*/
    struct list_elem all_list_tag;

    uint32_t *pgdir;//进程自己页表的虚拟地址

    uint32_t stack_magic;
};

void schedule(void);
struct task_struct *running_thread(void);
static void kernel_thread(thread_func *function, void *func_arg);
void init_thread(struct task_struct *pthread, char *name, int prio);
void thread_create(struct task_struct *pthread, thread_func function, void *func_arg);
struct task_struct *thread_start(char *name, int prio, thread_func function, void *func_arg);
static void make_main_thread(void);
void thread_init(void);

#endif
thread.h

  不过我并不建议现在就去看代码,我当时看这一章看得云里雾里,后面捋了好久,现在希望你能跟着我的思路从宏观上了解线程的创建,再回去掐细节就很好理解了。

  首先,我们先定义PCB结构,PCB结构由中断栈、线程栈和task_struct组成:

 1 /****************中断栈intr_stack****************/
 2 struct intr_stack {
 3     uint32_t vec_no;
 4     uint32_t edi;
 5     uint32_t esi;
 6     uint32_t ebp;
 7     uint32_t esp_dummy;
 8     uint32_t ebx;
 9     uint32_t edx;
10     uint32_t ecx;
11     uint32_t eax;
12     uint32_t gs;
13     uint32_t fs;
14     uint32_t es;
15     uint32_t ds;
16 
17 /*以下由cpu从低特权级进入高特权级时压入*/
18     uint32_t err_code;
19     void (*eip)(void);
20     uint32_t cs;
21     uint32_t eflags;
22     void *esp;
23     uint32_t ss;
24 };
25 
26 /***************线程栈thread_stack**********/
27 struct thread_stack 
28 {
29     uint32_t ebp;
30     uint32_t ebx;
31     uint32_t edi;
32     uint32_t esi;
33 
34     void (*eip) (thread_func *func, void *func_arg);
35     void (*unused_retaddr);
36     thread_func *function;
37     void *func_arg;
38 };
39 
40 /************进程或者线程的pcb,程序控制块**********/
41 struct task_struct
42 {
43     uint32_t *self_kstack;     //每个内核线程自己的内核栈               
44     enum task_status status;   //线程或进程状态
45     uint8_t priority;          //线程或进程状态
46     
47     char name[16];             //线程或进程名称
48     uint8_t ticks;            //每次在处理器上执行的时间滴答数
49 
50     /*此任务自从上CPU运行至今占用了多少滴答数,也就是这个任务执行了多久时间*/
51     uint32_t elapsed_ticks;
52 
53     /*general_tag的作用是用于线程在一般的队列中的节点*/
54     struct list_elem general_tag;
55 
56     /*all_list_tag的作用是用于线程thread_all_list的节点*/
57     struct list_elem all_list_tag;
58 
59     uint32_t *pgdir;                //进程自己页表的虚拟地址
60  
61     uint32_t stack_magic;           //魔数 边缘检测使用
62 };
PCB

  有了PCB,那么如何实现线程呢?在Linux中提供创建线程的函数为:

  int pthread_create(pthread_t *id , pthread_attr_t *attr, void(*fun)(void*), void *arg);

  其中fun就是线程将要执行的函数,arg就是要往函数里面传递的参数。

  照猫画虎,我们也实现一个类似的函数,就叫做:

  struct task_struct *thread_start(char *name, int prio, thread_func function, void *func_arg);

  其中name表示线程名字,prio表示线程优先级,function表示线程将要执行的函数,func_arg表示传递给函数的参数。

  在这个函数中我们对线程PCB噼里啪啦进行初始化等一系列操作之后,最后在内存中出现了这么一块东西:

  PCB在内存中的结构如上图所示,从上往下,首先是intr_stack,中断栈,它的作用是什么呢?假设我们的线程被调度在CPU上运行,突然来了一个中断,这时CPU肯定不能马上转头去处理中断,需要先把线程当前的运行环境给保存下来,然后才去处理中断。保存在哪里呢?就保存在这个中断栈中,关于这部分后面章节还会详细讲到,这里先不管;随后是thread_stack,又叫线程栈,它的作用就是保存线程需要运行的函数以及传递给该函数的参数,可以看到eip指向的函数:kernel_thread(thread_func *, void *)就是我们最终线程需要去执行的函数。至于其他的几个参数,待会儿再说;最后是task_struct,它呢就是保存了线程的一些基本信息,比如线程名称、优先级、状态等等。

三、线程的切换

  线程是怎么切换的呢?或者换句话说,线程是怎么被调度上CPU,又怎么被调度下CPU的呢?这里就不卖关子了,还记得我们在线程的初始化中,有一个ticks的变量么?这个ticks变量在初始化时就被赋了一定的值。另一边,在我们的系统中开启了一个定时器中断,这个中断每隔一段时间就会进入中断处理函数,在中断处理函数中将当前线程的ticks减1,当ticks被减为0后就调用schedule函数将当前线程调下,将下一个就绪线程调度上CPU,否则便从中断处理函数返回继续执行当前线程的程序。

  现在线程的切换我们也讲完了,不过我想你可能还是迷迷糊糊,心想就这?我还是不懂嘛。

  不急,我们还是带入具体情况来一步一步分析。现在我们来假想这么一种情况,假如我们的线程A的ticks已经减为0,那么意味着线程A要被换下,而下一个线程B要被换上,让我们来看一下线程A是如何切换到线程B的。先来看看schedule()这个函数,schedule()定义在thread.c文件中,这个函数就是调度函数。

/*实现任务调度*/
void schedule(void)
{
    //put_str("schedule\n");
    ASSERT(intr_get_status() == INTR_OFF);
    struct task_struct *cur = running_thread();
    if (cur->status == TASK_RUNNING) {
        ASSERT(!elem_find(&thread_ready_list, &cur->general_tag));
        list_append(&thread_ready_list, &cur->general_tag);
        cur->ticks = cur->priority;
        cur->status = TASK_READY;
    } else {
        /*阻塞等其他情况*/
    }

    ASSERT(!list_empty(&thread_ready_list));
    thread_tag = NULL;
    thread_tag = list_pop(&thread_ready_list);
    
    struct task_struct *next = elem2entry(struct task_struct, general_tag, thread_tag);
    next->status = TASK_RUNNING;
    switch_to(cur, next);
}
schedule()

  首先修改当前要被换下的线程A的信息,将运行状态改为TASK_READY,重新赋值ticks,然后添加到就绪队列中去,供调度器下一次调度。随后利用list_pop函数,将下一个准备就绪的线程B从就绪队列中弹出,利用elem2entry函数得到线程B的PCB所在的地址,随后修改线程B的运行状态为TASK_RUNNING。此时线程B已经准备好了所有,就准备通过switch_to函数调度上CPU了。在project/kernel目录下新建名为switch.S的文件,在其中实现switch_to函数。

[bits 32]
section .text
global switch_to
switch_to:
    push esi            ;这里是根据ABI原则保护四个寄存器 放到栈里面
    push edi
    push ebx
    push ebp
    
    mov eax, [esp+20]    ;esp+20的位置是cur cur的pcb赋值给eax
    mov [eax], esp       ;[eax]为pcb的内核栈指针变量 把当前环境的esp值记录下来
    
    mov eax, [esp+24]
    mov esp, [eax]       ;把要切换的线程的pcb 内核栈esp取出来

    pop ebp
    pop ebx
    pop edi
    pop esi
    ret                 ;这里的返回地址为 kernel_thread的地址
switch.S

  关于这里为什么要连续通过四个push操作将esi、edi、ebx和ebp,以及后面新线程又要弹出这四个寄存器值,这是因为ABI的规定,这里不详细展开,想了解的话可以参考原书《操作系统真象还原》P411页。总之现在通过四个push操作后,此时线程A栈里的情况是这样:

  随后通过mov eax, [esp + 20],将cur所指向的地址保存在eax寄存器中,也就是将当前线程A的PCB地址赋给了eax寄存器。又通过mov [eax], esp指令,将当前线程A的esp存放于线程A的self_kstack中。随后通过

  mov eax, [esp+24]   

  mov esp, [eax]  

  这两行命令将线程B的esp指针加载到esp寄存器中。这样就完成了栈的切换。此时,请注意,由于栈已经发生变化了,现在是线程B的栈了,还记得前面说线程初始化后的那张内存分布图么,在这里:

 

  对于初始化好的线程B,它的PCB内存分布图就如上图所示。此时线程B的栈的情况:

  

  接着看switch_to函数中的代码,我们还有下面一部分没有执行完。需要注意的是,栈已经发生变化了,所以接下来的pop操作都是针对线程B的栈,这里切忌不要搞错。所以我们可以看到,四个pop操作将ebp,ebx,edi和esi弹出到对应的寄存器中。随后调用ret指令,该指令的作用是将栈顶处的数据*eip弹到eip寄存器中并且使esp+4,也就是说cpu接下来将从eip寄存器所指向的地址开始执行,而我们事先已将线程的执行函数kernel_thread保存在*eip处。该函数实现如下:

/*由kernel_thread去执行function(func_arg)*/
static void kernel_thread(thread_func *function, void *func_arg)
{
    /*执行function前要开中断,避免后面的时钟中断被屏蔽,而无法调度其他线程*/
    intr_enable();
    function(func_arg);
}
kernel_thread

  此时相当于是调用*eip所指向的函数,对于这个函数而言,栈中的情况如上右图所示。这时我们事先设置的unused_retaddr就起到作用了,对于被调函数而言,*unused_retaddr就相当于是返回地址,只不过我们的函数永远不会从这里返回。所以被调函数会从esp+4开始取值作为函数的输入参数。至此我们便完成了线程的切换过程。

  我们又来分析一下,线程在运行时,如果时间片用光了,要被从CPU上调度下去的过程:

  我们知道定时器在每次中断中,都会将当前线程的ticks减1,并且检测ticks是否为0,如果为0就调用schedule函数,也就是调度函数,也就回到了上面我们讲解schedule的地方了。关于内核线程在执行过程中,遇到中断后发生的压栈情况,读者可以参看第12回 实现中断代码。在这一回我详细地讲解了中断的压栈和出栈过程。

  所以,对于这部分代码,我建议读者先从调度函数schedule出发,理清楚思路后,就很好理解整个线程的实现以及调度了。需要说明的是,为什么叫内核线程呢?其实线程不应该有什么内核,用户之分的。确切地说应该是线程所处的运行态,就拿从本系列开始到本回合为止的代码来说,我们一直都是处于内核态的,也就是最高特权级0级下的,所以我们可以随意访问任意地址的内存。我想会有读者好奇我们现在实现的PCB中,有一块中断栈始终没有讲到,它其实就是给我们后面实现的用户态的线程所使用的。我们用户态下的线程如果需要被调度下CPU,首先需要通过定时器中断进入中断函数,完成3特权级到0特权级的转变。一旦发生中断,处理器会将线程的相关运行环境保存在0特权级下的栈中,这个0特权级的栈就是我们前面所构建的这个中断栈,因为不同特权级下所用的栈是不同的。所以它的作用在此,这里就不展开讲,等我们到后面的章节再细说。

四、运行测试

  将thread_init()加入到init.c中,修改main.c,在main.c中创建多个线程。除此之外还要修改makefile,time.c下的定时器中断函数。

#include "init.h"
#include "print.h"
#include "interrupt.h"
#include "timer.h"
#include "memory.h"
#include "thread.h"
#include "list.h"

void init_all(void)
{
    put_str("init_all\n");
    idt_init();
    timer_init();
    mem_init();
    thread_init();
}
init.c
#include "print.h"
#include "init.h"
#include "memory.h"
#include "thread.h"
#include "list.h"
#include "interrupt.h"

void k_thread_a(void *arg);
void k_thread_b(void *arg);

int main(void)
{
    put_str("HELLO KERNEL\n");
    init_all();
    thread_start("k_thread_a", 31, k_thread_a, "A ");
    thread_start("k_thread_b", 8, k_thread_b, "B ");
    intr_enable();
    while(1) {
        put_str("Main: ");
    }
}

/*在线程中运行的函数k_thread_a*/
void k_thread_a(void *arg)
{
    char *para = arg;
    while (1) {
        put_str(para);
    }
}

/*在线程中运行的函数k_thread_b*/
void k_thread_b(void *arg)
{
    char *para = arg;
    while (1) {
        put_str(para);
    }
}
main.c
#include "timer.h"
#include "io.h"
#include "print.h"
#include "interrupt.h"
#include "thread.h"
#include "debug.h"

#define IRQ0_FREQUENCY         100
#define INPUT_FREQUENCY     1193180
#define COUNTER0_VALUE        INPUT_FREQUENCY / IRQ0_FREQUENCY
#define COUNTER0_PORT        0x40
#define COUNTER0_NO         0
#define COUNTER_MODE        2
#define READ_WRITE_LATCH    3
#define PIT_COUNTROL_PORT    0x43

uint32_t ticks;   //ticks是内核自中断开启以来总共的滴答数

/*时钟的中断处理函数*/
static void intr_timer_handler(void)
{
    struct task_struct *cur_thread = (struct task_struct *)running_thread();
    ASSERT(cur_thread->stack_magic == 0x19870916); //检查栈是否溢出
    cur_thread->elapsed_ticks++;
    ticks++;
    if (cur_thread->ticks == 0) {
        schedule();
    } else {
        cur_thread->ticks--;
    }
}


static void frequency_set(uint8_t counter_port ,uint8_t counter_no,uint8_t rwl,uint8_t counter_mode,uint16_t counter_value)
{
    outb(PIT_COUNTROL_PORT, (uint8_t) (counter_no << 6 | rwl << 4 | counter_mode << 1));
    outb(counter_port, (uint8_t)counter_value);
    outb(counter_port, (uint8_t)counter_value >> 8);
} 

void timer_init(void)
{
    put_str("timer_init start!\n");
    frequency_set(COUNTER0_PORT, COUNTER0_NO, READ_WRITE_LATCH, COUNTER_MODE, COUNTER0_VALUE);
    register_handler(0x20, intr_timer_handler);
    put_str("timer_init done!\n");
}
timer.c
 1 BUILD_DIR = ./build
 2 PATH1 = project/kernel
 3 PATH2 = project/lib/kernel
 4 PATH3 = project/lib/user
 5 PATH4 = project/userprog
 6 PATH5 = project/lib
 7 INCLUDE = -I $(PATH1) -I $(PATH2) -I $(PATH3) -I $(PATH4) -I $(PATH5) 
 8 SRC = $(wildcard $(PATH1)/*.c $(PATH2)/*.c $(PATH3)/*.c $(PATH4)/*.c $(PATH5)/*.c)
 9 OBJ = $(patsubst %.c, $(BUILD_DIR)/%.o, $(notdir $(SRC))) $(BUILD_DIR)/print.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/switch.o 
10 
11 kernel.bin: $(OBJ)
12     ld -m elf_i386 -Ttext 0xc0001500 -e main -o ./kernel.bin ./build/main.o ./build/print.o ./build/interrupt.o \
13     ./build/kernel.o ./build/timer.o ./build/init.o ./build/debug.o ./build/string.o ./build/bitmap.o ./build/list.o \
14     ./build/memory.o ./build/switch.o ./build/thread.o
15 
16 mbr.bin: mbr.S
17     nasm -I include/ mbr.S -o mbr.bin 
18 
19 loader.bin: loader.S
20     nasm -I include/ loader.S -o loader.bin 
21 
22 install: mbr.bin loader.bin
23     dd if=./mbr.bin of=./hd60M.img bs=512 count=1 conv=notrunc 
24     dd if=./loader.bin of=./hd60M.img bs=512 count=4 seek=2 conv=notrunc
25     dd if=./kernel.bin of=./hd60M.img bs=512 count=200 seek=9 conv=notrunc
26     ./bin/bochs -f bochsrc.disk
27 
28 #编译print.S
29 $(BUILD_DIR)/print.o : ./project/lib/kernel/print.S
30     nasm -f elf -o $(BUILD_DIR)/print.o ./project/lib/kernel/print.S
31 
32 #编译kernel.S
33 $(BUILD_DIR)/kernel.o : ./project/kernel/kernel.S
34     nasm -f elf -o $(BUILD_DIR)/kernel.o ./project/kernel/kernel.S
35 
36 #编译switch.S
37 $(BUILD_DIR)/switch.o : ./project/kernel/switch.S
38     nasm -f elf -o $(BUILD_DIR)/switch.o ./project/kernel/switch.S
39 
40 #编译四个目录下的.c文件为对应的.o文件
41 $(BUILD_DIR)/%.o : $(PATH1)/%.c 
42     gcc -m32 $(INCLUDE) -c -fno-builtin $< -o $@
43 
44 $(BUILD_DIR)/%.o : $(PATH2)/%.c
45     gcc -m32 $(INCLUDE) -c -fno-builtin $< -o $@
46 
47 $(BUILD_DIR)/%.o : $(PATH3)/%.c
48     gcc -m32 $(INCLUDE) -c -fno-builtin $< -o $@
49 
50 $(BUILD_DIR)/%.o : $(PATH4)/%.c
51     gcc -m32 $(INCLUDE) -c -fno-builtin $< -o $@
52 
53 $(BUILD_DIR)/%.o : $(PATH5)/%.c
54     gcc -m32 $(INCLUDE) -c -fno-stack-protector -fno-builtin $< -o $@
55 
56 .PHONY:clean #防止 外面有clean文件 阻止执行clean
57 clean:
58     -rm -rf $(BUILD_DIR)/*.o
makefile

  

  运行起来还算符合预期效果,可以看到argA打印的次数大概是argB的4倍,这跟我们所设置的时间片关系有关。不过可以看到字符打印的不连续性,这个留到下回再说。

  本回到此结束,预知后事如何,请看下回分解。

posted @ 2022-07-08 14:28  李知行  阅读(742)  评论(0编辑  收藏  举报