You are on page 1of 22

Introduction to the Nachos OS

Simulator
UMN CSci 5103 - Operating Systems
Spring 2015

What is Nachos?
An instructional operating system simulator
Written in Java at UC Berkeley
Based on earlier C++ versions
Implements many aspects of real OS
Threads, processes
Timers, interrupts
Scheduling
Memory management
Simulates hardware
MIPS-based CPU
Can load and execute MIPS binaries as user
processes
Timer
Console

Why Nachos?
Learn OS internals through first-hand exposure
Designing
Implementing
Testing
Evaluating
Easier to focus on main concepts with a simulator
Still provides basic OS functionality, but easier to extend
than real kernel (e.g., Linux)
Java leads to simpler programming than other instructional
alternatives easier to focus on high-level concepts

Nachos Architecture

Downloading and Installing Nachos


Download the Nachos code from the course Moodle site.
Extract the tar file, which will create Nachos-Java directory
> tar -xvf nachos-os-simulator.tar.gz

Explore the Nachos-Java directory


Source code
Configuration files
Documentation

Compiling and Running


Add nachos/bin to your PATH(Modify bashrc
file). This directory contains the nachos script, which
launches Nachos for you.
To compile Nachos, go to the Nachos-Java directory
which contains a makefile to build the entire project
> cd Nachos-Java
> make
To run Nachos:
> nachos
Use the h flag to see the command-line parameters
Beware some are poorly documented. Youll have to

Configuration
Running Nachos requires a configuration file.
By default nachos reads from a configuration file
named nachos.conf in the same directory
Use the [] option to specify a different
configuration
Important Parameters
Kernel.kernel:

Always required. Specifies the kernel class to be used.

Machine.processor:

Specifies if processor should be simulated. Set it to true only


when running MIPS binaries as user processes

Kernel.scheduler:

Specifies the scheduler class to be used

Downloading and setting up MIPS


Cross Compiler
Download the appropriate MIPS cross compiler from

https://inst.eecs.berkeley.edu/~cs162/sp07/Nachos/
xgcc.shtml
Extract the downloaded tar file
Set the ARCHDIR environment variable to point to the
extracted directory (Modify bashrc file)
Add the extracted directory to PATH environment
variable (Modify bashrc file)

Example Programs
Threading
Use the config file nachos_thread.conf to run a
sample multi-threaded configuration
> nachos -[] nachos_thread.conf
Executes a simple thread program which loops 5
times
Change the number of threads using the
Kernel.numThreads parameter
User Process
Use the config file nachos_process.conf
Runs UserKernel.selfTest (echo
characters)
Then runs trivial MIPS program (halts)
Change the program to be executed with the

Nachos Components
Important Packages
nachos.machine:
Simulates machine hardware and devices such
as Processor, Console, Timer and interrupts
nachos.threads:
Simulates kernel level threads,
scheduling, synchronization primitives,
etc.
nachos.userprog:
Supports execution of MIPS binaries as userlevel processes
nachos.vm:
Basic implementation of virtual memory

Booting Nachos
nachos.machine.Machine is responsible for starting
and setting up nachos for execution.
Reads the config file and sets up hardware devices:
processor, interrupt controller, timer, console, file system
etc., based on config parameters.
Then starts Autograder, which creates kernel object
based on the kernel class specified on config file. This
starts the OS.
Kernel creates the scheduler (class specified in config file)
and starts executing threads (if any).

Interrupt Controller
Kicks off hardware interrupts.
Implemented by nachos.machine.Interrupt class.
Clock ticks after execution of instruction or re-enabling
of interrupts.
After every tick, Interrupt class checks for pending
interrupts and runs them.
Important methods accessible by other devices
schedule: schedule interrupt, specify time and
interrupt handler
enabled
disabled
tick

Timer
Implemented by nachos.machine.Timer
Causes timer interrupt at about every 500 ticks (can
be changed through code)
Important methods
setInterruptHandler: specify handler to tell the
timer what to do when it goes off
getTime: tells how many ticks so far
Provides support for preemption

Serial Console
Java interface nachos.machine.SerialConsole
readByte returns one byte (or -1) and waits to
interrupt when it has more
writeByte takes one byte and waits to interrupt when
its ready for more
setInterruptHandlers tells the console who to call
when it receives data or finishes sending data
Normally implemented by
nachos.machine.StandardConsole, hooked up to
standard input and standard output

Kernel
Abstract class nachos.machine.Kernel
Important methods
initialize: initializes the kernel
selfTest: performs test
run: runs any user code
terminate: terminate the OS
Extend the abstract Kernel class to implement kernellevel functionality
The code changes will be specific to programming
assignments
See nachos.threads.ThreadedKernel class for
reference implementation

Threading
Happens in package nachos.threads
All Nachos kernel-level threads are instances of
nachos.thread.KThread (or subclasses)
KThread
Status: New, Ready, Running, Blocked, Finished
nachos.machine.TCB responsible for thread-level
context switching
Internally implemented by Java threads
Uses java.lang.Runnable interface
Thread is implemented by java class implementing
Runnable interface
Thread's logic is implemented in run method

Creating and Running Threads


Create a thread class implementing
java.lang.Runnable
Instantiate a KThread object using an instance of this
thread class
Call Kthreads fork method.
Example:

class Sprinter implements Runnable {


public void run() {
// Run real fast!
}
}
Sprinter s = new Sprinter();
new KThread(s).fork();

Scheduling
Implemented by nachos.threads.Scheduler and
nachos.threads.ThreadQueue
Control access of threads to Processor, to synchronization
objects.
ThreadQueue object decides what thread to run next.
Default scheduler: RoundRobinScheduler
Custom scheduler: specify in configuration.
To implement a specific scheduling policy
Extend Scheduler and ThreadQueue classes.
Implement scheduling policy logic by implementing
methods such as nextThread, get/setPriority,
etc.
See RoundRobinScheduler class as an example.

User Processes
Nachos can run binaries compiled for MIPS as user
processes.
Write code in C, cross-compile to MIPS, and run as a
user process
You will need MIPS cross compiler (see PA0)
Enable machine.processor flag in config file
Supports only basic functionalities
Can execute basic MIPS instructions; no floating point.
Console class provides interface to stdin and stdout.
halt is the only syscall implemented for you.

Further Reading
Nachos Java API documentation:
http://inst.eecs.berkeley.edu/~cs162/sp14/Nachos/doc/index.html

Berkeleys CS 162 Nachos Tutorial (note our projects will


be different from theirs):
http://inst.eecs.berkeley.edu/~cs162/sp14/Nachos/nachos.
pdf

The source code.

General Advice
Read the source code!
Ask and answer (general) questions on the forum.
Do not rely on office hours or emails to the TA for
debugging.
Start early.

Questions?

You might also like