C/C++ 编程代写
当前位置:以往案例 > >案例CS:操作系统OS案例C++/C语言lab1北美案例
2018-05-26



Download Nachos.tar from the blackboard and unpack it. Answer all of the following questions by ining the Nachos code (all sub-directories of code directory).


(1) (5pts) What are the possible NachosProcess (called Thread) states and where are they defined (filename & line number)?

ANS:

JUST_CREATED, RUNNING, READY, BLOCKED

In file code/threads/thread.h, line 63


(2) (5 pts) In threadtest.cc, how many lines are printed when you change:

for(int num = 0; num < 5; num++)

to
for(int num = 0; num < which; num++)

Why is that?

ANS:

1 line. The thread t starts with parameter 1, which prints 1 line. And the call SimpleThread(0) doesn’t do anything.


(3) (10 pts) Draw the call graph of creating a new NachoProcess with SimpleThread function.

ANS:

The first call to new a Thread object simply sets its initial value and makes no further function call. What matters is the latter Fork:



These codes are in code/threads/thread.cc – Thread::Fork

(4) (25 pts) Using flags will help you design and test your operating system (later in other labs and programming projects).

To use a flag: ./nachos -flag

ine source code for all the predefined flags and discuss briefly how Nachos operates and what information is displayed if it exists for each flag and its parameter(s).

ANS:

Those flags are described in code/threads/main.cc.

l -d is the debug flag, and its flag parameters is defined in code/lib/debug.h. For ple, “-d sf” means debug information of semaphores and file system is printed during the OS operation. To print debug information for, e.g. file system, “DEBUG(‘f’, “message”);” would print the message if ‘f’ is included in flag parameters.

l -z prints copyright string in copyright.h.

l -x would load a user program from disk and executes it.

l -K calls the ThreadTest() function that test thread functions.

l -C calls the ConsoleTest() that tests the console’s IO.

l -N tests network communication between host 0 and host 1.

The following flags requires nachos file system to be enabled, using the compile flag.

l -cp copies file from UNIX file system to nachos file system.

l -p prints file content of the specific file.

l -r removes file from file system.

l -l is similar to ls in bash.

l -D prints entire content of the file system, including bitmap’s header, directory’s header, and their content.

(5) (10pts) What system calls are currently implemented in Nachos? Explain what it does in detail and find a corresponding UNIX system call if exists.

ANS:

SysHalt and SysAdd, called in userprog/exception.cc and implemented in ksyscall.h.

SysHalt calls kernel->interrupt->Halt() directly, which would shutdown the system. Similar to UNIX system call halt.

SysAdd sums parameters and return the result.

(6) (15pts) What would happen when you execute the halt program (code/test/halt.c) – ./nachos -x ~/test/halt? Explain why it happens. Draw the call graph from a user program (halt) instruction to running a system call implementation.

ANS:

First the program file “halt” is loaded into an address space, then the program in “halt” is executed(the main function in halt.c is called). In the main function of halt.c, Halt() is called, which is declared in syscall.h. Its implementation is written in start.S in code/test and is written in assembly.



It first put the system call number to register 2, then call syscall, which would generate an interrupt and is captured and handled in ExceptionHandler() in userprog/exception.cc. It first check the exception type, and finds out that it’s a system call, and use the value in register 2 as the call number, then call SysHalt() defined in userprog/ksyscall.h.

(7) (10 pts) What would happen when you execute the shell program (code/test/shell.c)? Explain why it happens.

ANS:

“Unexpected system call” and system exits.

Though Write interface is defined in syscall.h and start.S, the system call number it uses and captured by ExceptionHandler does not have an corresponding code, i.e. there is no case SC_Write in ExceptionHandler.

(8) (5pts) Which emulated MIPS CPU register contains the system call code?

ANS:

Register 2. See code/userprog/exception.cc – line 54.


(9) (15 pts) Explain the difference between the threads created in ThreadTest() and the thread created for executing halt (or shell).
ANS:

The thread created in ThreadTest() is created by Fork, which makes it a whole new ready-to-run thread in the scheduler’s ready list. That thread can be executed is because the scheduler schedules it up.

The thread created for executing halt is actually the same thread threads/main.cc’s main() function uses. The halt file is loaded into a new addrspace, and the addrspace of current thread is replaced into the new one, i.e. the code in the main()’s thread is replaced into the code in file halt.

在线提交订单