OS 04 Threads
OS 04 Threads
A Biswas
Threads
Threads
Threads
Process
Threads
Stack pointer
Registers
Notes:
1. Thread context is much smaller. Hence thread
context switch is much faster than the process
context switch.
3. Each peer can read and write the same shared data.
Thread control
9:The main thread creates a new peer thread
Example hello world: by calling the pthread_create function
If the main thread calls pthread exit, it waits for all other
peer threads to terminate, and then terminates the main
thread and the entire process with a return value of thread
return.
Reaping threads
pthread_join
blocks until thread tid terminates,
assigns the (void *) pointer returned by the
thread routine to the location pointed to by thread
return, and then
reaps any memory resources held by the
terminated thread.
Detaching threads
At any point in time, a thread is joinable or detached.
The pthread detach function detaches the joinable thread tid. Threads can
detach themselves by calling pthread detach with an argument of pthread
self().
Detaching threads
For example,
a high-performance Web server
create a new peer thread each time it receives a
connection request from a Web browser.
each connection is handled independently by a
separate thread, it is unnecessary and
indeed undesirable for the server to explicitly
wait for each peer thread to terminate.
– Performance
• Scales better to multiprocessor systems
– Cooperation
• Shared address space incurs less overhead than IPC
Motivation for Threads
• Each thread transitions among a series of
discrete thread states
• Thread states
– Born state
– Ready state (runnable state)
– Running state
– Dead state
– Blocked state
– Waiting state
– Sleeping state
• Sleep interval specifies for how long a thread will sleep
Thread States: Life Cycle of a Thread
– User-level threads
– Kernel-level threads
– More portable
User-level Threads
Disadvantage
–Kernel views a multithreaded
process as a single thread of
control
»Can lead to suboptimal
performance if a thread issues
I/O
»Cannot be scheduled on multiple
processors at once
User-level Threads
Entire process blocks when any
of its threads requests a
blocking I/O operation.
User-level Threads
User-level threads.
Kernel-level Threads
• Advantages:
Increased scalability,
interactivity, and
throughput
• Disadvantages:
Overhead due to context switching and reduced
portability due to OS-specific APIs
Kernel-level Threads
• Kernel-level threads are not always the
optimal solution for multithreaded
applications
Kernel-level Threads
Kernel-level threads.
Combining User- and Kernel-
level Threads
• The combination of user- and kernel-level
thread implementation
– Many-to-many thread mapping (m-to-n
thread mapping)
• Number of user and kernel threads
need not be equal
• Can reduce overhead compared to
one-to-one thread mappings by
implementing thread pooling
Combining User- and Kernel-level
Threads
• Worker threads
• Resource Sharing
• Economy
• Utilization of MP Architectures
Threading models
• Three most popular threading models
– User-level threads
– Kernel-level threads
– Combination of user- and kernel-level threads
User Threads
• Thread management done by user-level
threads library
• Examples
– Windows XP/2000
– Solaris
– Linux
– Tru64 UNIX
– Mac OS X
Multi-threading Models
• Many-to-One
• One-to-One
• Many-to-Many
Many-to-One
• Many user-level threads mapped to single kernel thread
• One thread may block the processs
• Does not reap the benefit of multiprocessors
One-to-One
• Each user-level thread maps to kernel thread
• One kernel thread per user thread
• Multiple threads run in parallel on multiprocessors
• Linux, Windows NT/XP, Solaris 9 or later
Many-to-Many
• 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
Many-to-Many
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
Two-level model
Solaris Threads
• Process includes the user’s address
space, stack, and process control block
• User-level threads
• Lightweight processes (LWP)
• Kernel threads
Solaris Threads