Transcript
Page 1: Operating System -  NachOS Project Introduction

Operating System - Operating System - NachOS Project IntroductionNachOS Project Introduction

教授:周立德教授:周立德助教:楊宏昌助教:楊宏昌

分散式系統實驗室 工五館 分散式系統實驗室 工五館 A312 A312 分機:分機: 3523135231Email: [email protected]: [email protected]

Page 2: Operating System -  NachOS Project Introduction

NachOSNachOS

Not Another Completely Heuristic ONot Another Completely Heuristic Operating Systemperating System

Written by Tom Anderson and his stuWritten by Tom Anderson and his students at UC Berkeley in 1992 dents at UC Berkeley in 1992 http://wwhttp://www.cs.washington.edu/homes/tom/nachos/w.cs.washington.edu/homes/tom/nachos/

Page 3: Operating System -  NachOS Project Introduction

What is NachOSWhat is NachOS

An educational OS used toAn educational OS used to– Teach monolithic kernel design and Teach monolithic kernel design and

implementationimplementation– Do experiments, you can (and will) Do experiments, you can (and will)

modify and extend itmodify and extend it FactFact

– Real hardware is difficult to handleReal hardware is difficult to handle– May break if handled wrongMay break if handled wrong

Page 4: Operating System -  NachOS Project Introduction

What is NachOSWhat is NachOS

ApproachApproach– Use a virtual MIPS machineUse a virtual MIPS machine– Provide some basic OS elementsProvide some basic OS elements

Includes many facets of a real OS:Includes many facets of a real OS:– ThreadsThreads– InterruptsInterrupts– Virtual MemoryVirtual Memory– I/O driven by interruptsI/O driven by interrupts

Page 5: Operating System -  NachOS Project Introduction

What is NachOSWhat is NachOS

NachOS also contains some hardwarNachOS also contains some hardware simulatione simulation– MIPS processorMIPS processor

Can handle MIPS code in standard COFF, exCan handle MIPS code in standard COFF, except for floating point instructionscept for floating point instructions

You can (and will) write code in C/C++, comYou can (and will) write code in C/C++, compile it to MIPS and run it on NachOSpile it to MIPS and run it on NachOS

– Console, Network interface, TimerConsole, Network interface, Timer

Page 6: Operating System -  NachOS Project Introduction

How does it workHow does it work

Run as a single UNIX processRun as a single UNIX process Provide a skeletal OS that Provide a skeletal OS that

supportssupports– ThreadsThreads– User-level processesUser-level processes– Virtual memoryVirtual memory– Interrupt-driven I/O devicesInterrupt-driven I/O devices

Page 7: Operating System -  NachOS Project Introduction

How does it workHow does it work

Two modes of executionTwo modes of execution– NachOS kernelNachOS kernel

Executes whenExecutes when– NachOS starts upNachOS starts up– A user-program causes a hardware trap (page faA user-program causes a hardware trap (page fa

ult, syustem call, etc.)ult, syustem call, etc.)

– MIPS simulatorMIPS simulator Initialized and started by NachOS kernelInitialized and started by NachOS kernel

Page 8: Operating System -  NachOS Project Introduction

How does it workHow does it work

Page 9: Operating System -  NachOS Project Introduction

How does it workHow does it work

Simulates MIPS architecture on host Simulates MIPS architecture on host system (UNIX / Linux / Windows / Masystem (UNIX / Linux / Windows / MacOS X)cOS X)

User programs need a cross-compileUser programs need a cross-compiler (target MIPS)r (target MIPS)

NachOS appears as a single threadeNachOS appears as a single threaded process to the host operating systed process to the host operating systemm

Page 10: Operating System -  NachOS Project Introduction

How does it workHow does it work

Page 11: Operating System -  NachOS Project Introduction

How does it workHow does it work

Page 12: Operating System -  NachOS Project Introduction

How does it workHow does it work

Page 13: Operating System -  NachOS Project Introduction

Root directory

C++ introduction to teach how to write C++

Root directory of Nachos’s source code

File system

Nachos’s library

MIPS machine

In/Out message queues

NachOS’s sample user programs

threads

Source code of NachOS kernel and MIPS simulator

The tool to convert user programs from MIPS’s COFF into NachOS’s NOFF (NachOS Object File Format ) format

User programs’ interfaces: system calls, address space, noff format

Source TreeSource Tree

Page 14: Operating System -  NachOS Project Introduction

System Start UPSystem Start UP

code/threads/main.cc main

> ./nachos start

create kernel object

initialize the system

Start up interrupt handling

initialize the ready queue and scheduler

initialize MIPS machine

Run some system testingInvoke user program

Load user program

Run user program

System halt and terminate

No user program

Page 15: Operating System -  NachOS Project Introduction

Install NachOSInstall NachOS

Platform: Linux or Linux over VMwarePlatform: Linux or Linux over VMware– RedHat Linux 9.0RedHat Linux 9.0 (ftp.tku.edu.tw) (ftp.tku.edu.tw)

Install stepsInstall steps– Get NachOS-4.0Get NachOS-4.0

wgetwget http://dslab.csie.ncu.edu.tw/OS2006/nachos-4.0.tar.gzhttp://dslab.csie.ncu.edu.tw/OS2006/nachos-4.0.tar.gz– Get Cross CompilerGet Cross Compiler

wget http://dslab.csie.ncu.edu.tw/OS2006/mips-decstation.linux-xgcc.twget http://dslab.csie.ncu.edu.tw/OS2006/mips-decstation.linux-xgcc.tgzgz

– Move Cross Compiler to /Move Cross Compiler to / mv ./mips-decstation.linux-xgcc.tgz /mv ./mips-decstation.linux-xgcc.tgz /

– Untar Cross CompilerUntar Cross Compiler tar zxvf /mips-decstation.linux-xgcc.tgztar zxvf /mips-decstation.linux-xgcc.tgz

Page 16: Operating System -  NachOS Project Introduction

Install NachOSInstall NachOS

– Untar NachOS-4.0Untar NachOS-4.0 tar zxvf ./nachos-4.0.tar.gztar zxvf ./nachos-4.0.tar.gz

– Make NachOS-4.0Make NachOS-4.0 cd ./nachos-4.0/codecd ./nachos-4.0/code makemake

– Test if installation is succeededTest if installation is succeeded cd ./userprogcd ./userprog ./nachos -e ../test/test1./nachos -e ../test/test1 ./nachos -e ../test/test2./nachos -e ../test/test2

– You should see the following…You should see the following…

Page 17: Operating System -  NachOS Project Introduction

The test1 resultThe test1 result

Total threads number is 1Total threads number is 1Thread ../test/test1 is executing.Thread ../test/test1 is executing.Print integer:9Print integer:9Print integer:8Print integer:8Print integer:7Print integer:7Print integer:6Print integer:6return value:0return value:0No threads ready or runnable, and no pending interruptNo threads ready or runnable, and no pending interrupt

s.s.Assuming the program completed.Assuming the program completed.Machine halting!Machine halting! Ticks: total 200, idle 66, system 40, user 94Ticks: total 200, idle 66, system 40, user 94Disk I/O: reads 0, writes 0Disk I/O: reads 0, writes 0Console I/O: reads 0, writes 0Console I/O: reads 0, writes 0Paging: faults 0Paging: faults 0Network I/O: packets received 0, sent 0Network I/O: packets received 0, sent 0

Page 18: Operating System -  NachOS Project Introduction

The test2 resultThe test2 result

Total threads number is 1Total threads number is 1Thread ../test/test2 is executing.Thread ../test/test2 is executing.Print integer:20Print integer:20Print integer:21Print integer:21Print integer:22Print integer:22Print integer:23Print integer:23Print integer:24Print integer:24Print integer:25Print integer:25return value:0return value:0No threads ready or runnable, and no pending interruptNo threads ready or runnable, and no pending interrupt

s.s.Assuming the program completed.Assuming the program completed.Machine halting!Machine halting!

Ticks: total 200, idle 32, system 40, user 128Ticks: total 200, idle 32, system 40, user 128Disk I/O: reads 0, writes 0Disk I/O: reads 0, writes 0

Page 19: Operating System -  NachOS Project Introduction

Operate NachOSOperate NachOS

NachOS command helpNachOS command help– ./nachos -h./nachos -h

Debugging modeDebugging mode– ./nachos -s./nachos -s

Execute files on NachOSExecute files on NachOS– ./nachos -e ../test/test1./nachos -e ../test/test1

Page 20: Operating System -  NachOS Project Introduction

Recompile Modified NachRecompile Modified NachOS CodeOS Code cd nachos-4.0/codecd nachos-4.0/code make clean (optional)make clean (optional) makemake

– If you want to fully re-compile the sourcIf you want to fully re-compile the source code, “make clean” is required. Or e code, “make clean” is required. Or make will only re-compile the modified make will only re-compile the modified and related files. (save time)and related files. (save time)

Page 21: Operating System -  NachOS Project Introduction

Trace NachOSTrace NachOS

Read *.h and *.cc to have an overvieRead *.h and *.cc to have an overview about the whole system and see hw about the whole system and see how it is implementedow it is implemented

Documentation (A Road Map ThrougDocumentation (A Road Map Through NachOS)h NachOS)– http://www.cs.duke.edu/~narten/110/nhttp://www.cs.duke.edu/~narten/110/n

achos/main/main.htmlachos/main/main.html

Page 22: Operating System -  NachOS Project Introduction

Assignment 1Assignment 1

NachOS NachOS 介紹介紹 安裝 安裝 NachOSNachOS ,環境,過程,環境,過程 把安裝過程抓圖作成說明文件 把安裝過程抓圖作成說明文件 (word (word

檔檔 )) 把所有在 把所有在 linux linux 下用到的指令解釋清楚下用到的指令解釋清楚

其用法,並舉例說明其用法,並舉例說明

Page 23: Operating System -  NachOS Project Introduction

Assignment 2Assignment 2

到 到 nachos-4.0/code/userprog nachos-4.0/code/userprog 目錄目錄下執行下執行– ./nachos –e ../test/test1 –e ../test/test2./nachos –e ../test/test1 –e ../test/test2Total threads number is 2Total threads number is 2Thread ../test/test1 is executing.Thread ../test/test1 is executing.Thread ../test/test2 is executing.Thread ../test/test2 is executing.Print integer:9Print integer:9Print integer:8Print integer:8Print integer:7Print integer:7Print integer:20Print integer:20Print integer:21Print integer:21Print integer:22Print integer:22

Page 24: Operating System -  NachOS Project Introduction

Assignment 2 (cont.)Assignment 2 (cont.)

從上述的輸出,可知結果有誤從上述的輸出,可知結果有誤 Please trace the following files to see wPlease trace the following files to see w

hy the output results are wronghy the output results are wrong– nachos-4.0/code/userprog/addrspace.hnachos-4.0/code/userprog/addrspace.h– nachos-4.0/code/userprog/addrspace.ccnachos-4.0/code/userprog/addrspace.cc– nachos-4.0/code/userprog/userkernel.ccnachos-4.0/code/userprog/userkernel.cc– nachos-4.0/code/machine/translate.hnachos-4.0/code/machine/translate.h– Nachos-4.0/code/machine/translate.ccNachos-4.0/code/machine/translate.cc

Page 25: Operating System -  NachOS Project Introduction

Assignment 2 (cont.)Assignment 2 (cont.)

Please modified the Nachos code to ruPlease modified the Nachos code to run the correct resultsn the correct results

You may need to modify the following You may need to modify the following functionsfunctions– nachos-4.0/code/userprog/addrspace.ccnachos-4.0/code/userprog/addrspace.cc

constructorconstructor destructordestructor initRegisterinitRegister LoadLoad

Page 26: Operating System -  NachOS Project Introduction

Assignment 3Assignment 3

Implement system call “Sleep”Implement system call “Sleep” Implement Shortest-Job-First Implement Shortest-Job-First

schedulingscheduling

Page 27: Operating System -  NachOS Project Introduction

System call “Sleep”System call “Sleep”

請實作 請實作 Sleep(int x) Sleep(int x) 這個 這個 system call, system call, 把呼叫這個 把呼叫這個 system call system call 的 的 thread blthread blockock 住住 , , 並且在 並且在 x x 次的 次的 timer interrutimer interrupts pts 以後才又回到 以後才又回到 READY READY 的狀態。的狀態。

Page 28: Operating System -  NachOS Project Introduction

System call “Sleep” System call “Sleep” (cont.)(cont.) 修改 修改 exception.cc, syscall.h, start.sexception.cc, syscall.h, start.s 呼叫 呼叫 alarm.cc alarm.cc 的 的 WaitUntil(int x) WaitUntil(int x) 來來

處理 處理 Sleep(int x) Sleep(int x) 這個 這個 system callsystem call

Page 29: Operating System -  NachOS Project Introduction

SJF SchedulingSJF Scheduling

NachOS NachOS 內定的 內定的 scheduling algorithscheduling algorithm m 是 是 Round-RobinRound-Robin ,請設計 ,請設計 non-prnon-preemptive SJF Schedulingeemptive SJF Scheduling

請自行設計一組或多組 請自行設計一組或多組 test case test case 來證來證明你的 明你的 project project 是對的,針對 是對的,針對 RR RR 或或是 是 SJF SJF 都要能運作,放到 都要能運作,放到 NachOS NachOS 下面去執行的 下面去執行的 threadthread ,個數至少 ,個數至少 3 3 個 個

Page 30: Operating System -  NachOS Project Introduction

SJF Scheduling (cont.)SJF Scheduling (cont.)

由於是 由於是 non-preemptivenon-preemptive ,所以當 ,所以當 timtimer interrupts er interrupts 時不用將 時不用將 thread yieldthread yield– 什麼時候有可能換 什麼時候有可能換 thread thread 執行?執行?

需要記錄每個 需要記錄每個 thread thread 實際 實際 CPU burst CPU burst 長度 長度 (timer interrupts (timer interrupts 數目數目 ), ), 與預測與預測將來 將來 CPU burst CPU burst 長度長度– 在哪裡可以得到這個資訊?在哪裡可以得到這個資訊?– 何時該進行預測?何時該進行預測?

Page 31: Operating System -  NachOS Project Introduction

SJF Scheduling (cont.)SJF Scheduling (cont.)

Begin Running

Invoke Sleep(x)

Per timer interrupt:1.Record actual CPU burst2. 叫醒應該起床的 threads

1.Set next predicted CPU burst2.Insert this thread to Sleeping thread lists3.Invoke thread->Sleep

Page 32: Operating System -  NachOS Project Introduction

DemoDemo

Accompany with your report about Accompany with your report about what did you done to the NachOS sowhat did you done to the NachOS source code (why and how), and what urce code (why and how), and what did you get when tracing the NachOS did you get when tracing the NachOS source code.source code.

把寫好的把寫好的 source codesource code 與報告與報告 (word(word檔檔 )) 壓縮成 ”學號壓縮成 ”學號 .tar.gz”.tar.gz” ,, email temail to: o: [email protected]@dslab.csie.ncu.edu.tw

Page 33: Operating System -  NachOS Project Introduction

Demo (cont.)Demo (cont.)

郵件主旨請定為郵件主旨請定為– NachOS project report NUMNachOS project report NUM ,學號:姓,學號:姓

名名 助教收到 助教收到 email email 後會回信表示收到後會回信表示收到

– 如果一週內沒收到回信,請再寄一次如果一週內沒收到回信,請再寄一次


Recommended