Lecture 12 Code examples on csserver in directory /home/hwang/cs470/lecture12 Reminder: Homework 3 and Process Management Project due on Wednesday. Questions? Monday, February 6 CS 470 Operating Systems - Lecture 12 1 Outline Threads Monday, February 6 CS 470 Operating Systems - Lecture 12 2 Threads So far, we have assumed that each process has a single thread of control. This is often called a heavyweight process. Most modern OS's allow a process to have multiple threads of control. Each one is often called a lightweight process. A thread is a flow of control within a process. It is the basic unit of CPU utilization. Like a process it has a thread ID (TID), program counter, registers, and a stack. Monday, February 6 CS 470 Operating Systems - Lecture 12 3 Threads Unlike a process, a thread shares code, data, and OS resources like open files with other threads in its process. code code code code data data data data stack stack stack stack stack stack : : : : : : Three processes Monday, February 6 : : Three threads CS 470 Operating Systems - Lecture 12 4 Multi-threaded Applications Many applications are now multi-threaded. Some are for convenience. E.g. web browser might have threads to display images, handle network traffic, … For others, multiple threads is almost necessary. E.g. web server, since do not want to handle requests serially. Traditional implementations fork a new process to handle a request. Since each request usually is independent of each other, this works well. Monday, February 6 CS 470 Operating Systems - Lecture 12 5 Multi-threaded Applications For applications with lots of shared data, forking a new process is a very heavyweight technique with lots of overhead. Creating a thread is much easier. Modern OS kernels are often multi-threaded with each service being handled by a separate thread. Examples: Solaris interrupt handling, Linux free memory management. Monday, February 6 CS 470 Operating Systems - Lecture 12 6 Benefits of Multi-threading Responsiveness: an application can continue to run even if parts are blocked. This is especially good for networked applications Resource sharing: decreases contention. Economy: easier to create and switch between threads. Example: in Solaris 2, process creation 30x slower; context switch 5x slower. Multi-core/processor architectures: easier to utilize. Monday, February 6 CS 470 Operating Systems - Lecture 12 7 Multi-core Programming Multiple cores appear as separate processors to the OS. Multi-threaded programming is a mechanism to take advantage of this. E.g., 4 threads on a single core will alternate: 123412341234... On a dual-core machine, might assign two threads to each core: 131313... and 242424... On a quad-core, perhaps one thread per core. Monday, February 6 CS 470 Operating Systems - Lecture 12 8 Multi-core Programming Challenges to effective utilization How to divide activities? Is this a programmer issue? How to balance activities? Equal work or equal value from each core. How to manage data? How is it divided among the cores? How to synchronize threads? One task may depend on the result of another. How to test and debug? Much more difficult. Monday, February 6 CS 470 Operating Systems - Lecture 12 9 Thread Models Generally, two types of threads Kernel-level: running as part of the OS. Creation is similar to process creation. Most OS's support this. User-level: running on top of the OS and implemented as a library API. Programmers usually deal with user-level threads. These are mapped onto kernel-level threads by the OS. Monday, February 6 CS 470 Operating Systems - Lecture 12 10 Many-To-One Multiple user-level threads are mapped to one kernel level thread. This is not true concurrency. user user kernel Fast, since all management is in user space. But system calls block the entire process. user Examples: Solaris Green threads, GNU Portable Threads Monday, February 6 CS 470 Operating Systems - Lecture 12 11 One-To-One One user thread maps to one kernel thread. More parallelism, can allocate on separate processors. user user user kernel kernel kernel Can run another thread when one blocks. Kernel thread creation is relatively expensive, so may limit the number of threads. Examples: Linux, Win95 & later, Solaris 9 & later Monday, February 6 CS 470 Operating Systems - Lecture 12 12 Many-To-Many Overcomes limitations of previous models. Use as many user-level threads as want to. user user user kernel kernel kernel Create as many kernel-level threads as needed. User-level threads are multiplexed among the kernel threads. Examples: IRIX, HP-UX, pre-Solaris 9 Monday, February 6 CS 470 Operating Systems - Lecture 12 13 Thread Libraries Libraries provide programmer API for creating and managing threads. They can be user-level (regular library) or kernel-level (system calls). Three main ones in use: P(osix) threads: standard specification of behavior, not an implementation. Can be either user-level or kernel-level. Most systems have or can get one. Win32 threads: kernel-level Programming language API, e.g. Java thread objects. Monday, February 6 CS 470 Operating Systems - Lecture 12 14 Library Examples File thrd-posix.c shows use of the pthreads library. To compile, need to explicitly link the pthread library $gcc ­o th_xmpl thrd­posix.c ­lpthread See "man pthread.h" for overview documentation. Also see man pages for individual functions, e.g. pthread_create. File thrd-win32.c shows use of Win32 threads. Monday, February 6 CS 470 Operating Systems - Lecture 12 15 Library Examples File Driver.java show use of Java thread API. Objects either extend Thread class directly or implement the Runnable interface. The Runnable interface requires a method named run that is executed on creation. Since thread object is part of JVM, usually implemented on top of native thread support. To compile and run Java programs $ javac Driver.java $ java Driver Monday, February 6 CS 470 Operating Systems - Lecture 12 16 Threading Issues How does fork( ) work? fork( ) creates a copy of a process. Should it duplicate all threads, or just the calling thread? Perhaps it depends on whether exec( ) is being called? How does thread cancellation work? Asynchronous cancellation: immediately terminate thread. What problems may arise? Most common. Deferred cancellation: schedule a cancellation, but wait for target to notice and terminate self. E.g. Pthreads have cancellation points. Monday, February 6 CS 470 Operating Systems - Lecture 12 17 Threading Issues How does signal handling work? Signals get delivered to a process. Which thread handles the signal? Thread to which signal applies. E.g., synchronous signals in response to an illegal operation. All threads. E.g., asynchronous signal like SIGINT that terminates a process. Subset of threads. Perhaps handled by the first non-blocked thread in the set. A dedicated signal handling thread. E.g., Solaris 2. Monday, February 6 CS 470 Operating Systems - Lecture 12 18 Thread Creation Threads may be created more often than processes, so want creation to be fast. Common technique is to have a pool of threads that are waiting to be loaded with code and run. This is also done with processes, e.g. web servers. Pools can also be used to limit the number of threads/processes running at a given time. Monday, February 6 CS 470 Operating Systems - Lecture 12 19 Examples Example: Win XP implementation of Win32 API OS runs each application as a process Process can have more than one thread Default model is one-to-one Can use Fiber library to get many-to-many Example: Linux threads Uses clone( ) to create. Works like fork( ). Separate flags to control sharing. If share all, a true thread. If share none, almost a process. Monday, February 6 CS 470 Operating Systems - Lecture 12 20