You are on page 1of 47

Threads

Threads

• Overview
• Multithreading Models
• Threading Issues
• Thread libraries
Complex programs

The basic idea is that the several components in any


complex system will perform particular subfunctions
that contribute to the overall function.

What if they are not Sequential?

• Matrix multiplication.
• I/O of calculated values and continuing calculation.
• Receiving stream of data from a Network and
processing it.
Complex programs

The basic idea is that the several components in any


complex system will perform particular subfunctions
that contribute to the overall function.

Non Sequential execution

• Speed of execution
(Matrix multiplication.)
• Foreground and background work
(I/O of calculated values and continuing calculation)
• Asynchronous processing
(Receiving stream of data from a Network and
processing it)
Single and Multithreaded Processes

Threads encapsulate concurrency: “Active” component


Address spaces encapsulate protection: “Passive” part
Keeps buggy program from trashing the system
Why have multiple threads per address space?
Execution Stack Example
A: tmp=1
A(int tmp) {
ret=exit
if (tmp<2)
B: ret=A+2
B();
C: ret=b+1
printf(tmp);
A: tmp=2
}
Stack ret=C+1
B() { Pointer
C(); Stack Growth
}
C() {
• Stack holds temporary results
A(2);
• Permits recursive execution
• Crucial to modern languages
}
A(1);
“Lightweight” Process with Threads
• Thread: a sequential execution stream within process
(Sometimes called a “Lightweight process”)
 Process still contains a single Address Space
 No protection between threads
• Multithreading: a single program made up of a number of
different concurrent activities
 Sometimes called multitasking, as in Ada…
• Why separate the concept of a thread from that of a
process?
 “thread” part of a process (concurrency)
 Separate from the “address space” (Protection)
 Heavyweight Process  Process with one thread
Why Threads?

• Software engineering perspective


• Applications are easier to structure as a
collection of threads
• Each thread performs several [mostly
independent] tasks

CS677: Distributed
Examples of multithreaded programs
• Embedded systems
 Elevators, Planes, Medical systems, Wristwatches
 Single Program, concurrent operations
• Most modern OS kernels
 Internally concurrent because have to deal with
concurrent requests by multiple users
 But no protection needed within kernel
• Database Servers
 Access to shared data by many concurrent users
 Also background utility processing must be done
• Network Servers
 Concurrent requests from network
 File server, Web server, and airline reservation systems
Multi-threaded Clients Example : Web Browsers
• Browsers are multi-threaded
• Such browsers can display data before entire
document is downloaded: performs multiple
simultaneous tasks
• Fetch main HTML page, activate separate
threads for other parts
• Each thread sets up a separate connection with
the server
• Uses blocking calls
• Each part (gif image) fetched separately and in
parallel
• Advantage: connections can be setup to
different sources
• Ad server, image server, web server…
CS677: Distributed
Multi-threaded Server Example

• Apache web server: pool of pre-spawned worker threads


• Dispatcher thread waits for requests
• For each request, choose an idle worker thread
• Worker thread uses blocking system calls to service web
request

CS677: Distributed
Thread State
• State shared by all threads in process/addr space
• Contents of memory (global variables, heap)
• I/O state (file system, network connections, etc)
• State “private” to each thread
• Kept in TCB  Thread Control Block
• CPU registers (including, program counter)
• Execution stack – what is this?

• Execution Stack
• Parameters, Temporary variables
• return PCs are kept while called procedures are
executing
Each Thread has

• an execution state (Running, Ready, etc.)


• saved thread context when not running (TCB)
• an execution stack
• some per-thread static storage for local variables
• access to the shared memory and resources of
its process (all threads of a process share this)
Benefits

• Responsiveness

• Resource Sharing

• Economy

• Utilization of MP Architectures
Benefits

• Takes less time to create a new thread


than a process

• Less time to terminate a thread than a


process

• Switching between two threads takes less


time than switching between processes

• Threads enhance efficiency in


communication between programs
User Threads

• Thread management done by user-level threads


library

• Three primary thread libraries:


 POSIX Pthreads
 Win32 threads
 Java threads
Kernel Threads
• Supported by the Kernel
• Examples
Windows XP/2000
Solaris
Linux
Tru64 UNIX
Mac OS X
Multithreading Models

• Many-to-One
• One-to-One
• Many-to-Many
Many-to-One

• Many user-level threads mapped to single


kernel thread
• Examples:
Solaris Green Threads
GNU Portable Threads
Many-to-One Model
One-to-One

• Each user-level thread maps to kernel thread


• Examples
 Windows NT/XP/2000
 Linux
 Solaris 9 and later
One-to-one Model
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
Many-to-Many Model
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
Classification

spaces:
# of addr
One Many
# threads
Per AS:

One MS/DOS, early Macintosh Traditional UNIX

Embedded systems Mach, OS/2, Linux


(Geoworks, VxWorks, Windows 9x???
Many JavaOS,etc)
Win NT to XP, Solaris, HP-
JavaOS, Pilot(PC) UX, OS X

• Real operating systems have either


• One or many address spaces
• One or many threads per address space
User-level versus kernel threads

• Cost of thread management


• More efficient in user space
• Ease of scheduling
• Flexibility: many parallel programming models and
schedulers
• Process blocking – a potential problem

CS677: Distributed
User-level Threads

• Threads managed by a threads library


• Kernel is unaware of presence of threads
• Advantages:
• No kernel modifications needed to support threads
• Efficient: creation/deletion/switches don’t need system
calls
• Flexibility in scheduling: library can use different
scheduling algorithms, can be application dependent
• Disadvantages
• Need to avoid blocking system calls [all threads block]
• Threads compete for one another
• Does not take advantage of multiprocessors [no real
parallelism]
CS677: Distributed
User-level threads

CS677: Distributed
Kernel-level threads

• Kernel aware of the presence of threads


• Better scheduling decisions, more expensive
• Better for multiprocessors, more overheads for
uniprocessors

CS677: Distributed
Light-weight Processes

• Several LWPs per heavy-weight process


• User-level threads package
• Create/destroy threads and synchronization primitives
• Multithreaded applications – create multiple threads, assign
threads to LWPs (one-one, many-one, many-many)
• Each LWP, when scheduled, searches for a runnable thread
[two-level scheduling]
• Shared thread table: no kernel support needed
• When a LWP thread block on system call, switch to kernel
mode and OS context switches to another LWP

CS677: Distributed
LWP Example

CS677: Distributed
Threading Issues
• We are talking about threads for user processes.
Both ULT & KLT execute in user mode.
• It is necessary to synchronize the activities of the
various threads
• all threads of a process share the same
address space and other resources
• any alteration of a resource by one thread
affects the other threads in the same
process
• In a typical OS many system calls are blocking
 as a result, when a ULT executes a system
call, not only is that thread blocked, but all of
the threads within the process are blocked
Threading Issues

• Semantics of fork() and exec() system calls


• Thread cancellation
• Signal handling
• Thread pools
• Thread specific data
• Scheduler activations
Semantics of fork() and exec()

• Does fork() duplicate only the calling thread or all


threads?
Thread Cancellation

• Terminating a thread before it has finished


• Two general approaches:
 Asynchronous cancellation terminates
the target thread immediately
 Deferred cancellation allows the target
thread to periodically check if it should be
cancelled
Signal Handling
• Signals are used in UNIX systems to notify a
process that a particular event has occurred
• A signal handler is used to process signals
 Signal is generated by particular event
 Signal is delivered to a process
 Signal is handled
• 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
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
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)
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
Thread Libraries

• Thread libraries provide programmers


with an API for creating and managing
threads.
• Thread libraries may be implemented
either in user space or in kernel space.
 The former involves API functions
implemented solely within user space,
with no kernel support.
 The latter involves system calls, and
requires a kernel with thread library
support.
Pthreads

• A POSIX standard (IEEE 1003.1c) API for thread


creation and synchronization
• API specifies behavior of the thread library,
implementation is up to development of the library
• Common in UNIX operating systems (Solaris, Linux,
Mac OS X)
Windows XP Threads
• Implements the one-to-one mapping
• Each thread contains
 A thread id
 Register set
 Separate user and kernel stacks
 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)
Linux Threads

• Linux refers to them as tasks rather than


threads
• Thread creation is done through clone()
system call
• clone() allows a child task to share the
address space of the parent task
(process)
Java Threads

• Java threads are managed by the JVM

• Java threads may be created by:

 Extending Thread class


 Implementing the Runnable interface
Java Thread States

You might also like