CS140 Project 1: Threads Slides by Kiyoshi Shikuma Overview Quick Review – – – Threads Scheduling Synchronization Project – – – – Pintos Alarm Clock Priority Scheduling Advanced Scheduler Quick Review: Threads Threads are an execution stream Execution Context: – – Current location in program Values of Variables All maintained on thread’s stack Pintos: Struct thread { name ID stack pointer page directory etc. } This struct is defined in thread.h (a very useful file for this project) Quick Review: Scheduling New terminated admitted interrupt Ready Exit Running scheduled I/O or Event completion I/O Event or wait Waiting Quick Review: Scheduling cont’d Scheduling Policy – – – FIFO Round Robin Priority Preemption (Remove the “running” thread and schedule a new one) – – Timer Interrupt Device Interrupt (i.e. network, disk, etc) Quick Review: Thread Scheduling Wait on I/O Thread A ___________ ________ Thread B Kernel Timer Interrupt ______ __ _____ __ __ _____ __ Quick Background: Synchronization Will be covered in lecture next week Synchronization used to prevent race conditions, avoid concurrency problems One way to avoid synchronization issues is turn off interrupts (only one thing running so no issues), but don’t want to do this Better solution: Use synchronization primitives – – – Locks Semaphores Condition Variables You will probably be modifying synch.h/c Project: Pintos Simple OS created by Ben Pfaff Could run on x86 hardware but we are using emulators (makes debugging and running easier) – Bochs – Qemu For Threads project you will be (surprise!) working in the threads directory. – Alarm Clock will require modification of timer.c in the devices folder but you shouldn’t need to modify any other files outside threads Project: Getting Started Follow website instructions to install and set the path variable (suggest adding to .cshrc so you don’t have to set every time) To build run “make” in pintos/src/threads To run tests run “make check” in pintos/src/threads/build – 7/27 tests should pass when you first download and run “pintos” script simplifies usage of simulators: – i.e. “pintos –v -- -q run alarm-single” – Follow instructions to use two terminals to use GDB on pintos – Printf debugging is good too Project: Alarm Clock Function: void timer_sleep (int64_t ticks) – – – – You will be modifying this function in timer.c Currently busy waits Need to instead put thread to sleep and wake it up when enough time has elapsed Notice that testing suite already passes (most) of the alarm clock tests, so make sure your implementation is correct. Project: Priority Scheduling Each thread is assigned one of 64 priorities. Current implementation uses a round robin scheduler Change this to instead schedule the highest priority thread that can run (Useful file is thread.h/c) Project Priority Scheduling Need to consider all cases: – – – – When scheduling the next thread, choose one with highest priority A thread lowers its priority (should yield if there is a higher-priority thread in the ready list) A higher priority thread wakes up (such as alarm clock or after waiting on a lock/semaphore) it should preempt the lower priority thread. Etc. Project: Priority Donation Problem: Priority Inversion – L acquires a lock – H becomes ready, preempts L and starts running – M becomes ready – H waits for the lock held by L – M starts running M runs, then L, then H After H begins waiting on the lock, L should run (until it releases lock), then H, then M Project: Priority Donation cont’d Solution: Priority Donation – – When H starts waiting on the lock L is holding, it “donates” its priority to L such that L now runs with H’s priority When L releases the lock, it reverts back to its original priority Nested Donations: H->M->L Multiple Donations: H->L <-M Project: Advanced Scheduler BSD Scheduler (Read doc linked on site) – – – Attempts to reduce the average response time of jobs Calculates statistics during thread execution and sets priorities based on these Does NOT do priority donation Design Document Template is on website Start early (don’t wait until you’re done coding to write this) – Thinking through the questions will help you guide your coding Do NOT skimp on this, it is worth just as much as the code Project: Submission Save design doc as DESIGNDOC in threads directory (no extension) Run “make grade” Copy build/grade to GRADE in threads directory Run “make clean” Run “/usr/class/cs140/bin/submit 1” Submission instructions here Some Advice: Start early Read through the (relevant) docs and code first (it’s very well documented) Think about design before you start coding Meet often as a group to integrate (don’t wait until the last second) Maintain good consistent coding style