nachos projects overview and project 1cc.ee.ntu.edu.tw/~farn/courses/os/os2011/hw/proj1.pdf ·...

28
Nachos Projects Overview and Project 1 TA : 沙政亞 2011/10/18

Upload: others

Post on 27-Jun-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Nachos Projects Overview and Project 1

TA : 沙政亞

2011/10/18

Page 2: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Outline

What’s Nachos?

Why Do We Use Nachos?

Nachos Project Overview

Nachos Installation

Nachos Project 1

Reference

Page 3: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

NACHOS

Page 4: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

What’s Nachos?

Nachos Not Another Completely Heuristic Operating System

Developed by Tom Anderson and his students at UC, Berkeley. http://www.cs.washington.edu/homes/tom/nachos/

Page 5: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

What’s Nachos?

Nachos is an instrumental software that allows students to study and modify a “real” operating system.

The only difference between Nachos and a real operating system is that Nachos runs a Unix process, whereas a real operating system runs on a real machine.

Page 6: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

What’s Nachos?

Nachos simulates a MIPS machine (MIPS R2/3000) on the host system (Unix, Linux, etc.)

Conceptually, Nachos has two modes of execution, namely, MIPS simulator and Nachos “kernel”

Reference : http://course.cs.ust.hk/comp252/web07fall/Lab1/Nachos%20architecture/nachos.ps

Page 7: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Why do we use Nachos?

For understanding operating system concepts, nothing is better than building an operating system.

Nachos is simpler than UNIX.

Simulator makes debugging easier.

Deterministic

Page 8: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Nachos Project Overview

The project contains three phases, corresponding to each of the major pieces of a modern OS.

Thread management

File Systems and Virtual Memory

Networking

Page 9: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Nachos Installation

Platforms you may use Unix, Linux.

Recommend: Ubuntu Linux over Virtualbox if you’re on Windows http://yuan817.blogspot.com/2011/09/ubuntuvirtualbox.html

http://www.prudentman.idv.tw/2011/09/virtualboxguest-additionsubuntu.html

Compiler g++ 4.x.x

Checking g++ version

g++ -v

Page 10: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Nachos Installation

Cross compiler wget http://cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/nachos/mips-decstation.linux-xgcc.tgz

sudo mv ./mips-decstation.linux-xgcc.tgz /

cd /

sudo tar zxvf mips-decstation.linux-xgcc.tgz

Nachos

wget http://cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/nachos/nachos-3.4.tar.gz

tar -zxvf nachos-3.4.tar.gz

cd {NACHOS}/code/

make

Page 11: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Some Important Things

Nachos arguments Different execution status

If you run Nachos by “nachos -rs #”, with a different number each time, calls to Thread::Yield will be inserted at different places in the code. Hence simulate different scheduling of threads.

Recompilation of modified nachos code cd {NACHOS}/code

make clean (optional)

make

Page 12: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

PROJECT 1

Page 13: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Project 1

Abstract You are given part of a working thread system; your job is

to complete it, and then to use it to solve several synchronization problems.

Try the existing thread system cd {NACHOS}/code/threads

./nachos

Add to PATH environmental variables

export PATH=$PATH:{NACHOS}/code/threads

Page 14: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Existing Thread Systemads901119 ~/os/nachos-3.4/nachos-3.4/code/threads/

$ ./nachos

*** thread 0 looped 0 times

*** thread 1 looped 0 times

*** thread 0 looped 1 times

*** thread 1 looped 1 times

*** thread 0 looped 2 times

*** thread 1 looped 2 times

*** thread 0 looped 3 times

*** thread 1 looped 3 times

*** thread 0 looped 4 times

*** thread 1 looped 4 times

No threads ready or runnable, and no pending interrupts.

Assuming the program completed.

Machine halting!

Ticks: total 130, idle 0, system 130, user 0

Disk I/O: reads 0, writes 0

Console I/O: reads 0, writes 0

Paging: faults 0

Network I/O: packets received 0, sent 0

Cleaning up...

Page 15: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

What should you do?

Trace Nachos Read and understand the partial thread system provided to

you. The thread system implements thread fork, thread completion, along

with semaphores for synchronization.

Trace the execution path for the simple test case. (./nachos)

You might use some documentations to help yourself understand the system. A Road Map Through Nachos

http://cc.ee.ntu.edu.tw/~farn/courses/OS2010/ref/Nachos%20Road%20Map.pdf

Page 16: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Requirement 1/4

Implement locks and condition variables, using semaphores as a building block. We have provided the public interface to locks and

condition variables in synch.h. You need to define the

private data and implement the interface. Note that it should not take you very much code to implement either locks or condition variables.

Page 17: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Requirement 2/4

Implement send and receive, using condition variables. Messages are sent on “ports”, which allow senders and receivers

to synchronize with each other. Send(port, int msg) atomically waits until Receive(port, int *msg) is called on the same port, and then copies the msg into the Receive buffer. Once the copy is made, both can returen. Similarly, the Receive waits until Send is called, at which point the copy is

make, and both can return. (Essentially, this is equivalent to a 0-length bounded buffer.) Your solution should work even if there are multiple Senders and Receivers for the same port.

Page 18: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Requirement 3/4

Implement Thread::Join() in Nachos. Add an argument to the thread constructor that says

whether or not a Join will be called on this thread. Your

solution should properly delete the thread control block whether or not Join is to be called, and whether or not the forked thread finishes before the Join is called.

Page 19: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Requirement 4/4

Implement an “alarm clock” class. Threads call Alarm::GoToSleepFor(int howLong) to go to sleep

for a period of time. The alarm clock can be implemented using the hardware Timer device (cf. timer.h). When the timer interrupt goes off, the Timer interrupt handler checks to see if any thread that had been asleep needs to wake up now. There is no requirement that threads start running immediately after waking up; just put them on the ready queue after they have waited for the approximately the right amount of time.

Page 20: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Hints The important files are: {NACHOS}/code/threads/

main.cc, threadtest.cc

simple test of the thread routines.

thread.h, thread.cc

thread data structures and thread operations such as thread fork, thread sleep and thread finish

scheduler.h, scheduler.cc

manages the list of thread that are ready to run

synch.h, synch.cc

synchronization routines: semaphores, locks and condition variables.

list.h, list.cc

generic list management in C++

synchlist.h, synchlist.cc

synchronized access to lists using locks and condition variables (useful as an example of the use of synchronization primitives).

Page 21: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Hints system.h, system.cc

Nachos startup/shutdown routines

utility.h, utility.cc

some useful definitions and debugging routines

switch.h, switch.s

assembly language for starting up threads and context switching between them

interrupt.h, interrupt.cc

manage enabling and disabling interrupts as part of the machine emulation

timer.h, timer.cc

emulate a clock that periodically causes an interrupt to occur

stats.h

collect interesting statistics

Page 22: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Hints Properly synchronized code should work no matter what

order the scheduler chooses to run the threads on the ready list. In other words, we should be able to put a call to Thread::Yield (causing the scheduler to choose another thread to run) anywhere in your code where interrupts are enabled without changing the correctness of your code. To aid you in this, code linked in with Nachos will cause

Thread::Yield to be called on your behalf in a repeatable but

unpredictable way. Nachos code is repeatable in that if you call it repeatedly with the same argument, it will do exactly the same thing each time. However, if you invoke “nachos -rs #”, with a different number each time, calls to Thread::Yield will be inserted at different places

in the code.

Page 23: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Hints When you trace the execution path, it is helpful to keep

track of the state of each thread and which procedures are on each thread’s execution stack. You will notice that when one thread calls SWITCH, another thread starts running, and the first thing the new thread does is to return from SWITCH. This may seem strange to you at this point, but you will understand threads once you understand why the SWITCH that gets called is different from the SWITCH that returns.

Page 24: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Hints In the Nachos implementation of threads, each thread is

assigned a small, fixed-sized execution stack. This may cause bizarre problems (such as segmentation faults at strange lines of code) if you declare large data structures to be automatic variables (e.g., “int buf[1000]”). If

you encounter this problem, you may change the size of the stack by modifying the StackSize defined in switch.h.

Page 25: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Presentation Requirements

Prepare a presentation about

How you modified Nachos

Everything you consider important

11/22 on the class

Page 26: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Hand in your reports.

Please put the following in a directory and compress it to zip/rar/7z/gz...

modified source code(s) with path

presentation slides

final report

E-mail the file (project1_b99901000.zip) to TA.

Deadline: 2011/11/22 23:59

Page 27: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Grading Policy

Correct Result 40%

Report 60%

Page 28: Nachos Projects Overview and Project 1cc.ee.ntu.edu.tw/~farn/courses/OS/OS2011/hw/proj1.pdf · Requirement 1/4 Implement locks and condition variables, using semaphores as a building

Reference

Author’s Web Page http://www.cs.washington.edu/homes/tom/nachos/

Thomas Narten's Nachos Roadmap http://www.cs.duke.edu/~narten/110/nachos/main/main.html