Chapter 4: Threads
description
Transcript of Chapter 4: Threads
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition,
Chapter 4: Threads
4.2 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Single and Multithreaded Processes
4.3 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Benefits
Responsiveness
One part of a program can continue running even if another part is blocked
Resource Sharing
Threads of the same process share the same memory space and resources.
Economy
Much less time consuming to create and manage threads than processes
Solaris 2: creating a process is 30 times slower than creating a thread, context switching is 5 times slower.
Scalability
Each thread can run in parallel on a different processor
4.4 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Multithreaded Server (for instance a Web Server)
Example of Use
4.5 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Multicore Programming & Multithreading
Multicore systems putting pressure on programmers, challenges include
Dividing activities
Balance
Data splitting
Data dependency
Testing and debugging
4.6 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Concurrent Execution on a Single-core System
4.7 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Parallel Execution on a Multicore System
4.8 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
User Threads
User threads supported above the kernel and managed without kernel support
Thread management done by user-level threads library
Three primary thread libraries:
POSIX Pthreads
Win32 threads
Java threads
4.9 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Kernel Threads
Supported by the Kernel
Examples
Windows XP/2000
Solaris
Linux
Tru64 UNIX
Mac OS X
4.10 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Multithreading Models
Many-to-One
One-to-One
Many-to-Many
4.11 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Many-to-One
Many user-level threads mapped to single kernel thread
Entire process blocks with a thread blocking system call
Examples:
Solaris Green Threads
GNU Portable Threads
4.12 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
One-to-One
Each user-level thread maps to kernel thread
Creation of a user thread requires creation of a kernel thread
Examples
Windows NT/XP/2000
Linux
Solaris 9 and later
4.13 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Many-to-Many Model
Allows many user level threads to be mapped to many kernel threads (smaller or equal number)
Allows the operating system to create a sufficient number of kernel threads
Solaris prior to version 9
Windows NT/2000 with the ThreadFiber package
4.14 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Many-to-Many Model
4.15 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Two-level Model
Similar to M:M, except that it also allows a user thread to be bound to kernel thread
Examples
IRIX
HP-UX
Tru64 UNIX
Solaris 8 and earlier
4.16 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Two-level Model
4.17 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Thread Libraries
Thread library provides programmer with API for creating and managing threads
Two primary ways of implementing
Library entirely in user space
Kernel-level library supported by the OS
4.18 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Pthreads
Refers to the POSIX standard (IEEE 1003.1c) API for thread creation and synchronization
May be provided either as user-level or kernel-level
POSIX standard specifies behavior of the thread library; Implementation is up to development of the library
Common in UNIX operating systems (Solaris, Linux, Mac OS X)
4.19 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Win32 Threads
Also known as Windows API or WinAPI
Win32 is a kernel-level library
Available on Windows systems (Windows 95, 98, NT, 2000 and XP)
4.20 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Windows XP Threads
Implements the one-to-one mapping, kernel-level
Each thread contains
A thread id
Register set
user stack or kernel stack
Private data storage area
The register set, stacks, and private storage area are known as the context of the threads
The primary data structures of a thread include:
ETHREAD (executive thread block)
KTHREAD (kernel thread block)
TEB (thread environment block)
4.21 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Windows XP Threads
4.22 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Linux Threads
Linux refers to them as tasks rather than threads
Thread creation is done through clone() system call
Flags, arguments of clone() specify sharing details
4.23 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Linux Threads
If CLONE_FS, CLONE_VM, CLONE_SIGHAND, and CLONE_FILES are passed to clone(), parent and child tasks share every thing
If none of these flags are passed to clone(), no sharing takes place, resulting in functionality similar to that of fork()
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition,
End of Chapter 4