Week 4a
Week 4a
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
Operating System Concepts – 8th Edition 4.2 Silberschatz, Galvin and Gagne ©2009
Motivation
Modern OS allow a process to have multiple threads of control
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
l eitiorns
Share
E d
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
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
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
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)
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
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
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;
// thread routine
void *thread(void *vargp)
{
printf("Hello, world!\n");
Operating System Concepts – 8th Edition 4.29 Silberschatz, Galvin and Gagne ©2009