Christo Wilson Project 1: Threads in Pintos

Slides:



Advertisements
Similar presentations
Christo Wilson Project 2: User Programs in Pintos
Advertisements

The Kernel Abstraction. Challenge: Protection How do we execute code with restricted privileges? – Either because the code is buggy or if it might be.
Pintos: Threads Project Slides by: Vijay Kumar Updated by Godmar Back.
CMPT 300: Operating Systems I Dr. Mohamed Hefeeda
11/13/01CS-550 Presentation - Overview of Microsoft disk operating system. 1 An Overview of Microsoft Disk Operating System.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Dr. Mohamed Hefeeda.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
Christo Wilson Project 3: Virtual Memory in Pintos
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.
Build an Operating System
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
Christo Wilson Project 4: File System in Pintos
CS 0008 Day 2 1. Today Hardware and Software How computers store data How a program works Operators, types, input Print function Running the debugger.
I/O Tanenbaum, ch. 5 p. 329 – 427 Silberschatz, ch. 13 p
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
Host and Application Security Lesson 4: The Win32 Boot Process.
System Calls 1.
Topics Introduction Hardware and Software How Computers Store Data
Welcome to the World of Nachos CPS 110 Spring 2004 Discussion Session 1.
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto OS-Related Hardware.
Operating Systems Lecture 7 OS Potpourri Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software.
Race Conditions Defined 1. Every data structure defines invariant conditions. defines the space of possible legal states of the structure defines what.
1 CSE 451 Section 2: Interrupts, Syscalls, Virtual Machines, and Project 1.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
Operating System Structure A key concept of operating systems is multiprogramming. –Goal of multiprogramming is to efficiently utilize all of the computing.
Pintos: Threads Project
CS140 Project 1: Threads Slides by Kiyoshi Shikuma.
We will focus on operating system concepts What does it do? How is it implemented? Apply to Windows, Linux, Unix, Solaris, Mac OS X. Will discuss differences.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 2 Computer-System Structures Slide 1 Chapter 2 Computer-System Structures.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Chapter 1 Computers, Compilers, & Unix. Overview u Computer hardware u Unix u Computer Languages u Compilers.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Genesis: From Raw Hardware to Processes Andy Wang Operating Systems COP 4610 / CGS 5765.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
Unit 1: Computing Fundamentals. Computer Tour-There are 7 major components inside a computer  Write down each major component as it is discussed.  Watch.
1 Lecture 6 “Nachos” n nachos overview n directory structure n nachos emulated machine n nachos OS n nachos scheduler n nachos threads.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
Chapter 6 Limited Direct Execution Chien-Chung Shen CIS/UD
Transmitter Interrupts Review of Receiver Interrupts How to Handle Transmitter Interrupts? Critical Regions Text: Tanenbaum
CS140 Project Session Notes #1: Threads Haobo Xu, TA Fall Based on Ben Sapp’s slides Download these slides from
Tutorial 2: Homework 1 and Project 1
Introduction to Operating Systems Concepts
Bare metal OS project CSSE 332 Operating Systems
Introduction to Operating Systems
Chapter Objectives In this chapter, you will learn:
Processes and threads.
Topics Introduction Hardware and Software How Computers Store Data
CS 6560: Operating Systems Design
Christo Wilson Project 1: Threads in Pintos
CONFIGURING HARDWARE DEVICE & START UP PROCESS
Computer Science I CSC 135.
Introduction to Operating Systems
Genesis: From Raw Hardware to Processes
Module 2: Computer-System Structures
Operating Systems Chapter 5: Input/Output Management
Topics Introduction Hardware and Software How Computers Store Data
Lecture Topics: 11/1 General Operating System Concepts Processes
Transmitter Interrupts
Module 2: Computer-System Structures
CSE 153 Design of Operating Systems Winter 19
Module 2: Computer-System Structures
Contact Information Office: 225 Neville Hall Office Hours: Monday and Wednesday 12:00-1:00 and by appointment. Phone:
Module 2: Computer-System Structures
In Today’s Class.. General Kernel Responsibilities Kernel Organization
Chapter 13: I/O Systems “The two main jobs of a computer are I/O and [CPU] processing. In many cases, the main job is I/O, and the [CPU] processing is.
Presentation transcript:

Christo Wilson Project 1: Threads in Pintos 8/22/2012 CS 5600 Computer Systems Project 1: Threads in Pintos Defense

Getting Started With Pintos What does Pintos Include? Threads in Pintos Project 1

What is Pintos? Pintos is a teaching operating system from Stanford Written in C Implements enough functionality to boot… … perform basic device I/O… … and has a small standard library Your goal will be to expand it’s functionality

$ cp /course/cs5600f14/install/pintos.tar.gz ~ Pintos Documentation All of the Pintos docs are available on the course webpage http://www.ccs.neu.edu/home/cbw/5600/pintos/pintos.html You will need to copy the Pintos source to your home directory $ cp /course/cs5600f14/install/pintos.tar.gz ~ Source can also be downloaded from the course webpage

Setting Up Your Environment We have installed all necessary code and scripts on the CCIS Linux machines You will need to modify your PATH to access them Add the following line to ~/.bash_profile export PATH=${PATH}:/course/cs5600f14/bin:/course/cs5600f14/pintos/bin If ~/.bash_profile does not exist, create it Restart your shell

Pintos Projects Each project in this class corresponds to a particular directory Project 1: pintos/src/threads/ Project 2: pintos/src/userprog/ Project 3: pintos/src/vm/ Project 4: pintos/src/filesys/ Each directory includes a Makefile, and all necessary files to build Pintos

Building and Running Pintos $ cd ~/pintos/src/threads $ make $ cd build/ $ pintos -v -- -q run alarm-single Script to run Pintos in the QEMU simulator Parameters for the simulator Parameters for the Pintos kernel

Making Pintos When you run make, you compile two things build/loader.bin The Pintos bootloader (512 byte MBR image) Locates the kernel in the filesystem, loads it into memory, and executes it build/kernel.bin The Pintos kernel The pintos script automatically creates a file system image that includes the MBR and kernel

QEMU Pintos could be run on an actual machine But that would require installing it, dual booting with another OS Debugging would be hard Instead, we will run Pintos inside QEMU QEMU is a machine emulator In our case, a 32-bit x86 CPU with basic devices Executes a BIOS, just like a real machine Loads bootloader from MBR of emulated disk drive

Getting Started With Pintos What does Pintos Include? Threads in Pintos Project 1

Pintos Features Pintos is already a basic, bootable OS Switches from real to protected mode Handles interrupts Has a timer-interrupt for process preemption Does basic memory management Supports a trivial file system

Devices pintos/src/devices/ includes drivers and APIs for basic hardware devices System timer: timer.h Video: vga.h (use lib/kernel/stdio.h to print text) Serial port: serial.h File storage: ide.h, partition.h, block.h Keyboard input: kbd.h, input.h Interrupt controller: intq.h, pit.h

Standard Library The typical C standard library is not available to you (C lib doesn’t exist in Pintos) Pintos reimplements a subset of C lib in pintos/src/lib/ Variable types: ctypes.h, stdbool.h, stdint.h Variable argument functions: stdarg.h String functions: string.h Utility functions: stdlib.h Random number generation: random.h Asserts and macros for debugging: debug.h

Data Structures pintos/src/lib/kernel/ includes kernel data structures that you may use Bitmap: kernel/bitmap.h Doubly linked list: kernel/list.h Hash table: kernel/hash.h Console printf(): kernel/stdio.h Include using #include <kernel/whatever.h>

$ pintos -v -- run alarm-single Christo Wilson 8/22/2012 Tests Each Pintos project comes with a set of tests Useful for debugging Also what we will use to grade your code Out-of-the-box, Pintos cannot run user programs Thus, tests are compiled into the kernel You tell the kernel which test to execute on the command line $ pintos -v -- run alarm-single Use $ make check to run the tests Defense

[cbw@finalfight build] pintos -v -- -q run alarm-single qemu -hda /tmp/8HDMnPzQrE.dsk -m 4 -net none -nographic -monitor null PiLo hda1 Loading......... Kernel command line: run alarm-single Pintos booting with 4,088 kB RAM... 382 pages available in kernel pool. 382 pages available in user pool. Calibrating timer... 523,468,800 loops/s. Boot complete. Executing 'alarm-single': (alarm-single) begin (alarm-single) Creating 5 threads to sleep 1 times each. (alarm-single) Thread 0 sleeps 10 ticks each time, … (alarm-single) end Execution of 'alarm-single' complete. Execution of 'alarm-single' complete. Timer: 276 ticks Thread: 0 idle ticks, 276 kernel ticks, 0 user ticks Console: 986 characters output Keyboard: 0 keys pressed Powering off...

Pintos Bootup Sequence pintos/src/threads/init.c  main() bss_init (); /* Clear the BSS */ argv = read_command_line (); argv = parse_options (argv); thread_init (); console_init (); printf ("Pintos booting with…”); /* Initialize memory system. */ palloc_init (user_page_limit); malloc_init (); paging_init (); /* Segmentation. */ tss_init (); gdt_init (); /* Enable Interrupts */ intr_init (); /* Timer Interrupt */ timer_init (); /* Keyboard */ kbd_init (); input_init (); exception_init (); /* Enable syscalls */ syscall_init (); /* Initialize threading */ thread_start (); serial_init_queue (); timer_calibrate (); /* Initialize the hard drive and fs */ ide_init (); locate_block_devices (); filesys_init (format_filesys); printf ("Boot complete.\n"); /* Run actions specified on kernel command line. */ run_actions (argv); shutdown (); thread_exit ();

Getting Started With Pintos What does Pintos Include? Threads in Pintos Project 1

Threads in Pintos Pintos already implements a simple threading system Thread creation and completion Simple scheduler based on timer preemption Synchronization primitives (semaphore, lock, condition variable) But this system has problems: Wait is based on a spinlock (i.e. it just wastes CPU) The thread priority system is not implemented

Threading System thread_create() starts new threads Added to all_list and ready_list Periodically, the timer interrupt fires Current thread stops running Timer interrupt calls schedule() static void schedule (void) { struct thread *cur = running_thread (); struct thread *next = next_thread_to_run (); struct thread *prev = NULL; if (cur != next) prev = switch_threads (cur, next); thread_schedule_tail (prev); }

Switching Threads Remember the switch() function we talked about earlier? Pintos has one in threads/switch.S Saves the state of the CUR thread Saves ESP of the CUR thread Loads the state of the NEXT thread Loads ESP of the NEXT thread Returns to NEXT thread

Idle Thread There is always one thread in the system Known as the idle thread Executes when there are no other threads to run for (;;) { intr_disable (); /* Disable interrupts */ thread_block (); /* Let another thread run */ /* Re-enable interrupts and wait for the next one. The `sti' instruction disables interrupts until the completion of the next instruction, so these two instructions are executed atomically. */ asm volatile ("sti; hlt" : : : "memory"); }

Getting Started With Pintos What does Pintos Include? Threads in Pintos Project 1

Pintos Projects All four Pintos projects will involve two things Modifying the Pintos OS Producing a DESIGNDOC that explains your modifications We will use automated tests to gauge the correctness of your modified code The TA/graders will evaluate the quality of your DESIGNDOC Templates for DESIGNDOCs are provided by us

Project 1 Goals Fix the timer_sleep() function to use proper synchronization No busy waiting Implement the thread priority system High priority threads execute before low priority Watch out for priority inversion!

Goal 1: Fixing timer_sleep() Sometimes, a thread may want to wait for some time to pass, a.k.a. sleep Problem: Pintos’ implementation of sleep is very wasteful devices/timer.c void timer_sleep (int64_t ticks) { int64_t start = timer_ticks (); while (timer_elapsed (start) < ticks) thread_yield (); }

Modifying timer_sleep() void timer_sleep (int64_t ticks) { //int64_t start = timer_ticks (); // while (timer_elapsed (start) < ticks) // thread_yield (); thread_sleep(ticks); // New function! }

Modifying struct thread threads/thread.h enum thread_status { THREAD_RUNNING, /* Running thread. */ THREAD_READY, /* Not running but ready to run. */ THREAD_SLEEPING, /* New state for sleeping threads */ THREAD_BLOCKED, /* Waiting for an event to trigger. */ THREAD_DYING /* About to be destroyed. */ }; struct thread { … int64_t wake_time; }

thread_sleep() threads/thread.c static struct list sleeping_list; void thread_sleep (int64_t ticks) { struct thread *cur = thread_current(); enum intr_level old_level; old_level = intr_disable (); if (cur != idle_thread) { list_push_back (&sleeping_list, &cur->elem); cur->status = THREAD_SLEEPING; cur->wake_time = timer_ticks() + ticks; schedule (); } intr_set_level (old_level);

Modifying schedule () threads/thread.c struct list_elem *temp, *e = list_begin (&sleeping_list); int64_t cur_ticks = timer_ticks(); while (e != list_end (&sleeping_list)) { struct thread *t = list_entry (e, struct thread, allelem); if (cur_ticks >= t->wake_time) { list_push_back (&ready_list, &t->elem); /* Wake this thread up! */ t->status = THREAD_READY; temp = e; e = list_next (e); list_remove(temp); /* Remove this thread from sleeping_list */ } else e = list_next (e);

Better Implementation? I just (partially) solved part of Project 1 for you You’re welcome :) But, my implementation still isn’t efficient enough How could you improve it? Build your own improved timer_sleep() implementation and answer 6 questions about it in your DESIGNDOC

Goal 2: Thread Priority Modify the Pintos thread scheduler to support priorities Each thread has a priority High priority threads execute before low priority threads Why is this challenging? Priority inversion Implement priority scheduling and answer 7 questions about it in your DESIGNDOC

Priority Scheduling Examples Working Example Problematic Example Thread 1 Priority 0 Thread 1 Priority 0 Priority Inversion Thread 2 Priority 63 LOCK Read Read Add Add Store Read UNLOCK Add Thread 2 Priority 63 Store LOCK Read Store LOCK

Return to original priority Priority Donation Return to original priority Donate priority Thread 1 Priority 0 63 Thread 2 Priority 63 Challenges: What if a thread holds multiple locks? What if thread A depends on B, and B depends on C? LOCK Read Add LOCK Store UNLOCK Read Add Store UNLOCK

Overall File Modifications What files will you be modifying in project 1? devices/timer.c threads/synch.c  Most edits will be here… threads/thread.c  … and here threads/thread.h threads/DESIGNDOC  Text file that you will write

Advanced Scheduler? MLFQ? Project 1 originally included more work Asked student to build an advanced scheduler that implements MLFQ We have removed this from the assignment You’re welcome :) If you see references in the docs to “advanced scheduler” or references in the code to “mlfq” ignore them

Grading 15 points total To receive full credit: Turn in working, well documented code that compiles successfully and completes all tests (50%) You do not need to pass the MLFQ tests Turn in a complete, well thought our design document (50%) If your code doesn’t compile or doesn’t run, you get zero credit Must run on the CCIS Linux machines! All code will be scanned by plagiarism detection software

Turning In Your Project Register yourself for the grading system $ /course/cs5600f14/bin/register-student [NUID] Register your group All group members must run the script! $ /course/cs5600f14/bin/register project1 [team name] Run the turn-in script Two parameters: project name and code directory $ /course/cs5600f14/bin/turnin project1 ~/pintos/src/

DUE: September 24 11:59:59PM EST Questions?