第二周 内核进程调度

操作系统为了实现支持多任务处理的需要,一般都会支持多进程的机制,所以进程的的切换是内核当中一个非常重要的功能模块,其它几个功能模块主要有

  • 处理器管理

  • 内存管理

  • 磁盘管理

  • 输入输出管理

  • 进程管理

本次实验主要是模拟了内核中的进程切换机制,以便加深对内核进程切换的理解。其中本次实验主要涉及以下三个代码文件。

mypcb.h

 1 /*
 2  *  linux/mykernel/mypcb.h
 3  *
 4  *  Kernel internal PCB types
 5  *
 6  *  Copyright (C) 2013  Mengning
 7  *
 8  */
 9 
10 #define MAX_TASK_NUM        4
11 #define KERNEL_STACK_SIZE   1024*8
12 
13 /* CPU-specific state of this task */
14 struct Thread {
15     unsigned long        ip;
16     unsigned long        sp;
17 };
18 
19 typedef struct PCB{
20     int pid;
21     volatile long state;    /* -1 unrunnable, 0 runnable, >0 stopped */
22     char stack[KERNEL_STACK_SIZE];
23     /* CPU-specific state of this task */
24     struct Thread thread;
25     unsigned long    task_entry;
26     struct PCB *next;
27 }tPCB;
28 
29 void my_schedule(void);

mymain.c

技术分享
 1 /*
 2  *  linux/mykernel/mymain.c
 3  *
 4  *  Kernel internal my_start_kernel
 5  *
 6  *  Copyright (C) 2013  Mengning
 7  *
 8  */
 9 #include <linux/types.h>
10 #include <linux/string.h>
11 #include <linux/ctype.h>
12 #include <linux/tty.h>
13 #include <linux/vmalloc.h>
14 
15 
16 #include "mypcb.h"
17 
18 tPCB task[MAX_TASK_NUM];
19 tPCB * my_current_task = NULL;
20 volatile int my_need_sched = 0;
21 
22 void my_process(void);
23 
24 
25 void __init my_start_kernel(void)
26 {
27     int pid = 0;
28     int i;
29     /* Initialize process 0*/
30     task[pid].pid = pid;
31     task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped */
32     task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process;
33     task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1];
34     task[pid].next = &task[pid];
35     /*fork more process */
36     for(i=1;i<MAX_TASK_NUM;i++)
37     {
38         memcpy(&task[i],&task[0],sizeof(tPCB));
39         task[i].pid = i;
40         task[i].state = -1;
41         task[i].thread.sp = (unsigned long)&task[i].stack[KERNEL_STACK_SIZE-1];
42         task[i].next = task[i-1].next;
43         task[i-1].next = &task[i];
44     }
45     /* start process 0 by task[0] */
46     pid = 0;
47     my_current_task = &task[pid];
48     asm volatile(
49         "movl %1,%%esp\n\t"     /* set task[pid].thread.sp to esp */
50         "pushl %1\n\t"             /* push ebp */
51         "pushl %0\n\t"             /* push task[pid].thread.ip */
52         "ret\n\t"                 /* pop task[pid].thread.ip to eip */
53         "popl %%ebp\n\t"
54         : 
55         : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp)    /* input c or d mean %ecx/%edx*/
56     );
57 }   
58 void my_process(void)
59 {
60     int i = 0;
61     while(1)
62     {
63         i++;
64         if(i%10000000 == 0)
65         {
66             printk(KERN_NOTICE "this is process %d -\n",my_current_task->pid);
67             if(my_need_sched == 1)
68             {
69                 my_need_sched = 0;
70                 my_schedule();
71             }
72             printk(KERN_NOTICE "this is process %d +\n",my_current_task->pid);
73         }     
74     }
75 }
View Code

 

myinterrupt.c

 1 /*
 2  *  linux/mykernel/myinterrupt.c
 3  *
 4  *  Kernel internal my_timer_handler
 5  *
 6  *  Copyright (C) 2013  Mengning
 7  *
 8  */
 9 #include <linux/types.h>
10 #include <linux/string.h>
11 #include <linux/ctype.h>
12 #include <linux/tty.h>
13 #include <linux/vmalloc.h>
14 
15 #include "mypcb.h"
16 
17 extern tPCB task[MAX_TASK_NUM];
18 extern tPCB * my_current_task;
19 extern volatile int my_need_sched;
20 volatile int time_count = 0;
21 
22 /*
23  * Called by timer interrupt.
24  * it runs in the name of current running process,
25  * so it use kernel stack of current running process
26  */
27 void my_timer_handler(void)
28 {
29 #if 1
30     if(time_count%1000 == 0 && my_need_sched != 1)
31     {
32         printk(KERN_NOTICE ">>>my_timer_handler here<<<\n");
33         my_need_sched = 1;
34     } 
35     time_count ++ ;  
36 #endif
37     return;      
38 }
39 
40 void my_schedule(void)
41 {
42     tPCB * next;
43     tPCB * prev;
44 
45     if(my_current_task == NULL 
46         || my_current_task->next == NULL)
47     {
48         return;
49     }
50     printk(KERN_NOTICE ">>>my_schedule<<<\n");
51     /* schedule */
52     next = my_current_task->next;
53     prev = my_current_task;
54     if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */
55     {
56         /* switch to next process */
57         asm volatile(    
58             "pushl %%ebp\n\t"         /* save ebp */
59             "movl %%esp,%0\n\t"     /* save esp */
60             "movl %2,%%esp\n\t"     /* restore  esp */
61             "movl $1f,%1\n\t"       /* save eip */    
62             "pushl %3\n\t" 
63             "ret\n\t"                 /* restore  eip */
64             "1:\t"                  /* next process start here */
65             "popl %%ebp\n\t"
66             : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
67             : "m" (next->thread.sp),"m" (next->thread.ip)
68         ); 
69         my_current_task = next; 
70         printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);       
71     }
72     else
73     {
74         next->state = 0;
75         my_current_task = next;
76         printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);
77         /* switch to new process */
78         asm volatile(    
79             "pushl %%ebp\n\t"         /* save ebp */
80             "movl %%esp,%0\n\t"     /* save esp */
81             "movl %2,%%esp\n\t"     /* restore  esp */
82             "movl %2,%%ebp\n\t"     /* restore  ebp */
83             "movl $1f,%1\n\t"       /* save eip */    
84             "pushl %3\n\t" 
85             "ret\n\t"                 /* restore  eip */
86             : "=m" (prev->thread.sp),"=m" (prev->thread.ip)
87             : "m" (next->thread.sp),"m" (next->thread.ip)
88         );          
89     }  
90     return;    
91 }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。