CSE120 Principes of Operating Systems Prof Yuanyuan (YY) Zhou Lecture 4: Threads
Announcement Project 0 Due Project 1 out Homework 1 due on Thursday Submit it to Gradescope onine 2
Processes Reca that a process incudes many things An address space (defining a the code and data pages) OS resources (e.g., open fies) and accounting information Execution state (PC, SP, regs, etc.) Creating a new process is costy because of a of the data structures that must be aocated and initiaized Reca struct proc in Soaris Communicating between processes is costy because most communication goes through the OS Overhead of system cas and copying data 3
Parae Programs To execute these programs we need to Create severa processes that execute in parae Have the OS schedue these processes in parae (ogicay or physicay) This situation is very inefficient Space: PCB, page tabes, etc. Time: create data structures, fork and copy addr space, etc. Soution: possibe to have cooperating processes? 4
Rethinking Processes Key idea: Why don t we separate the concept of a process from its execution state? Process: address space, privieges, resources, etc. Execution state: PC, SP, registers Exec state aso caed thread of contro, or thread 5
Threads Thread vs Process A thread defines a sequentia execution stream within a process (PC, SP, registers) A process defines the address space and genera process attributes (everything but threads of execution) A thread is bound to a singe process A process, however, can have mutipe threads Threads become the unit of scheduing Processes are now the containers in which threads execute 6
Threads: Lightweight Processes A sequentia execution stream within a process Environment (resource) execution 7 (a) Three processes each with one thread (b) One process with three threads
The Thread Mode 8 Shared information Processor info: parent process, time, etc Memory: goba data, heap, page tabe, and stats, etc I/O and fie: communication ports, directories and fie descriptors, etc Private state State (ready, running and bocked) Registers Program counter Execution stack Why? Each thread execute separatey
Threads in a Process Stack (T1) Thread 1 Thread 2 Stack (T2) Stack (T3) Thread 3 9 PC (T2) Heap Static Data Code PC (T3) PC (T1)
Anaogy Process: 3 projects for different casses (CSE120, CSE140, CSE110) Each one has different text book, different web pages, different TAs/Instructors Threads: 3 activities in CSE120 (Homework, Lectures, Projects) Share the same concepts (textbook) Share TA/Tutors A of them are going on in parae (within one quarter) Each has their own things, too 10 10/7/18
A 2-min Expainer Video https://www.youtube.com/watch?v=dhf- DYO1K78 11
Threads: Concurrent Servers Using fork() to create new processes to hande requests in parae is overki for such a simpe task Reca our forking Web server: 12 whie (1) { } int sock = accept(); if ((chid_pid = fork()) == 0) { Hande cient request Cose socket and exit } ese { Cose socket }
Threads: Concurrent Servers Instead, we can create a new thread for each request web_server() { } whie (1) { } int sock = accept(); thread_fork(hande_request, sock); Difference from fork()? 13 hande_request(int sock) { Process request cose(sock); }
Thread Usage: Web Server 14
Thread Usage: word processor A thread can wait for I/O, whie the other threads can sti running. What if it is singe-threaded? 15
Windows Thread Lists from Performance Monitor 16
Windows Performance Anayzer 17
Mac OS Activity Monitor 18 10/7/18 CSE120 - Operating Systems, Yuanyuan Zhou
Thread Information on Linux Process information: Read /proc/[your PID]/stat fie Thread information (2.6 kerne): Read /proc/[your PID]/task/[thread ID]/stat 19
Kerne-managed Threads We have taken the execution aspect of a process and separated it out into threads To make concurrency cheaper As such, the OS now manages threads and processes A thread operations are impemented in the kerne The OS schedues a of the threads in the system OS-managed threads are caed kerne-eve threads or Kerne managed threads or ightweight processes NT: threads Soaris: ightweight processes (LWP) POSIX Threads (pthreads): PTHREAD_SCOPE_SYSTEM 20
Kerne-managed Thread Limitations Kerne-managed threads make concurrency much cheaper than processes Much ess state to aocate and initiaize However, for fine-grained concurrency, kernemanaged threads sti suffer from too much overhead Thread operations sti require system cas Ideay, want thread operations to be as fast as a procedure ca Kerne-eve threads have to be genera to support the needs of a programmers, anguages, runtimes, etc. For more fine-grained concurrency, need even cheaper threads 21
User-Leve-Managed Threads 22 To make threads cheap and fast, they need to be impemented at user eve Kerne-eve managed threads are managed by the OS User-eve managed threads are managed entirey by the runtime system (user-eve ibrary) User-eve-managed threads are sma and fast A thread is simpy represented by a PC, registers, stack, and sma thread contro bock (TCB) Creating a new thread, switching between threads, and synchronizing threads are done via procedure ca No kerne invovement User-eve managed thread operations 100x faster than kerne managed threads pthreads: PTHREAD_SCOPE_PROCESS
User eve threads Kerne eve Managed threads User eve Managed threads 23
Sma and Fast Nachos thread contro bock cass Thread { int *stack; int *stacktop; int machinestate[machinestatesize]; ThreadStatus status; char *name; <Methods> }; 24
User Leve Managed Thread Limitations But, user-eve managed threads are not a perfect soution As with everything ese, they are a tradeoff User-eve managed threads are invisibe to the OS They are not we integrated with the OS As a resut, the OS can make poor decisions Scheduing a process with ide threads Bocking a process whose thread initiated an I/O, even though the process has other threads that can execute Unscheduing a process with a thread hoding a ock Soving this requires communication between the kerne and the user-eve thread manager 25
Tradeoffs between the two Kerne-eve managed threads Integrated with OS (informed scheduing) Sow to create, manipuate, synchronize User-eve managed threads Fast to create, manipuate, synchronize Not integrated with OS (uninformed scheduing) Understanding the differences between kerne and user-eve managed threads is important For programming (correctness, performance) 26
Kerne and User Threads Or use both kerne and user-eve threads Can associate a user-eve thread with a kerne-eve thread Or, mutipex user-eve threads on top of kerne-eve threads Java Virtua Machine (JVM) (aso pthreads) Java threads are user-eve threads On oder Unix, ony one kerne thread per process Mutipex a Java threads on this one kerne thread On NT, modern Unix Can mutipex Java threads on mutipe kerne threads Can have more Java threads than kerne threads 27
Impementing Threads Impementing threads has a number of issues Interface Context switch Preemptive vs. non-preemptive Scheduing Synchronization (next ecture) Focus on user-eve managed threads Kerne-eve managed threads are simiar to origina process management and impementation in the OS What you wi be deaing with in Nachos Not ony wi you be using threads in Nachos, you wi be impementing more thread functionaity 28
Sampe Thread Interface 29 thread_fork(procedure_t) Create a new thread of contro Aso thread_create(), thread_setstate() thread_stop() Stop the caing thread; aso thread_bock thread_start(thread_t) Start the given thread thread_yied() Vountariy give up the processor thread_exit() Terminate the caing thread; aso thread_destroy Thread_join(t) or t.join() causes the current thread to pause execution unti t's thread terminates
Thread Scheduing The thread scheduer determines when a thread runs It uses queues to keep track of what threads are doing Just ike the OS and processes But it is impemented at user-eve in a ibrary Run queue: Threads currenty running (usuay one) Ready queue: Threads ready to run Are there wait queues? How woud you impement thread_seep(time)? 30
Non-Preemptive Scheduing Threads vountariy give up the CPU with thread_yied Ping Thread Pong Thread whie (1) { printf( ping\n ); thread_yied(); } whie (1) { printf( pong\n ); thread_yied(); } 31 What is the output of running these two threads?
thread_yied() Wait a second. How does thread_yied() work? The semantics of thread_yied are that it gives up the CPU to another thread In other words, it context switches to another thread So what does it mean for thread_yied to return? It means that another thread caed thread_yied! Execution trace of ping/pong printf( ping\n ); thread_yied(); printf( pong\n ); thread_yied(); 32
Impementing thread_yied() thread_yied() { } thread_t od_thread = current_thread; current_thread = get_next_thread(); append_to_queue(ready_queue, od_thread); context_switch(od_thread, current_thread); return; As od thread As new thread The magic step is invoking context_switch() Why do we need to ca append_to_queue()? 33
Thread Context Switch The context switch routine does a of the magic Saves context of the currenty running thread (od_thread) Push a machine state onto its stack (not its TCB) Restores context of the next thread Pop a machine state from the next thread s stack The next thread becomes the current thread Return to the NEW thread This is a done in assemby anguage It works at the eve of the procedure caing convention, so it cannot be impemented using procedure cas See code/threads/switch.s in Nachos 34
Preemptive Scheduing Non-preemptive threads have to vountariy give up CPU A ong-running thread wi take over the CPU Ony vountary cas to thread_yied(), thread_stop(), or thread_exit() causes a context switch Preemptive scheduing causes an invountary context switch Need to regain contro of processor asynchronousy How? Use timer/aarm interrupt 35
Bocking Vs. non-bocking System Cas Bocking system ca Usuay I/O reated: read(), fread(), getc(), write() Doesn t return unti the ca competes The process/thread is switched to bocked state When the I/O competes, the process/thread becomes ready Simpe Rea ife exampe: attending a ecture Using non-bocking system ca for I/O Asynchronous I/O Compicated The ca returns once the I/O is initiated, and the caer continue Once the I/O competes, an interrupt is deivered to the caer Rea ife exampe: appy for job 36
Threads Summary 37 The operating system as a arge mutithreaded program Each process executes as a thread within the OS Mutithreading is aso very usefu for appications Efficient mutithreading requires fast primitives Processes are too heavyweight Soution is to separate threads from processes Kerne-eve managed threads much better, but sti significant overhead User-eve managed threads even better, but not we integrated with OS Now, how do we get our threads to correcty cooperate with each other? Synchronization