【nachos】nachos学习笔记(二) 进程切换

一、实验题目

今天开始进行nachos的第一个实验。环境为32位的Ubuntu 16.04 LTS

实验题目如下:

1. trace the execution of Nachos and observe the executions of
(a) context switch function SWITCH() (b) function ThreadRoot()
using gdb and
2. answer the following questions:
(a) What are the addresses of the following functions in your Nachos:
i. InterruptEnable() ii. SimpleThread() iii. ThreadFinish() iv. ThreadRoot() and describe how did you find them.
(b) What are the addresses of the thread objects for i. the main thread of the Nachos ii. the forked thread created by the main thread and describe how did you find them.
(c) When the main thread executes SWITCH() function for the first time, to what address the CPU returns when it executes the last instruction ret of SWITCH()? What location in the program that address is referred to?
(d) When the forked thread executes SWITCH() function for the first time, to what address the CPU returns when it executes the last instruction ret of SWITCH()? What location in the program that address is referred to?

 二、解答

1.追踪函数SWITCH和ThreadRoot的执行

  因为两个函数所在的文件为.s的汇编代码,所以用b + 函数名 命令先在SWITCH和ThreadRoot中设置断点,然后使用layout asm显示汇编代码,然后通过run执行。用到的gdb指令如下:

(gdb) b *函数名+偏移     设置断点
(gdb) layout asm 显示汇编代码
(gdb) i r 寄存器名称 查看寄存器的值
(gdb) x 偏移+$寄存器名称 查看地址和其指向的内存单元的值

 Ctrl + x 再按a 退出asm模式

 

2.回答以下问题

(a)找出以下函数的地址

使用p 函数名 命令查看函数的地址。

(b)找到主线程对象和子线程对象的地址

通过源码分析发现main函数首先调用了system.cc中的Initialize函数,因此给initialize函数设置断点并且逐步执行,
最终发现创建main线程对象的语句,用p命令查看其地址为0x8054af0。


继续执行发现子进程在ThreadTest.cc中的ThreadTest函数中被创建。同上可得子线程对象的地址为0x8054b50

(c)主线程第一次执行SWITCH函数最后一条语句后,返回的地址指向什么

分析汇编代码可知在<SWITCH+77>处,把(eax+20h)处的数据存放到了eax中最为返回值。根据汇编源码知(eax+20h)中储存的是该线程的下一条要执行的指令地址。通过x命令查看此时eax中的值发现其为函数ThreadRoot的地址。

(d)子线程第一次执行SWITCH函数最后一条语句后,返回地址指向什么

主线程切换到子线程需要执行一次SWITCH,之后子线程再调用一次SWITCH切回主线程,因此在SWITCH设置断点,执行run之后再执行continue,此时即为子线程调用SWITCH函数的情况。用上一题的方法查看此时的eax,发现其指向的是Scheduler的Run方法中的偏移为117的指令,为改指令设置断点,然后按Ctrl + x,再按a回到传统gdb模式,执行continue,发现此指令在scheduler.cc的116行代码中。

猜你喜欢

转载自blog.csdn.net/darord/article/details/83303765