Nachos Instructional OS

Slides:



Advertisements
Similar presentations
Operating Systems ECE344 Midterm review Ding Yuan
Advertisements

More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
Processes CSCI 444/544 Operating Systems Fall 2008.
TTIT61 Nachos - short introduction Gert Jervan IDA/SaS/ESLAB.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Nachos Instructional OS and Project 1 CS 170, Tao Yang.
Welcome to the World of Nachos CPS 110 Spring 2004 Discussion Session 1.
CSC 501 Lecture 2: Processes. Process Process is a running program a program in execution an “instantiation” of a program Program is a bunch of instructions.
Threads Many software packages are multi-threaded Web browser: one thread display images, another thread retrieves data from the network Word processor:
Threads and Concurrency. A First Look at Some Key Concepts kernel The software component that controls the hardware directly, and implements the core.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto OS-Related Hardware.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Chapter 1 Process and Thread. 1.2 process The address space of a program – Text – Code – Stack – Heap A set of registers – PC – SP Other resources – Files.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Thread Scheduling.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Race Conditions Defined 1. Every data structure defines invariant conditions. defines the space of possible legal states of the structure defines what.
Project 2: Initial Implementation Notes Tao Yang.
Nachos Tutorial Courtesy: University of Waterloo.
Nachos Instructional OS CS 270, Tao Yang, Spring 2011.
CPS110: Implementing threads Landon Cox. Recap and looking ahead Hardware OS Applications Where we’ve been Where we’re going.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
CS140 Project 1: Threads Slides by Kiyoshi Shikuma.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Introduction to Operating Systems and Concurrency.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Nachos Lecture 2 Xiaorui Sun. Phase 2 You have got one machine (machine package) You have to implements the incomplete OS (userprog package) Run programs.
2 Processor(s)Main MemoryDevices Process, Thread & Resource Manager Memory Manager Device Manager File Manager.
1 Lecture 6 “Nachos” n nachos overview n directory structure n nachos emulated machine n nachos OS n nachos scheduler n nachos threads.
NachOS Threads System Road Map through the Code Lab 3.
Nachos Project 2 Lecturer: Hao-Hua Chu TA: Chun-Po Wang (Artoo) Date: 2008/10/14 Material Provided by Yuan-Hao Chang, Yung-Feng Lu.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
Processes and threads.
Process Tables; Threads
Process Management Process Concept Why only the global variables?
CPS110: Threads review and wrap up
CS 6560: Operating Systems Design
CSCS 511 Operating Systems Ch3, 4 (Part B) Further Understanding Threads Dr. Frank Li Skipped sections 3.5 & 3.6 
Operating Systems: A Modern Perspective, Chapter 6
Intro to Processes CSSE 332 Operating Systems
Nachos Threads and Concurrency
CSCI 511 Operating Systems Ch3, 4 (Part B) Further Understanding Threads Dr. Frank Li Skipped sections 3.5 & 3.6 
Process Tables; Threads
Nachos Assignment#2 Priority Scheduling.
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Mid Term review CSC345.
Interrupts and Exception Handling
Threads Chapter 5 2/17/2019 B.Ramamurthy.
Threads Chapter 5 2/23/2019 B.Ramamurthy.
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
CSE 451: Operating Systems Autumn 2001 Lecture 2 Architectural Support for Operating Systems Brian Bershad 310 Sieg Hall 1.
Still Chapter 2 (Based on Silberchatz’s text and Nachos Roadmap.)
Threads and Concurrency
February 5, 2004 Adrienne Noble
Implementing Processes, Threads, and Resources
Processes in Unix and Windows
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
CS510 Operating System Foundations
CS333 Intro to Operating Systems
Implementing Processes, Threads, and Resources
Chapter 3: Process Management
Presentation transcript:

Nachos Instructional OS Tao Yang 11/17/2018 Nachos Instructional OS CS 170, Tao Yang, Fall 2011 Thank you for the introduction. In this presentation, I plan to talk about my research on cluster-based network services. My work has been focusing on three specific aspects: service replication, load-balancing support, and a QoS-aware resource management framework. This study was supervised by my advisor, Tao Yang, at UC Santa Barbara.

What is Nachos OS? Allow students to examine, modify and execute operating system software. A skeletal OS that supports kernel threads user-level processes Simulates MIPS instruction execution. Running on a virtual machine, executed as a single Unix process in a hosting OS. Over 9K lines of C++ code. Can understand its basic mechanisms by reading about 1-2K lines of code. 11/17/2018

System Layers Nachos kernel threads Thread 1 Thread 2 Thread N User process User process Nachos kernel threads Thread 1 Thread 2 Thread N Nachos OS modules (Threads mgm, File System, Code execution/memory mapping, System calls/Interrupt) Simulated MIPS Machine (CPU, Memory, Disk, Console) Base Operating System (Linux for our class)

Steps to Install Nachos Obtain and install Nachos source code. Copy the source code from ~cs170/nachosSept20.tar.gz Compile the source code with Makefile Run Nachos demo Under the threads subdirectory (run threads) Under the userprog subdirectory (compile/execute binary code) Project 0: Form a group/turn-in proj 0. Due on Oct 4. 11/17/2018

Nachos code directory machine --- Basic machine specification (MIPS simulator). threads --- threads management (HW1). userprog -- binary code execution and system calls (HW2). vm -- virtual memory (HW3). filesys -- file system (HW3) test -- binary test code network -- networking protocol bin -- utilities/tools (binary format conversion) 11/17/2018

Source code reading and HW1 Objectives of next 2 weeks Scan through ~1,000-2,000 lines of code under threads directory Learn how context switch is accomplished among threads. Learn how thread scheduling is done. Learn how locks/synchronization are implemented and used. Complete HW1 Few hundred lines of code Sample code is available 11/17/2018

Single-threaded vs multithreaded Process 11/17/2018

Sample Example of Nacho Threads int shared=1; main () { Thread *t1 = new Thread("forked thread1"); Thread *t2 = new Thread("forked thread2"); t1->Fork(SimpleThread, 1); t2->Fork(SimpleThread, 2); SimpleThread(3); } SimpleThread(int i) printf(“Hello %d. Shared %d\n”, i, shared); currentThread->Yield(); Create 2 new threads. Start to fork and execute a function in each child thread. Parent also executes the same function Function executed by threads

Nachos Threads Each thread has the following TCB (thread control block) from thread.h int *stackTop; // stack pointer int machineState[18]; // copy of registers int *stack // bottom of stack ThreadStatus status; //ready, running, or blocked char *name; Thread operations: Thread(char *debugName). Create a thread. Fork(VoidFunctionPtr func, int arg). Let a thread execute a function. Yield(). Suspend the calling thread and select a new one for execution. Sleep(). Suspend the current thread, change its state to BLOCKED, and remove it from the ready list Finish() 11/17/2018

Nachos Thread States and Transitions running (user) When running in user mode, the thread executes within the machine simulator. HW 2 covers this. In HW 1 we are only concerned with the states in this box. Machine::Run, ExceptionHandler interrupt or exception running (kernel) Thread::Yield Thread::Sleep Scheduler::Run blocked ready Scheduler::ReadyToRun

Reading source code Have a picture of overall components and their roles. Nacho code structure Be able to compile and run a simple case. Follow Project 0 and execute two cases. Trace the code execution through a simple case Basic/high level mechanism involved in execution. Walk through a simple thread test case in Nachos. Week 2 link in http://www.cs.ucsb.edu/~cs170/refer.html 11/17/2018

Key steps when Nachos executes After you type ``nachos'' under threads subdirectory: It is executing as a single Unix process. The main() calls Initialize() to start up interrupt handling, create a scheduler for managing the ready queue. ThreadTest() (to be explained for HW 1). currentThread->Finish() to let other threads continue to run. 11/17/2018

Key Calling graph when Nachos executes under thread directory All files are in threads directory. StackAllocate() in thread.cc Thread:Fork () in thread.cc Initialize() in system.cc FindNextToRun () in scheduler.cc ReadyToRun () Run () SWITCH () in switch.s ThreadRoot () func() such as SimpleThread() in ThreadTest.cc main() in main.cc Thread:Yield () in thread.cc ThreadTest () in threadtest.cc currentThread->Finish () in threadtest.cc 11/17/2018

Scheduler object for thread scheduling A scheduler decides which thread to run next by scanning the ready list. The scheduler is invoked whenever the current thread gives up the CPU. The current Nachos scheduling policy is round-robin: new threads are appended to the end of the ready list, and the scheduler selects the front of the list. The Scheduler object has the following operations: ReadyToRun(Thread *thread). Make thread ready to run and place it on the ready list. Thread *FindNextToRun() Run(Thread *nextThread) 11/17/2018

Thread Switching Switching involves suspending current thread, saving its state, and restoring the state of new thread. Following code involved in execution: the old code, the new code, and the code that performs switching. Switch(oldThread, newThread): Save all registers in oldThread's context block. Save the program address to be used when the old thread is resumed. Load new values into the registers from the context block of the new thread. Once the saved PC of the new thread is loaded, Switch() is no longer executing. 11/17/2018

Key operations of SWITCH() Thread->Fork(func(), arg) Call ThreadRoot () in switch.s Save current thread context Load target SWITCH () in switch.s Call ThreadFinish() InterruptEnable() func(arg) func(arg) 11/17/2018

Assembly code of SWITCH(oldThread, nextThread) # a0 -- pointer to old thread’s TCB a1 -- pointer to new thread’sTCB Save register values to current thread’s TCB sw sp, SP(a0) # save new stack pointer sw s0, S0(a0) sw s1, S1(a0) sw s2, S2(a0) sw s3, S3(a0) sw s4, S4(a0) sw s5, S5(a0) sw s6, S6(a0) sw s7, S7(a0) sw fp, FP(a0) sw ra, PC(a0) # save return address sp s0 s1 s2 … stackTop machineState[0] machineState[1] machineState[2] a0 11/17/2018

Assembly code of SWITCH(oldThread, nextThread) # a0 -- pointer to old thread’s TCB a1 -- pointer to new thread’sTCB Load registers from new thread’s TCB lw sp, SP(a1) # load the new stack pointer lw s0, S0(a1) lw s1, S1(a1) lw s2, S2(a1) … lw s7, S7(a1) lw fp, FP(a1) lw ra, PC(a1) # load the return address j ra #Call ra which is ThreadRoot(); sp s0 s1 s2 s3 stackTop machineState[0] machineState[1] machineState[2] machineState[3] … machineState[9] ra a1 11/17/2018

Assembly code of SWITCH(oldThread, nextThread) # a0 -- pointer to old thread’s TCB a1 -- pointer to new thread’sTCB Load registers from new thread’s TCB lw sp, SP(a1) # load the new stack pointer lw s0, S0(a1) lw s1, S1(a1) lw s2, S2(a1) … lw s7, S7(a1) lw fp, FP(a1) lw ra, PC(a1) # load the return address j ra #Call ra which is ThreadRoot(); sp s0 s1 s2 s3 stackTop func() arg ThreadFinish() InterruptEnable() … ThreadRoot() addr ra a1 11/17/2018

Who fills values of the new thread TCB properly? StackAllocate() in Thread::Fork() fills values of machineState[]; machineState[PCState] = (int) ThreadRoot; // PCState=9 machineState[StartupPCState] = (int) InterruptEnable; //StartupPCState=3 machineState[InitialPCState] = (int) func; //InitialPCState=0 machineState[InitialArgState] = arg; //InitialArgState=1 machineState[WhenDonePCState] = (int) ThreadFinish; //WhenDonePCState=2 11/17/2018

Does ThreadRoot() use s0, s1,s2,s3 to execute targeted thread func()? jal StartupPC # call InterruptEnable() stored in register s3 move a0, InitialArg #move argument in s1 to a0 jal InitialPC # call main thread procedure func() stored in s0 jal WhenDonePC # when done, call ThreadFinish() in s2 11/17/2018

QA When will thread:Finish() be called? At the end of the forked thread. Check ThreadRoot assembly code. At the end of the main() thread. Thread:Finish() calls scheduler->run() to run a new thread. Will the old thread (that calls Thread:Finish()) be returned and continue to be executed? No. Because the following is called first. threadToBeDestroyed = currentThread; Scheduler->Run() will delete such TCB 11/17/2018

QA In Sleep() or Yield(), the interrupt is turned off before calling scheduler->() to execute another thread. When will interrupt be turned on? In executing the switched thread, ThreadRoot() assembly code first executes StartupPC function which is machineState[StartupPCState] = (int) InterruptEnable; 11/17/2018

Semaphore object for thread synchronization Disable and re-enable interrupts to achieve mutual exclusion (e.g., by calling Interrupt::SetLevel()). Operations for a Semaphore object: Semaphore(char* debugName, int initialValue) P(): Decrement the semaphore's count, blocking the caller if the count is zero. V() :Increment the semaphore's count, releasing one thread if any are blocked waiting on the count. 11/17/2018

HW 1: threads & synchronization Work under threads subdirectory. Modify ThreadTest() to do simple threads programming (spawning multiple threads). Implement locks and condition variables (missing from the file synch.cc). Workload: Read Nachos code and add few hundred lines of code. Undocumented sample solution is provided. 11/17/2018

HW 1: Files involved Key files Other related files. main.cc, threadtest.cc -- a simple test of our thread routines. thread.h thread.cc -- Nachos thread data structure and operations. scheduler.h scheduler.cc -- The thread ready list. synch.h synch.cc -- synchronization routines. Other related files. synchlist.h, synchlist.cc -- synchronized access to lists using locks/conditions (useful examples for your programming). list.h list.cc -- generic list management. system.h, system.cc -- Nachos startup/shutdown routines. utility.h utility.cc -- some useful definitions and debugging routines. interrupt.h interrupt.cc -- manage interrupts. time.h timer.cc -- clock emulation. switch.h, switch.s -- assembly code for thread switching. stats.h stats.cc -- collect interesting statistics. 11/17/2018

HW Sample Code Caveat: ~cs170/nachos-projtest/Proj2Sample.tar.gz Has an old solution for HW1, HW2, HW3 HW1  threads subdirectory. ~400 lines of new code. HW2 -> userprog subdirectory. ~1200 lines of new code. Code for Exec() and Read() is not included. HW3 -> Not included. Caveat: Mixed benefits/problems in using other students’ code. e.g. Not well documented, not fully tested. Possibly awkward style/design Still your responsibility to produce good solutions (correctness, performance, style) with at least 50% difference 11/17/2018