Threads
Operating System Concepts – 8th Edition, Silberschatz, Galvin and Gagne ©2009
Motivation
Context switch time was a bottleneck and needed new
structures to avoid it if possible
Previously we assumed a process had a single thread
of control
If the process has multiple threads of control, it can do
more than one task at a time
Operating System Concepts – 8th Edition 4.2 Silberschatz, Galvin and Gagne ©2009
Motivation
Modern OS allow a process to have multiple threads of control
A thread is a lightweight* process
Threads are the entities scheduled for execution on the CPU
Operating System Concepts – 8th Edition 4.3 Silberschatz, Galvin and Gagne ©2009
A process model has two concepts:
1. Resource grouping
2. Execution
Sometimes it is useful to separate them
Operating System Concepts – 8th Edition 4.4 Silberschatz, Galvin and Gagne ©2009
Distinguish between the two concepts
Address
space/Global
Variables Open
files
Child processes
Accounting info
Signal handlers
Program counter
Registers In case of multiple
Stack threads per process
Unit of Resource StateSplit Unit of Dispatch
Program counter
Address
Registers
space/Global Program counter
Variables Open files Sta ck
Registers
Child processes te
S tac Program counter
Accounting info Sta
Sk Registers
SSyisgtemnCaonl cehptas –n8 d
Operating t h
tate Stack State
l eitiorns
Share
E d
4.5 Silberschatz, Galvin and Gagne ©2009
Overview- Threads
A thread is a basic unit of CPU utilization
A thread consists of :
A thread id
A program counter
A register set
A stack
It shares with other threads in the same process;
Its code section
Data section
Other OS resources such as open files and signals
A traditional (or heavyweight ) process has a single thread
A Multithreaded process can perform more than one task at a time
Operating System Concepts – 8th Edition 4.6 Silberschatz, Galvin and Gagne ©2009
Single and Multithreaded Processes
Operating System Concepts – 8th Edition 4.7 Silberschatz, Galvin and Gagne ©2009
Multithreaded Server Architecture
When a server receives a message it services the
message using a separate thread, allowing it to service
concurrent requests.
Operating System Concepts – 8th Edition 4.8 Silberschatz, Galvin and Gagne ©2009
Benefits
Responsiveness
allows a program to continue running even if part of it is blocked,
increasing responsiveness to user
Resource Sharing
Threads share memory by default
Benefit of sharing code and data – can have several different threads of
activity in same address space
Economy
Threads share resources (30 times faster than creating new proceseses
and five times faster than a context switch in Solaris)
Scalability
Single threaded processes run on one CPU; multithreaded processes can
run in parallel on several CPUs in a multiprocessor machine
Operating System Concepts – 8th Edition 4.9 Silberschatz, Galvin and Gagne ©2009
Multicore Programming
Multicore or multiprocessor systems puts pressure on
programmers, challenges include:
• Dividing activities
• Balance
• Data splitting
• Data dependency
• Testing and debugging
Parallelism implies a system can perform more than one
task simultaneously
Concurrency supports more than one task making
progress
• Single processor / core, scheduler providing concurrency
Concurrency vs. Parallelism
Concurrent execution on single-core system:
Parallelism on a multi-core system:
Multicore Programming
Types of parallelism
Data parallelism – distributes subsets of the same
data across multiple cores, same operation on each
Task parallelism – distributing threads across
cores, each thread performing unique operation
Data and Task Parallelism
User Threads
Threads are either user threads or kernel threads
User threads managed without kernel support in user space
Thread management done by user-level threads library
Library provides for thread creation, scheduling, and management with
no support from the kernel.
User-level threads are generally fast to create and manage.
Three primary thread libraries:
POSIX Pthreads (We will work with this)
Win32 threads
Java threads
Operating System Concepts – 8th Edition 4.14 Silberschatz, Galvin and Gagne ©2009
Kernel Threads
Supported by the Kernel
Kernel threads supported/managed by the OS itself.
The kernel performs thread creation, scheduling, and management in
kernel space.
Because thread management is done by the OS, kernel threads are
generally slower to create and manage than are user threads.
Kernel-level threads are especially good for applications that frequently
block.
Examples
Windows XP/2000
Solaris
Linux
Tru64 UNIX
Mac OS X
Operating System Concepts – 8th Edition 4.15 Silberschatz, Galvin and Gagne ©2009
Multithreading Models
A relationship must exist between user and kernel threads
Many-to-One – maps many user threads to one kernel thread –blocks entire
process when one thread blocks ( Solaris)
One-to-One – maps each user thread to a kernel thread- provides more
concurrency by allowing another thread to run when one is blocked. Each
user thread requires a kernel thread – extra overhead. (Linux)
Many-to-Many – multiplexes many user threads to smaller or equal
number of kernel threads, which are often specific to a particular application
or machine. Developers can create many threads and they can run in
parallel on a multiprocessor. When one thread is blocked the kernel
schedules another to run.
Operating System Concepts – 8th Edition 4.16 Silberschatz, Galvin and Gagne ©2009
Many-to-One
Many user-level threads mapped to single kernel thread
Examples:
Solaris Green Threads
GNU Portable Threads
Operating System Concepts – 8th Edition 4.17 Silberschatz, Galvin and Gagne ©2009
One-to-One
Each user-level thread maps to kernel thread
Examples
Windows NT/XP/2000
Linux
Solaris 9 and later
Operating System Concepts – 8th Edition 4.18 Silberschatz, Galvin and Gagne ©2009
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
Solaris prior to version 9
Windows NT/2000 with the ThreadFiber package
Operating System Concepts – 8th Edition 4.19 Silberschatz, Galvin and Gagne ©2009
Two-level Model
Similar to M:M, except that it allows a user thread to be bound to kernel
thread
Examples:
IRIX
HP-UX
Tru64 UNIX
Solaris 8 and earlier
Operating System Concepts – 8th Edition 4.20 Silberschatz, Galvin and Gagne ©2009
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
Main thread libraries:
POSIX Pthreads
Win32
Java
Operating System Concepts – 8th Edition 4.21 Silberschatz, Galvin and Gagne ©2009
Threading Issues
Semantics of fork() and exec() system calls
Thread cancellation of target thread
Asynchronous or deferred
Signal handling
Thread pools
Thread-specific data
Scheduler activations
Operating System Concepts – 8th Edition 4.22 Silberschatz, Galvin and Gagne ©2009
Semantics of fork() and exec()
Does fork() duplicate only the calling thread or all threads?
Some Unix systems have two versions; one duplicates all threads and one
duplicates only the thread that invoked the fork() system call.
Which version is used depends on the application
If a thread invokes exec() the program specified in the parameter will
replace the entire process and all its threads.
When exec() follows a call to fork(), there is no need to duplicate all threads
since the entire program will be replaced.
Operating System Concepts – 8th Edition 4.23 Silberschatz, Galvin and Gagne ©2009
Thread Cancellation
Terminating a thread before it has finished
Example: two threads are searching a database and one returns a result-
the other can be cancelled.
A thread to be cancelled is called a target thread
Two general approaches:
Asynchronous cancellation terminates the target thread
immediately
Deferred cancellation allows the target thread to periodically check
if it should be cancelled
Difficulties arise if resources have been allocated to the cancelled thread
or if it in the midst of updating data it shares with other threads.
Operating System Concepts – 8th Edition 4.24 Silberschatz, Galvin and Gagne ©2009
Signal Handling
Signals are used in UNIX systems to notify a process that a
particular event has occurred. A signal may be either synchronous
or asynchronous.
A signal handler is used to process signals
1. Signal is generated by particular event
2. Signal is delivered to a process
3. Signal is handled
Handling multithreaded signals
Options:
Deliver the signal to the thread to which the signal applies
Deliver the signal to every thread in the process
Deliver the signal to certain threads in the process
Assign a specific thread to receive all signals for the process
Operating System Concepts – 8th Edition 4.25 Silberschatz, Galvin and Gagne ©2009
Thread Pools
Create a number of threads in a pool where they await work
Advantages:
Usually slightly faster to service a request with an existing thread
than create a new thread
Allows the number of threads in the application(s) to be bound to
the size of the pool
Operating System Concepts – 8th Edition 4.26 Silberschatz, Galvin and Gagne ©2009
Thread Specific Data
Allows each thread to have its own copy of data
Useful when you do not have control over the thread creation
process (i.e., when using a thread pool)
Operating System Concepts – 8th Edition 4.27 Silberschatz, Galvin and Gagne ©2009
Scheduler Activations
Both M:M and Two-level models require communication to maintain
the appropriate number of kernel threads allocated to the application
Scheduler activations provide upcalls - a communication mechanism
from the kernel to the thread library
This communication allows an application to maintain the correct
number kernel threads
Operating System Concepts – 8th Edition 4.28 Silberschatz, Galvin and Gagne ©2009
An example of thread!
int main()
{
pthread_t tid;
pthread_create(&tid, NULL, thread, NULL);
exit(0);
}
// thread routine
void *thread(void *vargp)
{
printf("Hello, world!\n");
Operating System Concepts – 8th Edition 4.29 Silberschatz, Galvin and Gagne ©2009