CS 162: Operating Systems and Systems Programming Lecture 4: Threads and Concurrency
Total Page:16
File Type:pdf, Size:1020Kb
CS 162: Operating Systems and Systems Programming Lecture 4: Threads and Concurrency Sept 10, 2019 Instructor: David E. Culler https://cs162.eecs.berkeley.edu Read: A&D 5.1-3, 5.7.1 HW 1 due 9/18 Groups and Section Pref Wed 9/10/19 cs162 Fa19 L08 Proj 1 released, Design doc1 Tues Recall: Multiplexing Processes • Snapshot of each process in its PCB • Only one thread active at a time per core… • Give out CPU to different processes • Scheduling • Policy Decision • Give out non-CPU resources • Memory/IO • Another policy decision Process Control Block 9/10/19 cs162 Fa19 L08 2 TCB, Stacks and Recall: Context Switch Register Mgmt 9/10/19 cs162 Fa19 L08 3 Recall: Lifecycle of a process / thread Scheduler dispatches proc/thread to run: existing proc context_switch to it terminated “forks” a new proc ready running exit syscall Create OS repr. of proc or abort • Descriptor • Address space interrupt, syscall, • Thread(s) sleep, blocking call • … Queue for scheduling completion waiting • OS juggles many process/threads using kernel data structures • Proc’s may create other process (fork/exec) • All starts with init process at boot Pintos: process.c 9/10/19 cs162 Fa19 L08 4 Recall: Process Management • exit – terminate a process • fork – copy the current process • exec – change the program being run by the current process • wait – wait for a process to finish • kill – send a signal (interrupt-like notification) to another process • sigaction – set handlers for signals 9/10/19 cs162 Fa19 L08 5 Recall: Process Management child fork pid = fork(); exec main () { if (pid == 0) ... exec(...); else } wait(pid); pid = fork(); if (pid == 0) exec(...); parent else wait(pid); pid = fork(); if (pid == 0) wait exec(...); else wait(pid); 9/10/19 cs162 Fa19 L08 6 User/OS Threading Models Simple One-to-One Threading Model Almost all current implementations 9/10/19 Many-to-One cs162 Fa19 L08 Many-to-Many 7 Single vs. Multithreaded Processes 9/10/19 cs162 Fa19 L08 8 To d ay • What, Why, and How of Threads • Kernel-Supported User Threads • Coordination among Threads • Synchronization • Implementing Synchronization • User-level Threads 9/10/19 cs162 Fa19 L08 9 Definitions • A thread is a single execution sequence that represents a separately schedulable task • Protection is an orthogonal concept • Can have one or many threads per protection domain • Single threaded user program: one thread, one protection domain • Multi-threaded user program: multiple threads, sharing same data structures, isolated from other user programs • Multi-threaded kernel: multiple threads, sharing kernel data structures, capable of using privileged instructions 9/10/19 cs162 Fa19 L08 10 Threads Motivation • Operating systems need to be able to handle multiple things at once (MTAO) • processes, interrupts, background system maintenance • Servers need to handle MTAO • Multiple connections handled simultaneously • Parallel programs need to handle MTAO • To achieve better performance • Programs with user interfaces often need to handle MTAO • To achieve user responsiveness while doing computation • Network and disk bound programs need to handle MTAO • To hide network/disk latency • Sequence steps in access or communication 9/10/19 cs162 Fa19 L08 11 Silly Example for Threads Imagine the following program: main() { ComputePI(“pi.txt”); PrintClassList(“classlist.txt”); } • What is the behavior here? • Program would never print out class list • Why? ComputePI would never finish 9/10/19 cs162 Fa19 L08 12 Adding Threads • Version of program with Threads (loose syntax): main() { thread_fork(ComputePI, “pi.txt” )); thread_fork(PrintClassList, “classlist.txt”)); } • thread_fork: Start independent thread running given procedure • What is the behavior here? • Now, you would actually see the class list • This should behave as if there are two separate CPUs CPU1 CPU2 CPU1 CPU2 CPU1 CPU2 Time 9/10/19 cs162 Fa19 L08 13 More Practical Motivation Back to Jeff Dean's "Numbers everyone should know" Handle I/O in separate thread, avoid blocking other progress 9/10/19 cs162 Fa19 L08 14 Little Better Example for Threads? Imagine the following program: main() { … ReadLargeFile(“pi.txt”); RenderUserInterface(); } • What is the behavior here? • Still respond to user input • While reading file in the background 9/10/19 cs162 Fa19 L08 15 Voluntarily Giving Up Control • I/O – e.g. keypress • Waiting for a signal from another thread • Thread makes system call to wait • Thread executes thread_yield() • Relinquishes CPU but puts calling thread back on ready queue 9/10/19 cs162 Fa19 L08 16 Adding Threads • Version of program with Threads (loose syntax): main() { thread_fork(ReadLargeFile, “pi.txt” ); thread_fork(RenderUserInterface, “classlist.txt”); } • thread_fork: Start independent thread running given procedure • What is the behavior here? • Now, you would actually see the class list • This should behave as if there are two separate CPUs CPU1 CPU2 CPU1 CPU2 CPU1 CPU2 Time 9/10/19 cs162 Fa19 L08 17 Switching Threads • Consider the following code blocks: Thread S Thread T func A() { A A B(); } B(while) B(while) growth func B() { yield yield while(TRUE) { yield(); Stack run_new_thread run_new_thread } switch switch } • Two threads, S and T, each run A Thread S's switch returns to Thread T's (and vice versa) 9/10/19 cs162 Fa19 L08 18 Aren't we still switching contexts? • Yes, but much cheaper than switching processes • No need to change address space • Some numbers from Linux: • Frequency of context switch: 10-100ms • Switching between processes: 3-4 μsec. • Switching between threads: 100 ns 9/10/19 cs162 Fa19 L08 19 Processes vs. Threads Process 1 Process N • Switch overhead: threads threads • Same process: low Mem. Mem. • Different proc.: high IO IO • Protection … state … … state • Same proc: low CPU CPU CPU CPU state state state state • Different proc: high • Sharing overhead CPU OS • Same proc: low sched. • Different proc: high 1 thread at a time CPU (1 core) 9/10/19 cs162 Fa19 L08 20 Processes vs. Threads Process 1 Process N • Switch overhead: threads threads • Same process: low Mem. Mem. • Different proc.: high IO IO • Protection … state … … state • Same proc: low CPU CPU CPU CPU state state state state • Different proc: high • Sharing overhead CPU OS • Same proc: low sched. • Different proc: high 4 threads at a time Core Core Core Core 1 2 3 4 9/10/19 cs162 Fa19 L08 21 Example: Multithreaded Server serverLoop() { connection = AcceptNewConnection(); (thread_)fork(ServiceWebPage, connection); } • One process/thread per connection, many concurrent connections • Process (isolation) vs Thread (performance) • How fast is creating threads? • Better than fork(), but still overhead • Problem: What if we get a lot of requests? • Might run out of memory (thread stacks) • Schedulers usually have trouble with too many threads 9/10/19 cs162 Fa19 L08 22 Web Server: Thread Pools • Bounded pool of worker threads • Allocated in advance: no thread creation overhead • Queue of pending requests • Limited number of requests in progress Request Master Client Thread Queue Thread Pool Response 9/10/19 cs162 Fa19 L08 23 Multiprocessing vs Multiprogramming • Multiprocessing: Multiple cores • Multiprogramming: Multiple Jobs/Processes • Multithreading: Multiple threads/processes • What does it mean to run two threads concurrently? • Scheduler is free to run threads in any order and interleaving A Multiprocessing B C A B C Multiprogramming A B C A B C B 9/10/19 cs162 Fa19 L08 24 Thread vs. Process State • Process-wide state: • Memory contents (global variables, heap) • I/O bookkeeping • Thread-”local" state: • CPU registers including program counter • Execution stack • Kept in Thread Control Block 9/10/19 cs162 Fa19 L08 25 Shared vs. Per-Thread State Shared Per−Thread Per−Thread State State State Thread Control Thread Control Heap Block (TCB) Block (TCB) Stack Stack Information Information Saved Saved Global Registers Registers Variables Thread Thread Metadata Metadata Stack Stack Code 9/10/19 cs162 Fa19 L08 26 Memory Footprint: Two Threads • Two sets of CPU registers Stack 1 • Two sets of Stacks • Issues: Stack 2 Space Address • How do we position stacks relative to each other? • What maximum size should we choose for the stacks? Heap • What happens if threads violate this? • How might you catch violations? Global Data • User threads need ‘proper’ stacks Code • System threads may be very constrained 9/10/19 cs162 Fa19 L08 27 Yield is covered, what about I/O? CopyFile Stack growth Stack read Trap to OS kernel_read run_new_thread switch • User code invokes syscall • IO operation initiated (more later) • Run new thread, switch • Really, same thing as before • Just put the thread on a different queue 9/10/19 cs162 Fa19 L08 28 Preempting a Thread • What happens if thread never does any I/O, never waits, and never yields control? • Must find way that dispatcher can regain control! • Interrupts: signals from hardware or software that stop the running code and jump to kernel • Timer: like an alarm clock that goes off every some milliseconds • Interrupt is a hardware-invoked mode switch • Handled immediately, no scheduling required 9/10/19 cs162 Fa19 L08 29 Example: Network Interrupt Raise priority (set mask) ... Ints Save registers add $r1,$r2,$r3 PC saved Reenable Ints subi $r4,$r1,#4 Dispatch to Handler slli $r4,$r4,#2 Disable All … Kernel Mode ... Transfer Network Packet Pipeline Flush from hardware to Kernel Buffers ... … lw $r2,0($r4) Restore PC Restore registers lw $r3,4($r4) Enable all User Mode Clear current Int External Interrupt External