CS 162 Discussion Section Week 2
description
Transcript of CS 162 Discussion Section Week 2
![Page 1: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/1.jpg)
CS 162Discussion Section
Week 2
![Page 2: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/2.jpg)
Who am I?
Haoyuan (HY) Lihttp://www.cs.berkeley.edu/[email protected]
Office Hours: 1pm-3pm Monday @ 465E SODA
Research: Data center systems
![Page 3: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/3.jpg)
Administrivia
• Get to know Nachos• Start project 1
![Page 4: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/4.jpg)
Project 1
• Can be found in the course website– Under the heading “Projects and Nachos”
• Stock Nachos has an incomplete thread system. Your job is to– complete it, and – use it to solve several synchronization problems
![Page 5: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/5.jpg)
Project 1 Grading
• Design docs [40 points]– First draft [10 points]– Design review [10 points]– Final design doc [20 points]
• Code [60 points]
![Page 6: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/6.jpg)
Design Document
• Overview of the project as a whole along with its parts
• Header must contain the following info– Project Name and #– Group Members Name and ID– Section #– TA Name
![Page 7: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/7.jpg)
Design Document Structure
Each part of the project should be explained using the following structure
• Overview• Correctness Constraints• Declarations• Descriptions• Testing Plan
![Page 8: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/8.jpg)
True/False
1. Threads within the same process share the same heap and stack.
2. Preemptive multithreading requires threads to give up the CPU using the yield() system call.
3. Despite the overhead of context switching, multithreading can provide speed-up even on a single-core cpu.
![Page 9: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/9.jpg)
Short Answers
1. What is the OS data structure that represents a running process?
2. What are some of the similarities and differences between interrupts and system calls? What roles do they play in preemptive and non-preemptive multithreading?
![Page 10: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/10.jpg)
Why Processes & Threads?Goals:
![Page 11: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/11.jpg)
Why Processes & Threads?• Multiprogramming: Run multiple applications concurrently• Protection: Don’t want a bad application to crash system!
Goals:
Solution:
![Page 12: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/12.jpg)
Why Processes & Threads?• Multiprogramming: Run multiple applications concurrently• Protection: Don’t want a bad application to crash system!
Goals:
Process: unit of execution and allocation• Virtual Machine abstraction: give process illusion it owns
machine (i.e., CPU, Memory, and IO device multiplexing)
Solution:
Challenge:
![Page 13: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/13.jpg)
Why Processes & Threads?• Multiprogramming: Run multiple applications concurrently• Protection: Don’t want a bad application to crash system!
Goals:
Process: unit of execution and allocation• Virtual Machine abstraction: give process illusion it owns
machine (i.e., CPU, Memory, and IO device multiplexing)
Solution:
• Process creation & switching expensive• Need concurrency within same app (e.g., web server)
Challenge:
Solution:
![Page 14: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/14.jpg)
Why Processes & Threads?• Multiprogramming: Run multiple applications concurrently• Protection: Don’t want a bad application to crash system!
Goals:
Process: unit of execution and allocation• Virtual Machine abstraction: give process illusion it owns
machine (i.e., CPU, Memory, and IO device multiplexing)
Solution:
• Process creation & switching expensive• Need concurrency within same app (e.g., web server)
Challenge:
Thread: Decouple allocation and execution• Run multiple threads within same process
Solution:
![Page 15: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/15.jpg)
Putting it together: Process
Memory
I/O State(e.g., file, socket contexts)
CPU state (PC, registers..)
Sequential stream of instructions
A(int tmp) { if (tmp<2) B(); printf(tmp);}B() { C();}C() { A(2);}A(1);…
(Unix) Process
Resources
![Page 16: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/16.jpg)
Putting it together: Processes
…
Process 1 Process 2 Process N
CPU sched.
OS
CPU(1 core)
1 process at a time
CPUsate
IOsate
Mem.
CPUsate
IOsate
Mem.
CPUsate
IOsate
Mem.
• Switch overhead: high– CPU state: low– Memory/IO state: high
• Process creation: high• Protection
– CPU: yes– Memory/IO: yes
• Sharing overhead: high (involves at least a context switch)
![Page 17: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/17.jpg)
Putting it together: ThreadsProcess 1
CPU sched.
OS
CPU(1 core)
1 thread at a time
IOsate
Mem.
…
threads
Process N
IOsate
Mem.
…
threads
…
• Switch overhead: low (only CPU state)
• Thread creation: low• Protection
– CPU: yes– Memory/IO: No
• Sharing overhead: low (thread switch overhead low)
CPUsate
CPUsate
CPUsate
CPUsate
![Page 18: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/18.jpg)
Putting it together: Multi-CoresProcess 1
CPU sched.
OS
IOsate
Mem.
…
threads
Process N
IOsate
Mem.
…
threads
…
• Switch overhead: low (only CPU state)
• Thread creation: low• Protection
– CPU: yes– Memory/IO: No
• Sharing overhead: low (thread switch overhead low)
core 1 Core 2 Core 3 Core 4 CPU
4 threads at a time
CPUsate
CPUsate
CPUsate
CPUsate
![Page 19: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/19.jpg)
Putting it together: Hyper-ThreadingProcess 1
CPU sched.
OS
IOsate
Mem.
…
threads
Process N
IOsate
Mem.
…
threads
…
• Switch overhead between hardware-threads: very-low (done in hardware)
• Contention to cache may hurt performance
core 1
CPU
core 2 core 3 core 4
8 threads at a time
hardware-threads(hyperthreading)
CPUsate
CPUsate
CPUsate
CPUsate
![Page 20: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/20.jpg)
Thread State• State shared by all threads in process/addr space
– Content of memory (global variables, heap)– I/O state (file system, network connections, etc)
• State “private” to each thread – Kept in TCB Thread Control Block– CPU registers (including, program counter)– Execution stack – what is this?
• Execution Stack– Parameters, temporary variables– Return PCs are kept while called procedures are executing
![Page 21: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/21.jpg)
What is the difference between a
Thread and a Process?
![Page 22: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/22.jpg)
When and how do you switch between processes?
![Page 23: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/23.jpg)
Scheduling states
![Page 24: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/24.jpg)
Dispatch Loop• Conceptually, the dispatching loop of the operating system looks as
follows:
Loop { RunThread(); ChooseNextThread(); SaveStateOfCPU(curTCB); LoadStateOfCPU(newTCB);}
• This is an infinite loop– One could argue that this is all that the OS does
![Page 25: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/25.jpg)
Context Switching
![Page 26: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/26.jpg)
An OS needs to mediate access to resources: how do we share the CPU?
• 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 pre-emption
– 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 27: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/27.jpg)
From Lecture: Two Thread Yield• Consider the following
code blocks:“" proc A() {B();}proc B() {while(TRUE) {yield();}}
• Suppose we have 2threads:“Threads S and T
Thread S
Sta
ck g
row
th
A
B(while)
yield
run_new_thread
switch
Thread T
A
B(while)
yield
run_new_thread
switch
![Page 28: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/28.jpg)
Detour: Interrupt Controller
• Interrupts invoked with interrupt lines from devices• Interrupt controller chooses interrupt request to honor
– Mask enables/disables interrupts– Priority encoder picks highest enabled interrupt – Software Interrupt Set/Cleared by Software– Interrupt identity specified with ID line
• CPU can disable all interrupts with internal flag• Non-maskable interrupt line (NMI) can’t be disabled
Network
IntID
Interrupt
Interrupt Mask
ControlSoftwareInterrupt NMI
CPU
Priority E
ncoder
Timer
Int Disable
![Page 29: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/29.jpg)
Max Number of Processes, Threads?
• Windows?• Linux?• Mac OS?
• How Many Threads and Processes are Enough?
![Page 30: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/30.jpg)
Questions / Examples about Process and Thread?
![Page 31: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/31.jpg)
Review: Execution Stack Example
• Stack holds function arguments, return address
• Permits recursive execution• Crucial to modern languages
A(int tmp) { if (tmp<2) B(); printf(tmp);}B() { C();}C() { A(2);}A(1);exit;
addrX:
addrY:
addrU:
addrV:
addrZ:
.
.
.
.
.
.
.
.
.
.
.
.
![Page 32: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/32.jpg)
Review: Execution Stack Example
• Stack holds function arguments, return address
• Permits recursive execution• Crucial to modern languages
A(int tmp) { if (tmp<2) B(); printf(tmp);}B() { C();}C() { A(2);}A(1);exit;
StackPointer
Stack Growth
A: tmp=1 ret=addrZaddrX:
addrY:
addrU:
addrV:
addrZ:
.
.
.
.
.
.
.
.
.
.
.
.
![Page 33: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/33.jpg)
Review: Execution Stack Example
• Stack holds function arguments, return address
• Permits recursive execution• Crucial to modern languages
A(int tmp) { if (tmp<2) B(); printf(tmp);}B() { C();}C() { A(2);}A(1);exit;
StackPointer
Stack Growth
A: tmp=1 ret=addrZaddrX:
addrY:
addrU:
addrV:
addrZ:
.
.
.
.
.
.
.
.
.
.
.
.
![Page 34: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/34.jpg)
Review: Execution Stack Example
• Stack holds function arguments, return address
• Permits recursive execution• Crucial to modern languages
A(int tmp) { if (tmp<2) B(); printf(tmp);}B() { C();}C() { A(2);}A(1);exit;
StackPointer
Stack Growth
A: tmp=1 ret=addrZaddrX:
addrY:
addrU:
addrV:
addrZ:
.
.
.
.
.
.
.
.
.
.
.
.
![Page 35: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/35.jpg)
Review: Execution Stack Example
• Stack holds function arguments, return address
• Permits recursive execution• Crucial to modern languages
A(int tmp) { if (tmp<2) B(); printf(tmp);}B() { C();}C() { A(2);}A(1);exit;
StackPointer
Stack Growth
A: tmp=1 ret=addrZ
B: ret=addrY
addrX:
addrY:
addrU:
addrV:
addrZ:
.
.
.
.
.
.
.
.
.
.
.
.
![Page 36: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/36.jpg)
Review: Execution Stack Example
• Stack holds function arguments, return address
• Permits recursive execution• Crucial to modern languages
A(int tmp) { if (tmp<2) B(); printf(tmp);}B() { C();}C() { A(2);}A(1);exit;
StackPointer
Stack Growth
A: tmp=1 ret=addrZ
B: ret=addrY
addrX:
addrY:
addrU:
addrV:
addrZ:
.
.
.
.
.
.
.
.
.
.
.
.
![Page 37: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/37.jpg)
Review: Execution Stack Example
• Stack holds function arguments, return address
• Permits recursive execution• Crucial to modern languages
A(int tmp) { if (tmp<2) B(); printf(tmp);}B() { C();}C() { A(2);}A(1);exit;
StackPointer
Stack Growth
A: tmp=1 ret=addrZ
B: ret=addrY
C: ret=addrU
addrX:
addrY:
addrU:
addrV:
addrZ:
.
.
.
.
.
.
.
.
.
.
.
.
![Page 38: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/38.jpg)
Review: Execution Stack Example
• Stack holds function arguments, return address
• Permits recursive execution• Crucial to modern languages
A(int tmp) { if (tmp<2) B(); printf(tmp);}B() { C();}C() { A(2);}A(1);exit;
StackPointer
Stack Growth
A: tmp=1 ret=addrZ
B: ret=addrY
C: ret=addrU
addrX:
addrY:
addrU:
addrV:
addrZ:
.
.
.
.
.
.
.
.
.
.
.
.
![Page 39: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/39.jpg)
Review: Execution Stack Example
• Stack holds function arguments, return address
• Permits recursive execution• Crucial to modern languages
A(int tmp) { if (tmp<2) B(); printf(tmp);}B() { C();}C() { A(2);}A(1);exit;
A: tmp=2 ret=addrVStack
Pointer
Stack Growth
A: tmp=1 ret=addrZ
B: ret=addrY
C: ret=addrU
addrX:
addrY:
addrU:
addrV:
addrZ:
.
.
.
.
.
.
.
.
.
.
.
.
![Page 40: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/40.jpg)
Review: Execution Stack Example
• Stack holds function arguments, return address
• Permits recursive execution• Crucial to modern languages
A(int tmp) { if (tmp<2) B(); printf(tmp);}B() { C();}C() { A(2);}A(1);exit;
A: tmp=2 ret=addrVStack
Pointer
Stack Growth
A: tmp=1 ret=addrZ
B: ret=addrY
C: ret=addrU
addrX:
addrY:
addrU:
addrV:
addrZ:
.
.
.
.
.
.
.
.
.
.
.
.
![Page 41: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/41.jpg)
Review: Execution Stack ExampleA(int tmp) { if (tmp<2) B(); printf(tmp);}B() { C();}C() { A(2);}A(1);exit;
A: tmp=2 ret=addrVStack
Pointer
Stack Growth
A: tmp=1 ret=addrZ
B: ret=addrY
C: ret=addrU
addrX:
addrY:
addrU:
addrV:
addrZ:
.
.
.
.
.
.
.
.
.
.
.
.
Output:2
![Page 42: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/42.jpg)
Review: Execution Stack ExampleA(int tmp) { if (tmp<2) B(); printf(tmp);}B() { C();}C() { A(2);}A(1);exit;
StackPointer
Stack Growth
A: tmp=1 ret=addrZ
B: ret=addrY
C: ret=addrU
addrX:
addrY:
addrU:
addrV:
addrZ:
.
.
.
.
.
.
.
.
.
.
.
.
Output:2
![Page 43: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/43.jpg)
Review: Execution Stack ExampleA(int tmp) { if (tmp<2) B(); printf(tmp);}B() { C();}C() { A(2);}A(1);exit;
StackPointer
Stack Growth
A: tmp=1 ret=addrZ
B: ret=addrY
addrX:
addrY:
addrU:
addrV:
addrZ:
.
.
.
.
.
.
.
.
.
.
.
.
Output:2
![Page 44: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/44.jpg)
Review: Execution Stack ExampleA(int tmp) { if (tmp<2) B(); printf(tmp);}B() { C();}C() { A(2);}A(1);exit;
StackPointer
Stack Growth
A: tmp=1 ret=addrZaddrX:
addrY:
addrU:
addrV:
addrZ:
.
.
.
.
.
.
.
.
.
.
.
.
Output:21
![Page 45: CS 162 Discussion Section Week 2](https://reader035.fdocuments.in/reader035/viewer/2022081503/56815f05550346895dcdc585/html5/thumbnails/45.jpg)
Review: Execution Stack ExampleA(int tmp) { if (tmp<2) B(); printf(tmp);}B() { C();}C() { A(2);}A(1);exit;
addrX:
addrY:
addrU:
addrV:
addrZ:
.
.
.
.
.
.
.
.
.
.
.
.
Output:21