一个简单的时间片轮转多道程序内核代码
WuQi + 原创作品转载请注明出处 + 《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000
PCB的分析
PCB是什么?
答:进程控制块(PCB,Process Control Block)
PCB的组成?
PCB中主要包括下述四方面的信息:
进程标识符:内部标识符,外部标识符
处理机状态
进程调度信息
进程控制信息
首先对mypcb.h中的代码进行分析
#define MAX_TASK_NUM 5 #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;//思考 task_entry的意义? struct PCB *next; }tPCB;
//ip应该类似寄存器中的ip:存放下一个CPU指令存放的内存地址,当CPU执行完当前的指令后,从IP寄存器中读取下一条指令的内存地址,然后继续执行。
看一下mymain.c
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;//将task0的识别符设置为0 task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped *///将task0设置为运行状态 task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process; //task0的程序入口设置为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));
//void *memcpy(void *dest, const void *src, size_t n);
//从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中 //比如task_entry和thread.ip 就不变了。
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*/ );
//用于这段代码将esp和ebp都设置为task[pid].thread.sp的值,这样操作系统操作的就是该线程(进程)的私有堆栈
//把task[pid].thread.ip赋给eip,这样指令就按照task[pid]的ip执行了
}
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); } } }
在中断文件中
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 */ //1f是接下来程序后面标号为1的地方,也就是eip "pushl %3\n\t" "ret\n\t" /* restore eip *///对eip不能直接操作,只能通过这种先push然后ret的方式进行操作 "1:\t" /* next process start here */ "popl %%ebp\n\t" : "=m" (prev->thread.sp),"=m" (prev->thread.ip)//prev指的是当前的进程 : "m" (next->thread.sp),"m" (next->thread.ip)//next指的是即将开始的进程 ); 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 */
//先将ebp储存到自己的堆栈中,然后将esp记录下来。这样下次执行的时候,将esp恢复回去,然后popl %%ebp就可以了
"movl %2,%%esp\n\t" /* restore esp */ "movl %2,%%ebp\n\t" /* restore ebp */
//因为是第一次执行,ebp和esp都是堆栈的esp值,所以直接将参数2,也就是next->thread.sp赋给esp和ebp就行了
"movl $1f,%1\n\t" /* save eip */
//将prev->thread.ip的值改为1f的值,程序下次调用就从上面的pop %%ebp开始
"pushl %3\n\t" "ret\n\t" /* restore eip */
//这两句结合在一起,因为eip不能直接操作,所以需要先push,然后ret(包含pop到eip的动作)
: "=m" (prev->thread.sp),"=m" (prev->thread.ip) : "m" (next->thread.sp),"m" (next->thread.ip) ); } return; }
//针对else中的 mov $1f,%1\n\t进行讨论
对论坛上 else中"movl $1f,%1\n\t" 的问题,看到老师的回复是“$1f放到eip里使用才算生效吧,不能见$1f就找标号1”。我的理解是这样的:if中的自然不必说,else中代码只有进程第一次被执行时候才会运行,此时将$1f存入prev->thread.ip,但当进程被重新调度执行的时候,此时进入了if代码块中,因此将执行if代码块中的1:标号处的代码,所以else中没有"1:"也就不奇怪了。
其他类似链接:
http://blog.csdn.net/chenjinlei456/article/details/44257313
http://segmentfault.com/a/1190000002595198
浙公网安备 33010602011771号