Professional Documents
Culture Documents
Threads
• Overview
• Multithreading Models
• Threading Issues
• Thread libraries
Complex programs
• Matrix multiplication.
• I/O of calculated values and continuing calculation.
• Receiving stream of data from a Network and
processing it.
Complex programs
• 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
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
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
• Responsiveness
• Resource Sharing
• Economy
• Utilization of MP Architectures
Benefits
• Many-to-One
• One-to-One
• Many-to-Many
Many-to-One
spaces:
# of addr
One Many
# threads
Per AS:
CS677: Distributed
User-level Threads
CS677: Distributed
Kernel-level threads
CS677: Distributed
Light-weight Processes
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