上一篇文章中介绍了一个进程执行的过程,分析了在调用子程序时栈的变化过程。本文介绍一下多进程的执行过程,当一个进程需要停下来等待某个条件或者cpu给它分配的时间片用完时需要切换给别的进程,在切换时首先会产生一个中断,然后cpu会执行相应的中断处理操作,比如一个重要的操作就是保护当前进程的栈和cpu寄存器的值,当cpu再次调度时可以恢复上一次状态继续执行下去,注意本文的举例可以理解为coroutine协程,而不是真正的进程。
按照mykernel上的步骤安装好程序,执行qemu -kernel arch/x86/boot/bzImage看到如下图:
执行的程序如下:
执行一个进程A 做i++操作,当i%100000时打印i的值,当时钟中断产生时执行中断处理my_timer_handler。我们发现打印的i值是一直在递增的,也就是中断处理执行完后cpu继续调度到A时,i保持了上次调度完成时的值,这是怎样做到的呢,就是中断处理,下面我们通过程序模拟这一过程。将mykernel 1.1中程序复制到mykernel目录重新编译执行如下。
下面我们分析一下程序,首先看一下数据结构,操作系统为每一个进程都分配了一个pcb(process control block),在我们的程序中定义如下:
typedef struct PCB{
int pid; // pcb id
volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
char stack[KERNEL_STACK_SIZE];// each pcb stack size is 1024*8
/* CPU-specific state of this task */
struct Thread thread;
unsigned long task_entry;//the task execute entry memory address
struct PCB *next;//pcb is a circular linked list
unsigned long priority;// task priority ////////
//todo add other attrubte of process control block
}tPCB;
struct Thread {
unsigned long ip;//point to cpu run address
unsigned long sp;//point to the thread stack's top address
//todo add other attrubte of system thread
};
PCB中stack记录着进程的调用栈,注意栈的空间从大到小分配,Thread结构中ip表示接下来需要执行的是哪一条指令(地址),sp指向调用栈的栈顶。下面的程序初始化每个进程的pcb并启动了0号进程:
void __init my_start_kernel(void)
{
int pid = 0;
/* Initialize process 0*/
task[pid].pid = pid;
task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped */
// set task 0 execute entry address to my_process
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(pid=1;pid<MAX_TASK_NUM;pid++)
{
memcpy(&task[pid],&task[0],sizeof(tPCB));
task[pid].pid = pid;
task[pid].state = -1;
task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1];
task[pid].priority=get_rand(PRIORITY_MAX);//each time all tasks get a random priority
}
task[MAX_TASK_NUM-1].next=&task[0];
printk(KERN_NOTICE "
system begin :>>>process 0 running!!!<<<
");
/* start process 0 by task[0] */
pid = 0;
my_current_task = &task[pid];
asm volatile(
"movl %1,%%esp
" /* set task[pid].thread.sp to esp */
"pushl %1
" /* push ebp */
"pushl %0
" /* push task[pid].thread.ip */
"ret
" /* pop task[pid].thread.ip to eip */
"popl %%ebp
"
:
: "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)
{
if(my_need_sched == 1)
{
my_need_sched = 0;
sand_priority();
my_schedule();
}
}
}
}//end of my_process
从上面的程序可以看到thread.sp都是指向stack[KERNEL_STACK_SIZE-1],也就是数组最后一个元素。thread.ip取my_process函数的地址,也就是这个进程的入口。从嵌入汇编中可以看到把0号进程的栈顶指针存入esp寄存器,把进程入口地址存入eip寄存器,当ret指令执行时,0号进程启动。接下来看一下进程的切换。
asm volatile(
"pushl %%ebp
" /* save ebp */
"movl %%esp,%0
" /* save esp */
"movl %2,%%esp
" /* restore esp */
"movl %2,%%ebp
" /* restore ebp */
"movl $1f,%1
" /* save eip */
"pushl %3
"
"ret
" /* restore eip */
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
asm volatile(
"pushl %%ebp
" /* save ebp */
"movl %%esp,%0
" /* save esp */
"movl %2,%%esp
" /* restore esp */
"movl $1f,%1
" /* save eip */
"pushl %3
"
"ret
" /* restore eip */
"1: " /* next process start here */
"popl %%ebp
"
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
从程序中可以看到切换时都会保存ebp,esp,eip的值,并且把新进程的值赋给相应寄存器。
从整个程序看很像是在一个进程里面切换不同的线程,这里的线程不是指系统级别的,和通过ucontext_t来实现协程类似。