Chapter 5 Threads Overview Multithreading Models Threading Issues

  • Slides: 20
Download presentation
Chapter #5: Threads • Overview • Multithreading Models • Threading Issues Operating System Concepts

Chapter #5: Threads • Overview • Multithreading Models • Threading Issues Operating System Concepts

Threads (Cont. ) q A thread, sometimes called a lightweight process (LWP), is a

Threads (Cont. ) q A thread, sometimes called a lightweight process (LWP), is a basic unit of CPU utlization ü It comprises a thread ID, a program counter, a register set, and a stack. ü It shares with other threads belonging to the same process its code section, data section, and other OS resources. , such as open files. q A traditional (or heavyweight) process has a single thread of control. q if the process has multiple threads of control, it can do more than one task at a time, Operating System Concepts

Threads (Cont. ) q Many software packages that run on modern desktop PCs are

Threads (Cont. ) q Many software packages that run on modern desktop PCs are multithreaded. q. An application typically is implemented as a separate process with several threads of control. q. Examples: Ø A web browser might have one thread display images or text while another thread retrieves data from the network. Ø A word processor may have a thread for displaying graphics, another for reading keystrokes from the user, and a third thread for performing spelling and grammar checking in the background. Operating System Concepts

Multithreaded Process Address space + Threads + Resources • Address space: contains code and

Multithreaded Process Address space + Threads + Resources • Address space: contains code and data of a process. • Threads: are individual execution contexts. • Resources: are physical support necessary to run the process (memory, disk, …). Operating System Concepts

Single and Multithreaded Processes Operating System Concepts

Single and Multithreaded Processes Operating System Concepts

Benefits • Responsiveness: q Example: a multithreaded web browser could still allow user interaction

Benefits • Responsiveness: q Example: a multithreaded web browser could still allow user interaction in one thread while an image is being loaded in another thread. • Resource Sharing: q By default, threads share the memory and the resources of the process to which they belong. q The benefit: it allows an application to have several different threads of activity all within the same address space. Operating System Concepts

Benefits • Economy: q Allocating memory and resources for process creation is costly q

Benefits • Economy: q Allocating memory and resources for process creation is costly q It is more economical to create and context switch threads because they share resources. q In general, it is much more time consuming to create and manage processes than threads. • Utilization of multiprocessor Architectures: q Each thread may be running in parallel on a different processor q A single-threaded process can only run on one CPU, no matter how many are available. q Multithreading on a multi-CPU machine increases concurrency Operating System Concepts

Single vs. multiprocessor q. In a single-processor architecture, multithreading generally occurs by time-division multiplexing

Single vs. multiprocessor q. In a single-processor architecture, multithreading generally occurs by time-division multiplexing (as in multitasking): the CPU generally moves between each thread so quickly as to create an illusion of parallelism, but in reality only one thread is running at all time q. On a multiprocessor or multi-core system, the threads or tasks will generally run at the same time, with each processor or core running a particular thread or task. Operating System Concepts

User and Kernel Threads • Support for threads may be provided at either Ø

User and Kernel Threads • Support for threads may be provided at either Ø the user level, for user threads, Ø or by the kernel, for kernel threads. Operating System Concepts

User Threads • Thread management done by user-level threads library q Supported and managed

User Threads • Thread management done by user-level threads library q Supported and managed above the kernel and are implemented by a thread library at the user level. q The library provides support for thread creation, scheduling, and management with no support from the kernel. q The kernel is unaware of user-level threads, all thread creation and scheduling are done in user space without the need for kernel intervention. q User-level threads are generally fast to create and manage. Operating System Concepts

User Threads (Cont. ) q Drawbacks: if the kernel is single-threaded, then any user-level

User Threads (Cont. ) q Drawbacks: if the kernel is single-threaded, then any user-level thread performing a blocking system call will cause the entire process to block, even if other threads are available to run within the application. • Examples: Ø POSIX Pthreads Ø Mach C-threads Ø Solaris 2 UI-threads Operating System Concepts

Kernel Threads • Supported and managed by the Kernel (OS) • The kernel performs

Kernel Threads • Supported and managed by the Kernel (OS) • The kernel performs thread creation, scheduling, and management in kernel space. • Kernel threads are generally slower to create and manage than are user threads. • If a thread performs a blocking system call, the kernel can schedule another thread in the application for execution. • Also, in a multiprocessor environment, the kernel can schedule threads on different processors. • Examples - Windows 95/98/NT/2000 - Solaris 2 - Tru 64 UNIX - Be. OS Operating System Concepts

Multithreading Models • Many-to-One (User-level threading) • One-to-One (Kernel-level threading) • Many-to-Many (Hybrid threading)

Multithreading Models • Many-to-One (User-level threading) • One-to-One (Kernel-level threading) • Many-to-Many (Hybrid threading) Operating System Concepts

Many-to-One • Many user-level threads mapped to single kernel thread • It is efficient

Many-to-One • Many user-level threads mapped to single kernel thread • It is efficient because thread management is done in user space • Shortcoming: The entire process will block if a thread makes a blocking system call • Only one thread can access the kernel at a time, therefore multiple threads are unable to run in parallel on multiprocessors • Used on systems that do not support kernel threads. Operating System Concepts

Many-to-One Model Operating System Concepts

Many-to-One Model Operating System Concepts

One-to-One • Each user-level thread maps to kernel thread. • Allow more concurrency than

One-to-One • Each user-level thread maps to kernel thread. • Allow more concurrency than many-to-one by allowing another thread to run when a thread makes a blocking system call. • It also allows multiple threads to run in parallel on multiprocessors. • Shortcoming: The overhead of creating kernel threads can burden the performance of an application • Examples - Windows 95/98/NT/2000 - OS/2 Operating System Concepts

One-to-one Model Operating System Concepts

One-to-one Model Operating System Concepts

Many-to-Many Model (N : M) • Multiplexes many user-level threads to a smaller or

Many-to-Many Model (N : M) • Multiplexes many user-level threads to a smaller or equal number of kernel threads • Allows the operating system to create a sufficient number of kernel threads. • Suffers from neither of the shortcomings of the other two approaches (1: 1 and 1: N) q Developers can create as many user threads as necessary q. And the corresponding kernel threads can run in parallel on a multiprocessor. q. The kernel can schedule another thread for execution, when a thread performs a blocking system call. Operating System Concepts

Many-to-Many Model (cont. ) Examples: q Solaris 2 q IRIX q. HP-UX q. Tru

Many-to-Many Model (cont. ) Examples: q Solaris 2 q IRIX q. HP-UX q. Tru 64 UNIX Operating System Concepts

Many-to-Many Model Operating System Concepts

Many-to-Many Model Operating System Concepts