Title: Chapter 5: Threads
1Chapter 5 Threads
- Overview
- Multithreading Models
- User-level and Kernel-level Threads
- Threading Issues
- Pthreads
- Solaris 2 Threads
- Linux Threads
- Java Threads (Language-level Threads)
Modified and synthesized from the Stallingss and
textbooks slides
Dr. Kocan
2Multithreading
- Operating system supports multiple threads of
execution within a single process - MS-DOS supports a single thread
- UNIX supports multiple user processes but only
supports one thread per process - Windows 2000, Solaris, Linux, Mach, and OS/2
support multiple threads
3Threads within a Process
4Processes (Threads) in Multithreaded Environment
- Process defined as the unit of resource
allocation and a unit of protection - Associated with the processes
- Have a virtual address space which holds the
process image - Protected access to processors, other processes
(for IP), files, and I/O resources (devices and
channels) - Threads in a process
- An execution state (running, ready, etc.)
- Saved thread context when not running
- Has an execution stack
- Some per-thread static storage for local
variables - Access to the memory and resources of its process
- all threads of a process share this
5Single and Multithreaded Processes
6TCB Register values, priority,etc
7More on threads
- All of the threads share the state and resources
of that process. - They reside in the same address space and have
access to the same data. - One alters the data, others see
- One thread opens a file for reading, the others
can also read from that file.
8Benefits of Threads
- Responsiveness
- Allowing partial blocking of a program
- Utilization of MP Architectures
- Each thread may run on different processors
concurrently - Takes less time to create a new thread than a
process - Less time to terminate a thread than a process
- Less time to switch between two threads within
the same process - Since threads within the same process share
memory and files, they can communicate with each
other without invoking the kernel
9Example Uses of Threads in a Single-User
Multiprocessing System
- Foreground to background work
- One thread to display menus and read user input.
- Another thread to execute user commands and
updates the spreadsheet - Increase the perceived speed
- Asynchronous processing
- Implement asynchronous elements in a program as
threads - Eg. Periodic backup, and that schedules itself
directly with the OS (against power failure) - Speed execution
- Overleap reading from I/O with computation
- Modular program structure
- Program variety of activities, sources and
destinations of input and output ? multiple
threads
10Threads
- Suspending a process involves suspending all
threads of the process since all threads share
the same address space - Termination of a process, terminates all threads
within the process
11Thread States
- States associated with a change in thread state
- Spawn
- Spawn another thread
- New thread goes to READY queue.
- Block
- Wait for an event
- Save its user registers, PC, SPs
- Unblock
- Event occurs, move thread to READY queue.
- Finish
- Deallocate register context and stacks
- QUESTION? Blocking of a thread results in the
blocking of the entire process (ie. Other threads
also are blocked?) - Assume do not block !!
- (User Level vs. Kernel Level Threads!)
12Remote Procedure Call Using Single Thread
13Remote Procedure Call Using Multiple Threads
14User Threads
- Thread management done by user-level threads
library - Like math library (-lm) during linking
- Create, destroy, message/data passing between
threads, saving/restoring thread context - The kernel is not aware of the existence of
threads - Week scheduling among threads
- Examples
- - POSIX Pthreads, - Mach C-threads, - Solaris
threads
15Illustration of User-level Threads
- An application begins with a single thread
- The application and its thread are allocated to a
single process managed by the kernel - The application is running (the process in the
running state) - Application spawn new threads (spawn procedure
call!) - Thread library
- Saves the context of the current thread
- Create a data structure for the new thread
- Pass control to one of the ready threads within
the process - Restore context of the thread
- Thread context user registers, PC, SPs
ALL ACTIVITIES IN THE USER SPACE and WITHIN ONE
PROCESS
16Relationship between Thread and Process
Scheduling (User-level)
- Process B is executing its thread 2
T1
T2
ready
running
ready
running
blocked
blocked
Process B
ready
running
blocked
17Possible occurrence - 1
T2 makes I/O system call to kernel the kernel
in turn blocks process B Kernel switches to
another PROCESS
T1
T2
ready
running
ready
running
blocked
blocked
Process B
ready
running
blocked
T2 is still running (not executed on the
processor!)
18Possible occurrence - 2
A clock interrupt passes control to kernel
process exhausted its time slice Kernel switches
to another PROCESS
T1
T2
ready
running
ready
running
blocked
blocked
T2 is still running (not executed on the
processor!)
Process B
ready
running
blocked
Execution resumes in T2 after kernel switches to B
19Possible occurrence - 3
T2 needs an action by T1 of process B Thread
library switches to another thread
T1
T2
ready
running
ready
running
blocked
blocked
Process B
ready
running
blocked
20Important Notes about User-level Threads
- A process in the midst of a thread switch from
one thread to another when interrupted - When that process is resumed, execution continues
from within the threads library - Complete thread switch, transfer control to new
thread within the process
21Advantages and Disadvantages of ULTs
- Thread switching does not require kernel
invocation - No switch to kernel mode
- Overhead of mode switches
- Scheduling can be application specific
- One application is round-robin, another is
priority-based - ULTs can run on any OS.
- No changes to underlying kernel
- ULT executes a blocking system call, all threads
in the process are blocked - ULT cannot take the advantage of multiprocessing
- OS assigns one process to one processor
- Impossible to assign the several threads of the
process onto multiple processors
22Kernel Threads
- Supported and managed by the Kernel
- No thread management code in the application area
- An API to the kernel thread facility
- Kernel maintains context information for the
process and the threads - Scheduling is done on a thread basis
- Many threads of a process onto many processors
- All threads within a process ? one process
- One thread is blocked, kernel schedules another
thread of the same process - Kernel routines themselves can be multithreaded
- Examples - Windows 95/98/NT/2000, Solaris,
Tru64 UNIX - - BeOS,- Linux,OS/2
- Disadvantage the transfer of control from one
thread to another within the same process
requires mode switch
23Combined User-level Kernel-level Threads
- Example is Solaris
- Thread creation done in the user space
- Bulk of scheduling and synchronization of threads
done in the user space - The multiple ULTs from a single application are
mapped onto some (smaller or equal) number of
KLTs. - The programmer can adjust the number of KLTs
- Advantages of ULTs KLTs
- Remove/minimize these disadvantages of ULTs and
KLTs - Multiple threads in an application can run in
parallel on Multiple-processors - Not all threads are blocked in process in case of
a blocking system call from one thread
24User-level, Kernel-level and Combined
25Relationship Between Threads and Processes
(Multithreading Models)
ThreadsProcess
Description
Example Systems
Traditional UNIX implementations
11
Each thread of execution is a unique process with
its own address space and resources.
M1
A process defines an address space and dynamic
resource ownership. Multiple threads may be
created and executed within that process.
Windows NT, Solaris, OS/2, OS/390, MACH
A thread may migrate from one process environment
to another. This allows a thread to be easily
moved among distinct systems.
Ra (Clouds), Emerald
1M
TRIX
MN
Combines attributes of M1 and 1N cases
26Many-to-One Model
Multiple threads to one process
27One-to-One
- Each user-level thread maps to kernel thread.
- Examples
- - Windows 95/98/NT/2000
- - OS/2
28One-to-many
- Distributed OS. (eg. Clouds OS, Ra kernel,
Emarald System) - Move threads between address spaces
- From one computer to another
- Carry with it certain info such as the
controlling terminal, global parameters,
scheduling guidance (i.e. priority) - Access to remote resource or load balancing
29Many-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 2
- Windows NT/2000 with the ThreadFiber package
30Many-to-many Relationships (in TRIX)
- Domain and thread
- Domain a static entity address space ports
- Ports messages sent/received
- Thread execution stack, processor state,
scheduling info. - Threads move from one domain to another
31Example for many-to-many
Program
I/O subprogram
Main program may wait for I/O subprogram or
continue
- Treat the main program and I/O program as single
activity ? single thread - One address space (domain) for the main program
one for the I/O subprogram - Move the thread from one address space to another
as execution continues - OS manage the address space independently, and no
process creation overhead - The address space could be used by other I/O
programs
32Threading Issues fork() and exec()
- Semantics of fork() and exec() system calls.
- Thread fork()
- Duplicate the process (i.e. all threads)
- Or the new process is single-threaded
- UNIX two different forks
- Thread exec()
- The program replace the entire process all
threads and LWPs - Fork()?exec() single-threaded fork
- Fork() ? No exec() ?duplicate all threads (i.e.
The process) - Signal handling
- Thread pools
- Thread specific data
33Thread issues thread cancellation
- Thread cancellation (cancelling the target
thread). - Multiple threads for searching a database
- One finds,the others are cancelled
- Asynchronous cancellation one thread immediately
terminates the target thread - Deferred cancellation the target thread can
periodically check if it should terminate - Difficulties in Asynchronous cancellation
- Resources have been allocated to a cancelled
thread - Thread cancelled in the middle of updating shared
data - The OS reclaims the system resources from a
cancelled thread (but the OS will not reclaim ALL
RESOURCES) - Cancellation points in deferred cancellation (in
Pthreads)
34Thread issues - Signal handling
- Signal in UNIX notify a process that a
particular event has occurred - Receive types
- Synchronous (internal to the process)
- Asynchronous (external to the process) eg.
ltControlgt ltCgt,timer expire - Signal patterns
- The occurrence of a particular event triggers a
signal - The generated signal delivered to the process
- The process must handle the signal
- Example illegal memory access or division by
zero (sync. Signal) - The process generated that signal also the
receiver
35Thread Issues - Signal handling
- A default signal handler
- A user-defined signal handler
- Every signal has a default signal handler that is
run by the kernel - The default signal handler may be overridden by a
user-defined signal handler
36Thread issues Signal handling in Multithreaded
programs
- Options
- Deliver the signal to the thread to which the
signal applies - Synchronous signals to the thread that generated
the signal - Deliver the signal every thread in the process
- Eg. ltcontrol Cgt asynchronous signal
- Deliver the signal to certain threads in the
process - Assign a specific thread to receive all signals
for the process (eg. Solaris) - Some UNIX
- A thread can specify which signals it will accept
and which it will block. - Deliver a signal only the first thread which
accepts it.
37Thread issues Thread Pools
- Thread creation per service request
- Overhead of creation
- Unlimiting the number of threads ? exhausting the
system resources - Solution Use Thread Pool
- Create a number of Threads during process
creation - They wait for work
- No free thread in the pool, the service has to
wait
Thread issues Thread-Specific Data
Unique thread identifier
38Pthreads (User-level Thread Library)
- 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.
39PThread Example
- Void runner(void param)
-
-
- sum .
- pthread_exit(0)
- include ltpthread.hgt
- include ltstdio.hgt
- Int sum / shared by the threads /
- Void runner(void param)
- Main(int argc, char argv)
-
- pthread_t tid / thread id /
- pthread_attr attr / the set of thread
attributes sa stack size, scheduling - .
- pthread_attr_init(attr) / get the default
attributes / - pthread_create(tid,attr, runner, argv1)
- / now wait for the thread to exit /
- pthread_join(tid,NULL)
- printf(sum d\n,sum)
-
-
40Solaris 2 Threads
- Process includes the users address space, stack,
and process control block - User-level threads
- Bound (permanently attached to an LWP) or unbound
- Lightweight processes
- At least ONE LWP per process
- Mapping between ULTs and KLTs
- Each LWP supports one or more ULTs and maps to
ONE kernel thread - The threads library multiplexes ULTs on the pool
of LWPs of the process - ULTs connected to LWPs accompish work, others
wait/block - LWPs scheduled independently by the kernel and
may execute in parallel on MPs. - Kernel threads
- The fundamental entities that can be scheduled
and dispatched to run on one of the processors
41Solaris 2many-to-many model
Kernel thread not bounded to a LWP
42Motivation for Combined
- Logical concurrency ? no hardware parallelism
needed - A set of windows, only one is active at a time
- A set of ULTs on a single LWP
- Multiple threads some may block (eg. I/O)
- Multiple LWPs (lt ULTs)
- One ULT per LWP
- A parallel array computation
- One row of array per thread
- A mixture of bound and unbound threads
- A real-time application some threads system-wide
priority and real-time scheduling others
perform background functions and can share one or
a small pool of LWPs
43Process Structure in Solaris
Signal dispatch table kernel to use to send
signals to the process
File description the files in use by the process
Memory map Address space of the process
Which signals will be accepted
Pointers to KLT and process structure
44Unbound threads states
User-level library manages
Active assigned to a LWP
Bound threads bound ULT sleeps, its LWP stops
45Solaris Thread ExecutionWhy unbound T1 leaves
active state
- Synchronization
- invocation of one of the synchronization
primitive (conditions, semaphores etc). - Goto sleep state condition is met? go to
runnable state - Suspension
- Any thread (incl. T1) may cause T1 to be
suspended and placed in a stopped state - Another thread issues continue
- Preemption
- Another thread (T2) of higher-priority to become
runnable - Yielding
- T1 executes the thr_yield() library command
- The threads scheduler will look to see another
runnable thread (T2) of the same priority - T1?runnable state T2? active state
46Interrupts as Threads in Solaris
- Interrupts are converted to kernel threads (to
reduce the overhead) - Solaris employs a set of kernel threads to
handle interrupts - Int. thread has its own ID, priority, context,
and stack - The kernel controls access to data structures
and synchronizes among interrupt threads using
mutual exclusion primitives - Interrupt threads are assigned higher priorities
than all other types of kernel threads
47Interrupt Handling in Solaris
- Interrupt delivered to a particular processor
- The thread on that processor is pinned
- Cannot move to another processor and context is
preserved (suspended until interrupt handled) - The processor begins executing an interrupt
thread - The thread is selected from a pool of
deactivated int. threads - The interrupt thread handles the interrupt
- The handler needs to wait for locked data (by
another thread) - The interrupt thread can be preempted by another
interrupt thread of higher priority.
48Windows 2000 Threads -SKIP
- Implements the one-to-one mapping.
- Each thread contains
- - a thread id
- - register set
- - separate user and kernel stacks
- - private data storage area
49Linux task table
Task table
P1
Task_struct
P2
P3
Task_struct
P2 is a clone of P1
P3 is fork of P1
50Linux Process task_struct
- State executing, ready, suspended, stopped,
zombie - Scheduling information normal or real-time, a
priority - Identifiers unique process ID, user and group
IDs - Interprocess communication
- Links link to parent links to siblings links
to its children - Times and timers process creation time, the
amount of processor time consumed - File system pointers to opened files (by this
process) - Virtual memory defines the virtual memory
assiged to this process - Processor-specific context registers and stack
Some fields are pointers, e.g. File system
51Linux Process States
- Running
- Interruptable (blocked)
- Uninterruptable
- Blocked for hardware conditions wont accept any
signal - Stopped
- Halted can be resumed by another process
- Zombie
- Terminated but must have its task structure in
the process table
52Linux Threads
- Linux refers to them as tasks rather than
threads. - Fork() duplicating a process
- Thread creation is done through clone() system
call. - Clone() allows a child task to share the address
space of the parent task (process) - Parameters to specify the degree of sharing
- Clone() w/o flag set fork()
- TWO processes share the same virtual memory
- Functions as Threads within a single process
53Linux Threads clone() vs. fork()
Fork()
Virtual Memory
Clone()
P1
P1
P1,P2
Copy task_struct fields
P2
54Java Threads
- LANGUAGE-LEVEL THREAD SUPPORT
- Creation and management of threads (i.e.managed
by JVM) - Java threads may be created by
- Extending Thread class
- Override run() method of Thread class
- Implementing the Runnable interface
- Java threads are managed by the JVM.
55Example Extending Thread class
- Class Summation extends Thread
-
- public Summation(int n)
- uppern
-
- public void run()
- int sum 0
- .
-
-
-
- Public class ThreadTester
-
- public static void main()
-
- Summation thrd new Summation()
- thrd.start()
-
-
2nd thread
Creates the thread
- Allocate memory and initialize a new thread in
JVM - Call run() method
56Java Thread States
57The JVM and the Host OS
- The specification of JVM does not indicate how
Java threads are to be mapped to the underlying
OS - Leaving decision to a particular implementation
- Winows95/98/NT/2000 one-to-one model
- One java thread ? one kernel thread
- Solaris 2
- Initially many-to-one model
- Solaris 2.6 many-to-many model