操作系统实验一
The purpose of this laboratory is understand how context switch is realized in Nachos by tracing the Nachos test program in both C++ and the machine code levels.
答案:实验环境初始化:将nachos的源码拷到Ubuntu的一个目录下,进入threads文件夹make,会产生nachos可运行文件,用./nachos运行。
Your tasks of this lab session is to
1. trace the execution of Nachos and observe the executions of
(a) context switch function SWITCH()
(b) function ThreadRoot()
using gdb
答案:在当前的目录下用gdb nachos 打开,然后在两个函数上设置断点,如break SWITCH..然后run,用next单步执行,代码会在断点处停下,然后Continue,用Step单步查看函数的运行。
answer the following questions:
(a) What are the addresses of the following functions in your Nachos:
i. InterruptEnable()
ii. SimpleThread()
iii. ThreadFinish()
iv. ThreadRoot()
答案:在gdb下break相应函数名,gdb会返回相应的地址信息,例如i Ox804a192:file thread.cc,line242
(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
在main方法下加断点,不断Step找到149行main对象。返回答案
I:Thread(this=ox80500e8,threadName=Ox804bc96 “main”)
II:Thread(this=Ox8050148,threadName=Ox804bda “forbed thread”)
(c) When the main thread executes SWITCH() function for the _rst 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?
答案: Gdb nachos,接着b SWITCH,程序在SWITCH函数处停下,用ni让gdb单步运行编译指令,大约按10下左右,gdb返回ret的堆栈地址。
(d) When the forked thread executes SWITCH() function for the _rst 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?
答案:由于forked thread 和main程序之间是交替运行5次,(c)问题基础下用c,切换到forked thread,同样用ni单步运行编译程序到ret得到堆栈的返回地址。