1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
2 A Thread (Lightweight Process, LWP) A (traditional/ heavyweight) process has a single thread (of control). Modern operating systems allow a process to contain multiple threads of control. Each thread, is defined by its thread ID, program counter, register set & stack (for temporary data). A thread shares with other threads of the same process its code section, data section, open files & other operating system resources.
3 Multithreaded Applications Many applications are best executed as a single process with several threads of control Multithreading: single program composed of a number of different concurrent activities
4 Example Multithreaded Applications Web browser: –A thread displays information –A thread retrieves data from the network A Web Server: –A thread to listen for client requests –A thread to service each request Word Processor: –Thread ONE: displays graphics –Thread TWO: reads keystrokes –Thread THREE: performs spelling and grammar checking –Thread FOUR: ….
5 Single and Multithreaded Processes
6 Benefits of Multithreaded Programming Responsiveness e.g. a multithreaded web browser allows user interaction in one thread and downloads an image in another thread Resource Sharing threads share memory and other process resources
7 Benefits of Multithreaded Programming (continued) Economy: –Creation is 30 times slower for a process than a thread –Context switching is 5 times slower for a process than a thread More Efficient Utilization of Multiprocessor Architectures –threads run in parallel on different processors
8 Thread State Shared state: –Global variables –Heap variables Private state: –Stack variables –Registers When thread switch occurs, OS needs to save and restore private state
9 User Threads In user space and without kernel intervention, the library provides support for thread –Creation –Scheduling –Management Thread management done by user-level threads library User threads are fast to create and manage
10 Example User Threads Solaris threads: Sun Microsystems proprietary version of threads. Not portable. Pthreads –An Application Programming Interface, (API), standard for thread creation and synchronization specified by the IEEE Portable Operating Systems Interface, POSIX in –API specifies behavior of the thread library, implementation is up to development of the library. –Common in UNIX operating systems. –Pthreads are defined as a set of C language programming types and procedure calls, implemented with a pthread.h header/include file and a thread library.
11 Kernel Threads Kernel performs thread –Creation –Scheduling –Management In a MP (multiprocessing) environment, the kernel schedules threads on different processors Example operating systems with kernel threads: - Windows 95/98/NT/ Solaris - Linux
12 Multithreading Models Many-to-One One-to-One Many-to-Many
13 Many-to-One Many user-level threads mapped to single kernel thread. One user thread at a time can access the kernel –Not useful for multiprocessing Used on systems that do not support kernel threads.
14 Many-to-One Model
15 One-to-One Each user-level thread maps to a kernel thread. Allows another thread to run when a thread makes a blocking system call (more concurrency than many to 1) Allows multiple threads to run in parallel on multiprocessors Drawback: Creating a user thread requires creating a the corresponding kernel thread –To reduce overhead, restrict the number of threads supported by the system Examples - Windows 95/98/NT/ OS/2
16 One-to-one Model
17 Many-to-Many Model Allows many user level threads to be multiplexed to a smaller or equal number of kernel threads. Allows the operating system to create a sufficient number of kernel threads. Solaris 2 Windows NT/2000 with the ThreadFiber package
18 Many-to-Many Model