本文共 646 字,大约阅读时间需要 2 分钟。
昨天有空看了一下nachos里的switch.s中switch 函数,从最底层了解了一下,进程(线程)切换时,是如何把旧进程搬出寄存器,新进程如何搬进寄存器。具体源码和分析如下:
/* void SWITCH( thread *t1, thread *t2 ) ** ** on entry, stack looks like this: ** 8(esp) -> thread *t2(new) ** 4(esp) -> thread *t1(old) ** (esp) -> return address ** ** we push the current eax on the stack so that we can use it as ** a pointer to t1, this decrements esp by 4, so when we use it ** to reference stuff on the stack, we add 4 to the offset.*/
具体分析如下:
1. 回顾一下EAX等这些寄存器是干嘛的?
2. movl的语法是如何操作的?
movl source des
3. 基于上面的知识和源码,流程图如下:
总的思路, 就是当前的进程 所有寄存器状态保存到 4(esp )所指向的旧线程的内存中,
然后再把8(esp)所指向的新线程的内存中的值更新当前的寄存器值,
然后把该函数的返回地址,指向新的线程
如有不对之处,欢迎指正!
转载地址:http://npoji.baihongyu.com/