Threads ICS 240 Operating Systems William Albritton Information
Threads • ICS 240: Operating Systems – William Albritton • Information and Computer Sciences Department at Leeward Community College – Original slides by Silberschatz, Galvin, and Gagne © 2007 from Operating System Concepts with Java, 7 th Edition with some modifications – Also includes material by Dr. Susan Vrbsky from the Computer Science Department at the University of Alabama 12/21/2021 1
Threads • A thread (light-weight process) is a single sequential flow of control consisting of a PC (program counter), register set, stack space – Threads share code section, data section, and OS resources (such as files) • These items are combined for a task – A task is the execution environment in which threads run consisting of a code section, data section, and OS resources (such as files) • A traditional process is a task with one thread (heavy-weight process) 12/21/2021 2
Single & Multithreaded Processes 12/21/2021 3
Examples • Multiple threads allow for multiple points of execution – A Web browser may have one thread display text, while another thread retrieves data from a network – A word processor may have one thread handling the user’s keystrokes, and another thread doing the spell check – A Web server may be serving 1, 000 different clients with 1, 000 different threads 12/21/2021 4
Benefits of Using Threads 1. Responsiveness – An application can do several things as once • For example, a browser can use one thread for I/O and another thread to download an image 2. Resource Sharing – Code, data, and other resources are shared • 12/21/2021 Can have multiple threads using the same address space in memory 5
Benefits of Using Threads 3. Economy – Thread creation is less costly than process creation • With the Solaris Operating System of Sun Microsystems, creating a thread is 30 times faster than creating a process, and 5 times faster than context switching 4. Utilization of MP (Multiple Processor) Architectures – Schedule a separate thread on each processor 12/21/2021 6
User and Kernel Threads • User threads – Thread management done by user-level threads library. • Kernel threads – Threads directly supported by the kernel. 12/21/2021 7
Multithreading Models • Mapping user threads to kernel threads 1. Many-to-One 2. One-to-One 3. Many-to-Many 12/21/2021 8
Many-to-One • Many user-level threads mapped to a single kernel thread – Efficient, because thread management done by thread library in user space – Cannot take advantage of running multiple threads in parallel on multiprocessors • Examples – Solaris Green Threads – GNU Portable Threads 12/21/2021 9
Many-to-One Model 12/21/2021 10
One-to-One • Each user-level thread maps to a single kernel thread – Kernel threads can run in parallel on a multiprocessor – Possible to create too many kernel threads and overburden the system • Examples – Windows NT/XP/2000 – Linux – Solaris 9 and later 12/21/2021 11
One-to-one Model 12/21/2021 12
Many-to-Many Model • Allows many user level threads to be mapped to many kernel threads – Allows the operating system to create a sufficient number of kernel threads – Kernel threads can run in parallel on a multiprocessor • Examples – Solaris prior to version 9 – Windows NT/2000 with the Thread. Fiber package 12/21/2021 13
Many-to-Many Model 12/21/2021 14
Java Threads • Java threads are managed by the JVM (Java Virtual Machine) – The JVM is can be thought of as a software computer that runs inside a hardware computer • Java threads may be created by: – Implementing the Runnable interface 12/21/2021 15
Summation Program 12/21/2021 16
Summation Program 12/21/2021 17
Producer-Consumer Program 12/21/2021 18
Producer-Consumer Program 12/21/2021 19
Producer-Consumer Program 12/21/2021 20
- Slides: 20