discussion week 2 ta: kyle dewey. overview concurrency process level thread level mips - switch.s...

44
Discussion Week 2 TA: Kyle Dewey

Upload: kelly-morris

Post on 17-Jan-2016

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Discussion Week 2TA: Kyle Dewey

Page 2: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Overview

•Concurrency

•Process level

•Thread level

•MIPS - switch.s

•Project #1

Page 3: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Process Level

•UNIX/Linux: fork()

•Windows: CreateProcess()

Page 4: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

fork()/waitpid() Example

Page 5: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

while( true ) { fork(); }

Page 6: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Threading Overview

Page 7: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

User-space Threads

•OS does not know about them

•Handle their own scheduling

•If one blocks, all block

•Cannot exploit SMP

Page 8: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Blocking Example

User Thread 1User Thread 1 User Thread 2User Thread 2 User Thread 3User Thread 3

Process 1Process 1

OSOS

Process 2Process 2

Page 9: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Thread Standpoint

User Thread 1User Thread 1 User Thread 2User Thread 2 User Thread 3User Thread 3

Process 1Process 1

OSOS

Process 2Process 2

Page 10: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

OS Standpoint

Process 1Process 1

OSOS

Process 2Process 2

Page 11: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Blocking

•OS only sees a process

•OS blocks the process, in turn blocking all user-space threads

Page 12: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

SMP

•Processes have only a single thread

•Without kernel assistance, this cannot be changed

•Only one thread means only one CPU

Process 1Process 1

OSOS

Process 2Process 2

Page 13: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Kernel-Assisted

•OS has knowledge of threads

•OS schedules them

•Act like individual processes sharing an address space

Page 14: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

General Pros/Cons

•Kernel threads can exploit SMP

•Kernel threads will not cause all threads to block

•User-space threads are lightweight

•Context switch is cheap

•Likely far less code

Page 15: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

These are the concepts!

Page 16: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Then implementation

happened...

Page 17: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Question: Do Pthreads threads run in user-space or are they kernel-

assisted?

Page 18: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Answer: Yes.

Page 19: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Pthreads

•Really just a standard with a number of possible implementations

•Implementation can be kernel-assisted or in user-space

•Most OSes are kernel-assisted

Page 20: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Pthreads Example

Page 21: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Java Threads

•Again, merely a standard

•Most implement as kernel-assisted threads

Page 22: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Java Example

Page 23: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Kernel Thread Implementation

•OS can implement threads however it likes

•Pthreads and Java are libraries built on top of the threading primitives provided by the OS

Page 24: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Linux vs. Windows

•Linux provides the clone() system call

•Threads are actually processes

•Windows provides CreateThread()

•Referred to as “lightweight processes”

Page 25: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

NACHOS Threads

•Kernel-assisted

•Cannot currently handle interrupts or preemption correctly

•Similar to MS-DOS...until project 2

Page 26: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

MS-DOS/NACHOS

•One thread of execution

•One process can run

•OS is more like a large, complex software library

Page 27: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Thread Primitives

•Fork() - acts much like pthread_create

•Yield() - gives up the CPU for any other available threads

•Sleep() - like yield, but calling thread is blocked

•Finish() - terminates calling thread

Page 28: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

For Project 1

•Fork() creates, but does not immediately start running, a new thread

•Though there is no I/O, Sleep() can still be called to block on waiting for a critical region to clear

Page 29: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

NACHOS Threads

Page 30: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Concurrency

•Looks easy

•Really hard to get right

•Really hard

•No seriously, borderline impossible

Page 31: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Race Condition

•Different results are possible based on different process/thread orderings

•Ordering may be correct 99.999% of the time

Page 32: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Deadlock

•Two processes/threads wait for each other to do something

•While they wait, they do not do whatever it is they are waiting for

•Potential outcome of a race condition

Page 33: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Critical Region

•A point in code where the ordering matters

•Almost always this is some state that is shared between processes/threads

Clientconnect to server:port1connect to server:port2do something with both

Serveraccept from port1accept from port2

do something with both

Page 34: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Fixing the Problem

•Do not share state

•Only share read-only state

•Carefully regulate write access to shared state

Page 35: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Regulation

•A critical region can be manipulated by only one thread at a time

•Need a way to enforce that at most one thread at any time point is in such a region

Page 36: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Solving in Java

•Java provides the synchronized keyword for blocks

•Only one thread at a time may access a block marked with the synchronized keyword

int x = 0;public synchronized void set( int y ) {x = y;}public int get() {return x;}

Page 37: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Who cares about Java?

•Many concurrency primitives work exactly like this, just with a little more work

•One call upon entrance to critical region, another upon exit

•The entrance and exit are implicit through blocks with Java

Page 38: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Semaphores

•Simply a shared integer

•One call decrements, another increments

•By convention, 0 is locked, and values > 0 are unlocked

•Values < 0 mean the semaphore is not working!

Page 39: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Semaphores

•Increment/decrement are atomic - they are uninterruptible

•The highest possible number it can hold is equal to the max number of callers to the region it protects

Page 40: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Exampleint x = 0;Semaphore s;public void set( int y ) {s.decrement(); // wait/P/down x = y; s.increment(); } // signal/V/uppublic int get() {return x;}

Page 41: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Project 1 Task 1

•Experiment according to instructions

•Explain the execution of multithreaded code

•Add semaphores and contrast the difference

Page 42: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Project 1 Task 2

•Implement locks - essentially semaphores with a maximum of one caller at a time

•Given all the semaphore code to look at

•Hint hint it is a special case of a semaphore

Page 43: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Project 1 Task 3

•Implement conditions

•Require a correct Lock implementation

•Allows a group of threads to synchronize on a given section of code

•Can enforce that all must be at the same point of execution

•Block until this is true

Page 44: Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1

Project 1 Task 4

•Identify and describe a race condition in a given section of code

•Fix the race condition using semaphores

•Fix it another way using locks and/or conditions