实验二:一个简单的时间片轮转多道程序内核代码分析
版权声明:本文为博主原创文章,未经博主允许不得转载。
钟晶晶
+ 原创作品转载请注明出处 + 《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000
一.实验截图


二.代码分析
2.1 mypcb.h
1 #define MAX_TASK_NUM 4 2 #define KERNEL_STACK_SIZE 1024*8 3 4 /* CPU-specific state of this task */ 5 struct Thread { 6 unsigned long ip; 7 unsigned long sp; 8 }; 9 10 typedef struct PCB{ 11 int pid; 12 volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */ 13 char stack[KERNEL_STACK_SIZE]; 14 /* CPU-specific state of this task */ 15 struct Thread thread; 16 unsigned long task_entry; 17 struct PCB *next; 18 }tPCB; 19 20 void my_schedule(void);

可知mypcb定义了如上的数据结构,同时声明了一个调度函数my_schedule().
2.2 mymain.c
1 #include "mypcb.h" 2 3 tPCB task[MAX_TASK_NUM]; 4 tPCB * my_current_task = NULL; 5 volatile int my_need_sched = 0; 6 7 void my_process(void); 8 9 10 void __init my_start_kernel(void) 11 { 12 int pid = 0; 13 int i; 14 /* Initialize process 0*/ 15 task[pid].pid = pid; 16 task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped */ 17 task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process; 18 task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1]; 19 task[pid].next = &task[pid]; 20 /*fork more process */
初始化循环体,初始一个单pcb循环链表
/*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]; }
扩充循环链表,使用memcpy将task[0]初始状态复制到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*/ ); }
初始化堆栈 ebp,esp,eip






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); } } }
每隔10000000次判断是否需要调度
2.3 Myinterrupt.c
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; }
my_timer_handler :定期将my_need_sched置为1,以调用my_schedule()。
oid 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 */ { my_current_task = next; printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid); /* 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_shedule();







三.总结
操作系统是如何工作的:
操作系统是运行在相应的硬件平台上的一个特别程序,它的任务是实现任务(也就是进程)的创建、运行和调度;同时实现对运行在相应的平台的资源管理和分配,实现整个硬件和软件系统处于最优的工作状态。Linux操作系统由内核来实现它的具体工作的,一个进程是通过系统调用fork()函数来创建的,他先是将先前CPU正在运行的进程的进程上下文保存在内核态堆栈中,包括有eip,esp,ebp,cs等寄存器的数据;然后加载创建的进程的上下文信息到相应的寄存器中,运行当前新建进程;运行完毕后根据系统的调度继续执行相应的进程。在这个过程中的执行流程是:SAVE_ALLà进建PID对应的task_structàrestore allàiret。而Linux操作系统是多进程的操作系统,不同的进程就是基于以上的方式有操作系统实现调度运行的。同时,操作系统以一种中断的机制实现与用户的交互。操作系统中的IDT描述好各个中断对应的处理程序,当发生相对应的中断时,由硬件来实现中断信号的传递,CPU接收到相应的IRQ信号后,由操作系统如调度进程那样调度相应的处理程序,来完成相应的中断请求,实现与用户的交互。整个操作系统就是如此实现。
流程如下:
- 将当前的用户态堆栈的esp. ebp指针保存在当前进程内核栈。
- 执行save_all, 保存进程a 的各个寄存器的值到其内核中。
- 进入中断处理程序
- 操作系统调用schedule()函数来进行调度,进入进程b 的内核栈;
- 执行RESTALL_ALL,恢复现场,恢复进程b 的寄存器的值;
- 执行IRET,恢复EIP,ESP以及EFLAGS寄存器;
- 系统从内核态返回用户态
浙公网安备 33010602011771号