Você está na página 1de 31

Threads

Fred Kuhns
fredk@cse.wustl.edu
Applied Research Laboratory,
Department of Computer Science and Engineering,
Washington University in St. Louis

Washington
WASHINGTON UNIVERSITY IN ST LOUIS
Introduction to Threads
Multithreaded Process Model
Single-Threaded
Thread Thread Thread
Process Model
Thread Thread Thread
Control Control Control
Process User Block Block Block
Control Stack
Block
Process User User User
Control Stack Stack Stack
User Kernel Block
Address Stack
Space User Kernel Kernel Kernel
Address Stack Stack Stack
Space

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 2


Processes and Threads
• Process model
– Unit of dispatching: process is an execution path
through one or more programs set of threads
(computational entities)
• execution may be interleaved with other processes
– Unit of resource ownership: process is allocated a
virtual address space to hold the process image

• What if we separate notion of execution from


Process abstraction? Answer: we get threads.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 3


Threads
• Dynamic object representing an execution path and
computational state.
– threads have their own computational state: PC, stack, user
registers and private data
– Remaining resources are shared amongst threads in a process

• Effectiveness of parallel computing depends on the performance of


the primitives used to express and control parallelism

• Useful for expressing the intrinsic concurrency of a program


regardless of resulting performance

• We will discuss three examples of threading:


– User threads,
– Kernel threads and
– Scheduler Activations

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 4


Common Thread Models
• User level threads - user libraries implementation
– Benefits: no kernel modifications, flexible and low cost
– Drawbacks: thread may block entire process, no parallelism
• Kernel level threads - kernel directly supports multiple threads of
control in a process.
– Benefits: scheduling/synchronization coordination, less overhead
than process, suitable for parallel application
– Drawbacks: more expensive than user-level threads, generality
leads to greater overhead
• Light-Weight Processes (LWP) Kernel supported user thread
– LWP bound to kernel thread: a kernel thread may not be bound to an
LWP
– LWP is scheduled by kernel
– User threads scheduled by library onto LWPs
– Multiple LWPs per process

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 5


Thread States
• Primary states:
– Running, Ready and Blocked.

• Operations to change state:


– Spawn: new thread provided register context and
stack pointer.
– Block: event wait, save user registers, PC and stack
pointer
– Unblock: moved to ready state
– Finish: deallocate register context and stacks.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 6


Threading Models

P P P P P P

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

• User threads := Many-to-One


• kernel threads := One-to-One
• Mixed user and kernel := Many-to-Many

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 7


User Level Threads

• User level threads - supported by user level


threads libraries
– Examples
• POSIX Pthreads, Mach C-threads, Solaris threads
– Benefits:
• no modifications required to kernel
• flexible and low cost
– Drawbacks:
• can not block without blocking entire process
• no parallelism (not recognized by kernel)

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 8


Kernel Level Threads
• Kernel level threads - directly supported by
kernel, thread is the basic scheduling entity
– Examples:
• Windows 95/98/NT/2000, Solaris, Tru64 UNIX, BeOS, Linux
– Benefits:
• coordination between scheduling and synchronization
• less overhead than a process
• suitable for parallel application
– Drawbacks:
• more expensive than user-level threads
• generality leads to greater overhead

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 9


Threading Issues
• fork and exec
– should fork duplicate one, some or all threads
• Cancellation – cancel the target thread, issues with freeing resources and
inconsistent state
– asynchronous cancellation – target is immediately canceled
– deferred cancellation – target checks periodically. check at cancellation points
• Signals: generation, posting and delivery
– Every signal handled by a default or user-defined handler
• Signal delivery:
– to thread for which it may apply
– to every thread in process
– to certain threads
– specifically designated thread (signal thread)
• synchronous signals should go to thread causing the signal
• what about asynchronous signals?
– Solaris: deliver to a special thread which forward to first user created thread
that has not blocked the signal.
• Bounding the number of threads created in a dynamic environment
– use thread pools
• Al threads share some address space:
– use of thread specific data
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 10
First Class threads (Psyche OS)
• Thread operations in user space:
– create, destroy, synch, context switch
• kernel threads implement a virtual processor
• Course grain in kernel - preemptive scheduling
• Communication between kernel and threads
library
– shared data structures.
– Software interrupts (user upcalls or signals). Example,
for scheduling decisions and preemption warnings.
– Kernel scheduler interface - allows dissimilar thread
packages to coordinate.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 11


Scheduler Activations

• Attempt to combine benefits of both user and


kernel threading support
– blocking system call should not block whole process
– user space library should make scheduling decisions
• efficiency by avoiding unnecessary user, kernel
mode switches.
• Kernel assigns a set of virtual processors to
each process. User library then schedules
threads on these virtual processors.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 12


Scheduler Activations
• An activation:
– serves as execution context for running thread
– notifies thread of kernel events (upcall)
– space for kernel to save processor context of current user thread when
stopped by kernel
– Library schedules user threads on activations.
– space for kernel to save processor context of current user thread when
stopped by kernel
– upall performed when one of the following occurs:
• user thread performs blocking system call
• blocked thread belonging to process, then its library is notified
allowing it to either schedule a new thread or resume the preempted
thread.
• kernel is responsible for processor allocation => preemption by
kernel.
• Thread package responsible for scheduling threads on available
processors (activations)

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 13


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.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 14


UNIX Support for Threading
• BSD:
– pthreads and similar user space implementations
– process model only. 4.4 BSD enhancements.
– BSD based OSes are adding support for threads
• Solaris
– user threads, kernel threads, LWPs and in 2.6
Scheduler Activations
• Mach
– kernel threads and tasks. Thread libraries provide
semantics of user threads, LWPs and kernel threads.
• Digital UNIX - extends MACH to provide usual
UNIX semantics: Pthreads library.
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 15
Solaris Threads
• Supports:
– user threads (uthreads) via libthread and
libpthread
– LWPs, abstraction that acts as a virtual CPU
for user threads.
• LWP is bound to a kthread.
– kernel threads (kthread), every LWP is
associated with one kthread, however a
kthread may not have an LWP
• interrupts as threads

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 16


Solaris kthreads
• Fundamental scheduling/dispatching object
• all kthreads share same virtual address
space (the kernels) - cheap context switch
• System threads - example STREAMS,
callout
• kthread_t, /usr/include/sys/thread.h
– scheduling info, pointers for scheduler or sleep
queues, pointer to klwp_t and proc_t

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 17


Solaris LWP
• Kernel provided mechanism to allow for both user
and kernel thread implementation on one platform.
• Bound to a kthread
• LWP data (see /usr/include/sys/klwp.h)
– user-level registers, system call params, resource
usage, pointer to kthread_t and proc_t
• All LWPs in a process share:
– signal handlers
• Each may have its own
– signal mask
– alternate stack for signal handling
• No global name space for LWPs
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 18
Solaris User Threads
• Implemented in user libraries
• library provides synchronization and scheduling
facilities
• threads may be bound to LWPs
• unbound threads compete for available LWPs
• Manage thread specific info
– thread id, saved register state, user stack, signal
mask, priority*, thread local storage
• Solaris provides two libraries: libthread and
libpthread.
• Try man thread or man pthreads
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 19
Solaris Thread Data Structures

proc_t
p_tlist
kthread_t
t_procp
t_lwp
klwp_t t_forw
lwp_thread
lwp_procp

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 20


Solaris Threading Model (Combined)

Process 1 Process 2

user
L L L L Int kthr

kernel ......
...

P P P
hardware
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 21
Solaris User Level Threads

Stop
Runnable Wakeup

Continue

Stop
Stopped Sleeping

Preempt Dispatch

Stop Active Sleep

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 22


Solaris Lightweight Processes

Timeslice Running Stop


or Preempt
Dispatch Wakeup

Blocking
Runnable System Stopped
Call
Continue
Wakeup
Stop
Blocked

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 23


Solaris Interrupts

• One system wide clock kthread


• pool of 9 partially initialized kthreads per
CPU for interrupts
• interrupt thread can block
• interrupted thread is pinned to the CPU

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 24


Solaris Signals and Fork

• Divided into Traps (synchronous) and


interrupts (asynchronous)
• each thread has its own signal mask, global
set of signal handlers
• Each LWP can specify alternate stack
• fork replicates all LWPs
• fork1 only the invoking LWP/thread

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 25


Mach
• Two abstractions:
– Task - static object, address space and system resources called port
rights.
– Thread - fundamental execution unit and runs in context of a task.
• Zero or more threads per task,
• kernel schedulable
• kernel stack
• computational state
• Processor sets - available processors divided into non-intersecting
sets.
– permits dedicating processor sets tasks
• Mach C-Threads:
– Coroutine-based - multiple user threads onto a single-threaded task
– Thread-based - one-to-one mapping from c-threads to Mach threads.
Default.
– Task-based - One Mach Task per c-thread.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 26


Continuations
• Address problem of excessive kernel stack memory
requirements
• process model versus interrupt model
– one per process kernel stack versus a per thread kernel stack
• Thread is first responsible for saving any required
state (the thread structure allows up to 28 bytes)
• indicate a function to be invoked when unblocked (the
continuation function)
• Advantage: stack can be transferred between
threads eliminating copy overhead.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 27


Digital UNIX
• Based on Mach 2.5 kernel
• Provides complete UNIX programmers interface
• 4.3BSD code and ULTRIX code ported to Mach
– u-area replaced by utask and uthread
– proc structure retained
• Threads:
– Signals divided into synchronous and asynchronous
– global signal mask
– each thread can define its own handlers for synchronous
signals
– global handlers for asynchronous signals

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 28


Windows 2000 Threads
• Implements the one-to-one mapping.
• Each thread contains
- a thread id
- register set
- separate user and kernel stacks
- private data storage area

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 29


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)

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 30


4.4 BSD UNIX

• Initial support for threads implemented but


not enabled in distribution
• Proc structure and u-area reorganized
• All threads have a unique ID
• How are the proc and u areas reorganized to
support threads?

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 31

Você também pode gostar