CS140 Project 1: Threads Slides by Kiyoshi Shikuma.

18
CS140 Project 1: Threads Slides by Kiyoshi Shikuma

Transcript of CS140 Project 1: Threads Slides by Kiyoshi Shikuma.

Page 1: CS140 Project 1: Threads Slides by Kiyoshi Shikuma.

CS140 Project 1: Threads

Slides by Kiyoshi Shikuma

Page 2: CS140 Project 1: Threads Slides by Kiyoshi Shikuma.

Overview

Quick Review– Threads– Scheduling– Synchronization

Project– Pintos– Alarm Clock– Priority Scheduling– Advanced Scheduler

Page 3: CS140 Project 1: Threads Slides by Kiyoshi Shikuma.

Quick Review: Threads

Threads are an execution stream Execution Context:

– Current location in program– Values of Variables

All maintained on thread’s stackPintos:Struct thread {

nameIDstack pointerpage directoryetc.

}

This struct is defined in thread.h (a very useful file for this project)

Page 4: CS140 Project 1: Threads Slides by Kiyoshi Shikuma.

Quick Review: Scheduling

Ready Running

terminatedNew

Waiting

admitted

scheduled

interrupt

I/O Event or waitI/O or Event completion

Exit

Page 5: CS140 Project 1: Threads Slides by Kiyoshi Shikuma.

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)

Page 6: CS140 Project 1: Threads Slides by Kiyoshi Shikuma.

Quick Review: Thread Scheduling

Thread A ___________ ________

Thread B ______ _____ _____

Kernel __ __ __ __

Timer Interrupt

Wait on I/O

Page 7: CS140 Project 1: Threads Slides by Kiyoshi Shikuma.

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

Page 8: CS140 Project 1: Threads Slides by Kiyoshi Shikuma.

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

Page 9: CS140 Project 1: Threads Slides by Kiyoshi Shikuma.

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

Page 10: CS140 Project 1: Threads Slides by Kiyoshi Shikuma.

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.

Page 11: CS140 Project 1: Threads Slides by Kiyoshi Shikuma.

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)

Page 12: CS140 Project 1: Threads Slides by Kiyoshi Shikuma.

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.

Page 13: CS140 Project 1: Threads Slides by Kiyoshi Shikuma.

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

Page 14: CS140 Project 1: Threads Slides by Kiyoshi Shikuma.

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

Page 15: CS140 Project 1: Threads Slides by Kiyoshi Shikuma.

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

Page 16: CS140 Project 1: Threads Slides by Kiyoshi Shikuma.

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

Page 17: CS140 Project 1: Threads Slides by Kiyoshi Shikuma.

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

Page 18: CS140 Project 1: Threads Slides by Kiyoshi Shikuma.

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