computer science 162 discussion section week 2

26
Computer Science 162 Discussion Section Week 2

Upload: hua

Post on 23-Feb-2016

22 views

Category:

Documents


0 download

DESCRIPTION

Computer Science 162 Discussion Section Week 2. Agenda. Recap “What is an OS?” and Why? Process vs. Thread “THE” System. Note: Many slides are modifications of slides from Matei Zaharia Who referenced slides from Steve Gribble, Ed Lazowska , Hank Levy, and John Zahorian. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Computer Science 162 Discussion Section Week 2

Computer Science 162Discussion Section

Week 2

Page 2: Computer Science 162 Discussion Section Week 2

Agenda

• Recap “What is an OS?” and Why?• Process vs. Thread• “THE” System

Page 3: Computer Science 162 Discussion Section Week 2

Note: Many slides are modifications of slides from Matei Zaharia

Who referenced slides from Steve Gribble, Ed Lazowska, Hank Levy, and John Zahorian

Page 4: Computer Science 162 Discussion Section Week 2

Why do we want an OS?• Isolation

– Fault: “if my program crashes yours shouldn’t”– Performance: “if my program starts to do some massive computation, it

shouldn’t starve yours from running”

• Mediation (multiplexing/sharing + protection)– Manage the sharing of hardware resources (CPU, NIC, RAM, disk, keyboard,

sound card, etc),

• Abstractions and Primitives– Set of constructs and well-defined interfaces to simplify application

development: “all the code you didn’t write” in order to implement your application

• Because hardware changes faster than applications!• Because some concepts are useful across applications

Page 5: Computer Science 162 Discussion Section Week 2

Why bother with an OS?• User benefits

– Efficiency (cost and performance)• share one computer across many users• concurrent execution of multiple programs

– Safety• OS protects programs from each other• OS fairly multiplexes resources across programs

• Application benefits– Simplicity

• sockets instead of ethernet cards– Portability

• device independence: 3COM card or Intel card?

Page 6: Computer Science 162 Discussion Section Week 2

Concurrency and Parallelism

• Concurrency means multiple threads of computation can make progress, but possibly by sharing the same processor– Like doing homework while chatting on IM

• Parallelism means leveraging multiple processors to compute a result faster– Like dividing a pile of work among people

Page 7: Computer Science 162 Discussion Section Week 2

Why Concurrency?

• Consider a web server: while it’s waiting for a response from one client, it could read a request for another client

• Consider a browser: while it’s waiting for a response from a web server, it wants to react to mouse or keyboard input

Concurrency increases/enables responsiveness

Page 8: Computer Science 162 Discussion Section Week 2

Why Parallelism?

• Because we actually have multiple CPUs!• Because matrix multiply goes so much faster!

NOTE: Parallelism requires multiple processors, while concurrency also helps on a uniprocessor

Page 9: Computer Science 162 Discussion Section Week 2

Lifecycle of a Thread (or Process)

• As a thread executes, it changes state:– new: The thread is being created– ready: The thread is waiting to run– running: Instructions are being executed– waiting: Thread waiting for some event to occur– terminated: The thread has finished execution

• “Active” threads are represented by their TCBs– TCBs organized into queues based on their state

Page 10: Computer Science 162 Discussion Section Week 2

How does OS do it?• Kernel: The highly privileged code that carries out lowest level OS

functions

• Use multiple processes, OS schedules them (i.e. multiplexes resources between them)– Each process has its own address space– Each process maintains a list of open files, open network connections …

• Use multiple threads within a process, either OS or user schedules them– Threads share the process’s address space

Threads are cheaper than processes and can more easily share state! But have no isolation.

Page 11: Computer Science 162 Discussion Section Week 2

• Strategy 1: force everyone to cooperate– a thread willingly gives up the CPU by calling yield() which calls into the scheduler, which context-switches to another thread

– what if a thread never calls yield()?

• Strategy 2: use preemption– at timer interrupt, scheduler gains control and

context switches as appropriate

Recall, an OS needs to mediate access to resources: how do we share the CPU?

Page 12: Computer Science 162 Discussion Section Week 2

Review: Two Thread Yield Example

• Consider the following code blocks: proc A() {

B();}proc B() { while(TRUE) { yield(); }}

• Suppose we have 2 threads:– Threads S and T

Thread S

Stac

k gr

owth

A

B(while)

yield

run_new_thread

switch

Thread T

A

B(while)

yield

run_new_thread

switch

Page 13: Computer Science 162 Discussion Section Week 2

“THE” System

• Dijkstra– Algorithm (shortest path)– OS (“THE”)– Software Engineering (“GOTO Considered

Harmful”)– Programming Language and Formal Verification

Page 14: Computer Science 162 Discussion Section Week 2

“THE” Multiprogramming System

• Why Multiprogramming?– A Reduction in turnaround time for short

programs– Economic use of peripheral devices– Automatic control of backing store and efficient

use of CPU– Need general processor but not all the power

Page 15: Computer Science 162 Discussion Section Week 2

Storage

• Core ->RAM, Drum ->Disk• Separation of Virtual and Physical location• Page Swap, the content of the page can be

written to a different location on the Drum.• No need for consecutive physical locations

Page 16: Computer Science 162 Discussion Section Week 2

Processor

• A Collection of Sequential Processes Working Together

• Process State• Mutual Synchronization

Page 17: Computer Science 162 Discussion Section Week 2

Hierarchy

• Level 0 – Present a virtual processor• Level 1 – Present virtual segments• Level 2 – Present a virtual console• Level 3 – Present a buffered IO interface to

devices• Level 4 – User Programs

Page 18: Computer Science 162 Discussion Section Week 2

Benefit of Layering

• Limited Interface• Fewer Bugs• Easier to Test• Easier to Communicate

Page 19: Computer Science 162 Discussion Section Week 2

Semaphore

• Found in Appendix, but so Important!• Shared between sequential processes for

synchronization• P -> decrease ->Possibly lock (if value <0)• V -> increase ->unlock• P, V are indivisible (atomic)

Page 20: Computer Science 162 Discussion Section Week 2

Backup Slides

Page 21: Computer Science 162 Discussion Section Week 2

os kernel

(thread create, destroy, signal, wait, etc.)

CPU

Kernel-level threads

address space

thread

Page 22: Computer Science 162 Discussion Section Week 2

Are kernel threads too expensive?

• Historically yes (thread operations require system calls), but aren’t too bad in practice today, if you use them correctly.

• Alternatives?

Page 23: Computer Science 162 Discussion Section Week 2

os kernel

CPU

User-level threadsuser-levelthread library

(thread create, destroy, signal, wait, etc.)

address space

thread

Page 24: Computer Science 162 Discussion Section Week 2

address space

thread

os kernel

CPU

User-level threads: what the kernel sees

Page 25: Computer Science 162 Discussion Section Week 2

os kernel

(kernel thread create, destroy, signal, wait, etc.)

CPU

User-level threads: the full storyuser-levelthread library

(thread create, destroy, signal, wait, etc.)

kernel threads

address space

thread

Page 26: Computer Science 162 Discussion Section Week 2

• No, Google “scheduler activations” for a great discussion of why user-level threads aren’t enough!

Are user-level threads the answer?