Nachos Intro

Embed Size (px)

Citation preview

  • 8/4/2019 Nachos Intro

    1/24

    Welcome to the World ofNachos

    CPS 110

    Spring 2004

    Discussion Session 1

  • 8/4/2019 Nachos Intro

    2/24

    Todays Objective

    Provide a basic understanding of thestructure of Nachos

    Get ready to get started on ProgrammingAssignment #1

  • 8/4/2019 Nachos Intro

    3/24

    What is Nachos?

    A real operating system

    Kernel runs on host hardware

    Kernel code is written in C++ User programs are written in ANSI C

    User programs are compiled for MIPS

    R2/3000, runs on MIPS emulator

  • 8/4/2019 Nachos Intro

    4/24

    Visualize Nachos

    User Programs

    Nachos

    Kernel

    MIPS HW

    Syscalls

    Machine instructions

    SPARC HW

    OS Kernel

    MIPS simNachos

    User Programs

    Syscalls

    Machine instructions

    MIPS instrNachos calls

    Abstract View Reality

  • 8/4/2019 Nachos Intro

    5/24

    Nachos: Simulated Devices

    Console terminal

    Disk

    Timer Network interface (not used)

  • 8/4/2019 Nachos Intro

    6/24

    What youll do with Nachos

    Start with very limited functionality Only one user program at a time

    Contiguous loading at low end of physical

    memory(no paging, process loads entire address space)

    no system calls are implemented(you implement them as a part of the labs)

    Improve Nachos by designing andimplementing missing and inadequateportions of the system

  • 8/4/2019 Nachos Intro

    7/24

    Nachos Assignments

    Lab 1: The Trouble with ConcurrentProgramming

    Lab 2: Threads and Synchronization Lab 3: Programming with Threads

    Lab 4: Multiprogrammed Kernel

    Lab 5: I/O Lab 6: Virtual Memory

    http://www.cs.duke.edu/education/courses/cps110/spring00/nachos-guide/nachos.11.htmlhttp://www.cs.duke.edu/education/courses/cps110/spring00/nachos-guide/nachos.11.htmlhttp://www.cs.duke.edu/education/courses/cps110/spring00/nachos-guide/nachos.12.htmlhttp://www.cs.duke.edu/education/courses/cps110/spring00/nachos-guide/nachos.13.htmlhttp://www.cs.duke.edu/education/courses/cps110/spring00/nachos-guide/nachos.14.htmlhttp://www.cs.duke.edu/education/courses/cps110/spring00/nachos-guide/nachos.15.htmlhttp://www.cs.duke.edu/education/courses/cps110/spring00/nachos-guide/nachos.16.htmlhttp://www.cs.duke.edu/education/courses/cps110/spring00/nachos-guide/nachos.16.htmlhttp://www.cs.duke.edu/education/courses/cps110/spring00/nachos-guide/nachos.15.htmlhttp://www.cs.duke.edu/education/courses/cps110/spring00/nachos-guide/nachos.14.htmlhttp://www.cs.duke.edu/education/courses/cps110/spring00/nachos-guide/nachos.13.htmlhttp://www.cs.duke.edu/education/courses/cps110/spring00/nachos-guide/nachos.12.htmlhttp://www.cs.duke.edu/education/courses/cps110/spring00/nachos-guide/nachos.11.htmlhttp://www.cs.duke.edu/education/courses/cps110/spring00/nachos-guide/nachos.11.html
  • 8/4/2019 Nachos Intro

    8/24

    Lab 1: The Trouble withConcurrent Programming

    Become familiar with Nachos and the behavior of a

    working (but incomplete) thread system

    Use what is supplied to experience the joys of concurrent

    programming

    All use of the Nachos thread primitives will be internal to

    your Nachos operating system kernel

    For now, you are using these internal Nachos primitives

    to create simple concurrent programs.

    Instructions on setting globals from the command line:

    http://www.cs.duke.edu/courses/spring01/cps110/globalset.html

    http://www.cs.duke.edu/education/courses/cps110/spring00/nachos-guide/nachos.11.htmlhttp://www.cs.duke.edu/education/courses/cps110/spring00/nachos-guide/nachos.11.htmlhttp://www.cs.duke.edu/courses/spring01/cps110/globalset.htmlhttp://www.cs.duke.edu/courses/spring01/cps110/globalset.htmlhttp://www.cs.duke.edu/education/courses/cps110/spring00/nachos-guide/nachos.11.htmlhttp://www.cs.duke.edu/education/courses/cps110/spring00/nachos-guide/nachos.11.html
  • 8/4/2019 Nachos Intro

    9/24

    Learning about Nachos

    You cannot learn all about software systems fromtextbooks Read the source code for systems that other people have

    written

    As soon as possible, begin reading over the NACHOSsource code

    Try to understand where the various pieces of the systemlive, and how they fit together

    It will take a while to develop an understanding.Don't worry!

  • 8/4/2019 Nachos Intro

    10/24

    Traversing the Nachos Files

    CVS This directory contains control information for the

    CVS source code management system. Similar

    subdirectories are present at each level of thetree. You should not change anything in thesedirectories. (optional)

    Makefile

    This file controls what happens when you typegmake in the code directory. It describes how tocompletely compile all the NACHOS code anduser application programs.

    http://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/CVShttp://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/Makefilehttp://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/Makefilehttp://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/CVS
  • 8/4/2019 Nachos Intro

    11/24

    Traversing the Nachos Files

    Makefile.common This file contains the lists of source and object

    files that go into making each version of

    NACHOS. Each time you add a new source orobject file of your own, you will have to editMakefile.common and add this file to theappropriate lists.

    Makefile.dep This file contains some system-dependent

    parameters that control the building of NACHOS.You probably don't need to change this.

    http://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/Makefile.commonhttp://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/Makefile.dephttp://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/Makefile.dephttp://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/Makefile.common
  • 8/4/2019 Nachos Intro

    12/24

    Traversing the Nachos Files

    threads

    This directory contains source code to supportthreads (lightweight processes) and

    synchronization. The threads support is fullyfunctional, though some of the synchronizationprimitives have not been implemented.Implementation and testing of these

    synchronization primitives will be part of your jobfor Homework #2.

    http://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/threadshttp://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/threads
  • 8/4/2019 Nachos Intro

    13/24

    Traversing the Nachos Files

    userprog This directory contains source code to support the loading

    and execution of user application programs.

    vm This directory will contain the source code for the virtual

    memory subsystem, when you implement it for Homework#6.

    filesys

    This directory contains source code for a ``stub''implementation of the NACHOS file system. Thisimplementation is very limited and incomplete. Your job inHomework #5 will be to rewrite and improve it.

    http://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/userproghttp://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/vmhttp://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/filesyshttp://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/filesyshttp://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/vmhttp://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/userprog
  • 8/4/2019 Nachos Intro

    14/24

    Traversing the Nachos Files

    test This directory contains source code for some

    simple NACHOS user application programs. It

    also contains Makefile for compiling theseprograms and converting them from COFF toNOFF.

    machine

    This directory contains source code for themachine emulator. You would need to look atsome of the header files in this directory, butdont modify anything here.

    http://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/testhttp://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/test/Makefilehttp://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/test/Makefilehttp://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/test/Makefilehttp://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/machinehttp://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/machinehttp://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/test/Makefilehttp://bsd7.starkhome.cs.sunysb.edu/~stark/CSE306/handouts/nachos/test
  • 8/4/2019 Nachos Intro

    15/24

    A Nachos Thread

    Thread* t

    machine state

    name/status, etc.

    fencepost

    0xdeadbeef

    Stack

    low

    highstack top

    unused region

    thread object

    or

    thread control block

    int stack[StackSize]

    t = new Thread(name);

    t->Fork(MyFunc, arg);;

    currentThread->Yield();

  • 8/4/2019 Nachos Intro

    16/24

    Thread Operations

    new thread - inits a thread control block

    Thread::Fork - runs a specified procedure inthe newly created thread (allocates stack andmakes ready to run)

    Thread::Finish - cleans up its state

    Thread::Yield - gives up CPU - makesrunning thread ready to run and invokesscheduler to choose new running thread

  • 8/4/2019 Nachos Intro

    17/24

    Threads in Nachos

    SPARC HW

    OS Kernel

    NachosSyscalls

    Machine instructions

    thread.h,cc

    scheduler.h, cc

  • 8/4/2019 Nachos Intro

    18/24

    Nachos Context Switches:Voluntary vs. Involuntary

    On a uniprocessor, the set of possible executionschedules depends on when context switches canoccur.

    Voluntary: one thread explicitly yields the CPU to another.

    A Nachos thread can suspend itself wth Thread::Yield.

    It may also blockto wait for some event with Thread::Sleep.

    Involuntary:the system schedulersuspends an active thread, and

    switches control to a different thread. Thread scheduler tries to share CPU fairly by timeslicing.

    Suspend/resume from a timer interrupt handler (e.g., nachos -rs)

    This can happen any time, so concurrency races can occur.

  • 8/4/2019 Nachos Intro

    19/24

    Blocking or Sleeping

    An executing thread may request some resource oraction that causes it to blockor sleepawaiting someevent.

    passage of a specific amount of time (a pause

    request) completion of I/O to a slow device (e.g., keyboard

    or disk)

    release of some needed resource (e.g., memory)

    In Nachos, threads block by callingThread::Sleep.

  • 8/4/2019 Nachos Intro

    20/24

    Blocking or Sleeping (cont)

    A sleeping thread cannot run until the eventoccurs.

    The blocked thread is awakened when theevent occurs. E.g., Wakeupor NachosScheduler::ReadyToRun(Thread* t)

    In an OS, processes may sleep while

    executing in the kernel to handle a system callor fault.

    N h Th d S

  • 8/4/2019 Nachos Intro

    21/24

    Nachos Thread StateTransitions

    Ready

    Running

    Blocked

    Scheduler::ReadyToRun

    Scheduler::Run

    Thread::Sleep

    Thread::Yield

    t = new Thread(name);t->Fork(MyFunc, arg);

    Thread::Finish

    N h Th d S

  • 8/4/2019 Nachos Intro

    22/24

    Nachos Thread StateTransitions

    Ready

    Runningin kernel

    Blocked

    Scheduler::ReadyToRun

    Scheduler::Run

    Thread::SleepThread::Yield

    t = new Thread(name);t->Fork(MyFunc, arg);

    Running

    user mode

    interrupt or

    exception

    Machine::Run

    Thread::Finish

  • 8/4/2019 Nachos Intro

    23/24

    new = scheduler->FindNextToRun(); /* get next ready thread */

    scheduler->Run(t); /* run it */

    Run calls SWITCH(currentThread, new) to suspend current thread andpass control to new thread.

    The Nachos Scheduler

    The core of Nachos is the Schedulerclass:

    one global shared schedulerobject

    pool of ready threads (the ready list)

    readyList

  • 8/4/2019 Nachos Intro

    24/24

    /** Save context of the calling thread (old), restore registers of* the next thread to run (new), and return in context ofnew.*/switch/MIPS (old, new) {

    old->stackTop = SP;save RA in old->MachineState[PC];save callee registers in old->MachineState

    restore callee registers from new->MachineStateRA = new->MachineState[PC];SP = new->stackTop;

    return (to RA)}

    ++A Nachos Context Switch

    Caller-saved registers (ifneeded) are already savedon the threads stack.

    Caller-saved regs restoredautomatically on return.

    Return to last procedure

    that called switch innew.

    Save current stack pointerand callers return address

    inoldthread object.

    Switch off ofoldstack and

    back tonew stack.