chapter 5: threads
DESCRIPTION
Chapter 5: Threads. Overview Multithreading Models Threading Issues Pthreads(POSIX thread) Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads. Multithreading vs. Single threading. Multithreading: OS supports multiple threads of execution within a single process - PowerPoint PPT PresentationTRANSCRIPT
Chapter five 5.1 Operating System Concepts
Chapter 5: Threads
Overview Multithreading Models Threading Issues Pthreads(POSIX thread) Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads
Chapter five 5.2 Operating System Concepts
Multithreading vs. Single threading
Multithreading: OS supports multiple threads of execution within a single process
Single threading: OS does not recognize the concept of thread
MS-DOS supports a single user process and a single thread
Traditional UNIX supports multiple user processes but only supports one thread per process
Modern OS such as Solaris, Windows, supports multiple threads
Chapter five 5.3 Operating System Concepts
Threads
Thread, sometimes called lightweight process(LWP) Traditional process,sometimes called heavyweight process(HWP) A thread is an execution path within a process ( 线程是“进程中的
一条执行路径或线索”,或“进程中的一个可调度实体” ) Has an execution state (running, ready, etc.) Saves thread context when not running Has an execution stack and some per-thread static storage for
local variables Has access to the memory address space and resources of its
process all threads of a process share this when one thread alters a (non-private) memory item, all other
threads (of the process) sees that a file opened with one thread, is available to others
Chapter five 5.4 Operating System Concepts
Single and Multithreaded Processes
Chapter five 5.5 Operating System Concepts
Benefits
Responsiveness :可以获得快速的用户响应,如在 C/S 模式下, web server 为每个用户连接运行一个线程; RPC 服务器中, RPC 服务进程会开启多个线程服务于每个 RPC request
Resource Sharing :进程是拥有资源的基本单位( CPU ,地址空间, I/O 资源),进程中的线程可以共享这些资源
Economy : 创建线程比创建进程更快,进程内的线程切换( context switch) 比进程更快, solaris 中创建线程比进程快 30倍,线程切换比进程切换快 5 倍
Utilization of SMP Architectures :可以充分利用多处理器体系结构,使得一个进程中的线程在不同的处理器上运行,提高进程执行的并行度
Chapter five 5.6 Operating System Concepts
Application benefits of threads
Consider an application that consists of several independent parts that do not need to run in sequence( 一些应用程序可以分成若干相对独立的部分)
Each part can be implemented as a thread( 每一部分由一个线程来实现 )
Whenever one thread is blocked waiting for an I/O, execution could possibly switch to another thread of the same application (instead of switching to another process)
Chapter five 5.7 Operating System Concepts
Application benefits of Threads
Since threads within the same process share memory and files, they can communicate with each other without invoking the kernel ( 线程间通信无需内核干预)
Therefore necessary to synchronize the activities of various threads so that they do not obtain inconsistent views of the data( 需要进行线程间同步 )
Chapter five 5.8 Operating System Concepts
Example of inconsistent view
3 variables: A, B, C which are shared by thread T1 and thread T2
T1 computes C = A+B T2 transfers amount X from A to B( 转帐 )
T2 must do: A = A -X and B = B+X (so that A+B is unchanged)
If T1 computes A+B after T2 has done A = A-X but before B = B+X, then T1 will not obtain the correct result for C = A + B
Chapter five 5.9 Operating System Concepts
Threads States
Three key states: running, ready, blocked They have no suspend state because all threads
within the same process share the same address space Indeed: suspending (ie: swapping) a single thread
involves suspending all threads of the same process
Termination of a process, will terminates all threads within the process
Chapter five 5.10 Operating System Concepts
User Level Threads(ULT)
The kernel is not aware of the existence of threads All thread management is done by the application by
using a user-level thread library Thread switching does not require kernel mode
privileges (no mode switch) Scheduling is application specific(but may depend on OS
support)
Examples- POSIX Pthreads- Mach C-threads- Solaris threads
Chapter five 5.11 Operating System Concepts
User Level Threads library
Contains codes for: creating and destroying threads passing messages and data between threads scheduling thread execution saving and restoring thread contexts
Chapter five 5.12 Operating System Concepts
Kernel activity for User Threads
The kernel is not aware of thread activity but it is still managing process activity
When a thread makes a system call, the whole process will be blocked
but that thread is still perceived as in the running state by the thread library
So thread states are independent of process states
Chapter five 5.13 Operating System Concepts
Advantages and inconveniences of ULT
Advantages Thread switching does
not involve the kernel: no mode switching
Scheduling can be application specific: choose the best algorithm.
ULTs can run on any OS. Only needs a thread library
Inconveniences Most system calls are
blocking and the kernel blocks processes. So all threads within the process will be blocked
The kernel can only assign processes to processors. Two threads within the same process cannot run simultaneously on two processors
Chapter five 5.14 Operating System Concepts
Kernel Level Threads(KLT)
All thread management is done by kernel No thread library but an API to the kernel thread facility Kernel maintains context information for the process and the threads Switching between threads requires the kernel Scheduling on a thread basis
Examples- Windows 95/98/NT/2000
- Solaris- Tru64 UNIX- BeOS
Chapter five 5.15 Operating System Concepts
Advantages and inconveniences of KLT
Advantages the kernel can
simultaneously schedule many threads of the same process on many processors
blocking is done on a thread level
kernel routines can be multithreaded
Inconveniences thread switching within the
same process involves the kernel. We have 2 mode switches per thread switch
this results in a significant performance slowing down
Chapter five 5.16 Operating System Concepts
Multithreading Models
Many-to-One
One-to-One
Many-to-Many
Chapter five 5.17 Operating System Concepts
Many-to-One
Many user-level threads mapped to single kernel thread. (纯用户级线程)
Usually used on systems that do not support kernel threads.
Chapter five 5.18 Operating System Concepts
Many-to-One Model
Chapter five 5.19 Operating System Concepts
One-to-One
Each user-level thread maps to kernel thread.(纯核心级线程)
Examples
- Windows 95/98/NT/2000
- OS/2
Chapter five 5.20 Operating System Concepts
One-to-one Model
Chapter five 5.21 Operating System Concepts
Many-to-Many Model
Allows many user level threads to be mapped to many kernel threads. (混合式线程)
Takes advantage of many-to-one and one-to-one models
Allows the operating system to create a sufficient number of kernel threads.
Solaris 2 Windows NT/2000 with the ThreadFiber package
Chapter five 5.22 Operating System Concepts
Many-to-Many Model
Chapter five 5.23 Operating System Concepts
Threading Issues
Semantics of fork() and exec() system calls. - exec will make the specified program to replace the
entire process - if fork in a thread is immediately followed by exec, it
will duplicate only the calling thread, otherwise, all threads in the process will be duplicated.
Thread cancellation. - terminate a thread before its completion - thread to be terminated is referred to as target thread - asynchronous:immediately terminated by another
thread. - deffered:thread check itself for cancellation and
terminate.
Chapter five 5.24 Operating System Concepts
Signal handling
- signal is used in UNIX to notify a process that a particular event has occurred.
- signal may be synchronous or asynchronous:
* synchronous: signals are delivered to the same process that caused the signal(e.g.illegal memory access, divide by zero);
* asynchronous:notified by an event external to the process.
- signal must be handled
- method of handling a signal
* ignore(some signals must not be ignored,e.g. SIGKILL);
* default signal handler
* user defined signal handler
- in a multithreaded program, signals may be sent to different threads
- windows2000 uses asynchronous procedure call(APC) to simulate signal.
Chapter five 5.25 Operating System Concepts
Thread pools
- avoid too many threads from exhausting system resources(CPU,memory,etc.)
- a pool of threads is created in advance to sit and wait for work
- faster to service a request with an existing thread than waiting to create one.
- number of threads is limited.
- example: multi-threaded server architecture in database management systems.
Thread specific data
- a thread might need to own its private data
Chapter five 5.26 Operating System Concepts
Pthreads(POSIX thread)
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.
Chapter five 5.27 Operating System Concepts
Linux refers to them as tasks rather than threads. Thread creation is done through clone() system call. Clone() allows a child task (lightweight process) to share the
resources of the parent process( address space, files, signal-handling, etc.)
Pthread implementation on linux (pthread_create) makes use of clone,hence do_fork , to create threads(lightweight processes)
Hence pthread in linux is implemented as lightweight process But it does not completely conform to POSIX thread
specifications.
e.g. thread created by pthread_create has pid different from the calling thread(process)
Kernel 2.6
NPTL ( Native Posix Thread Library ) ,by redhat
Threads on linux(LinuxThreads)
Chapter five 5.28 Operating System Concepts
#include <pthread.h>#include <stdio.h>int sum;void *runner(void *param);main(int argc, char *argv[]){ ptread_t id; pthread_attr_t attr; pthread_attr_init(&attr); // 初始化线程属性为缺省属性 pthread_create(&tid,&attr,runner,argv[1]); // 创建线程 pthread_join(tid,NULL); // 等待线程 tid 结束 printf(“sum=%d\n”,sum);}
void *runner(void *param){ int upper=atoi(param); int i; sum = 0; if (upper >0) for ( i = 1; i <=upper; i++) sum +=i; pthread_exit(0);}
Chapter five 5.29 Operating System Concepts
Solaris 2 Threads
Chapter five 5.30 Operating System Concepts
Solaris Process
Chapter five 5.31 Operating System Concepts
Solaris: versatility
Implements the many-to-many mapping. We can use ULTs when logical parallelism does not need
to be supported by hardware parallelism (we save mode switching) e.g.: Multiple windows but only one is active at any
one time If threads may block then we can specify two or more
LWPs to avoid blocking the whole application Type of ULTs:
Bounded: bounded to a particular LWP Unbounded: may transit within a pool of LWPs
Chapter five 5.32 Operating System Concepts
Solaris: Lightweight Process States
LWP states are independent of ULT states(except for bound ULTs)
Chapter five 5.33 Operating System Concepts
Solaris: user-level thread states
(attached to a LWP)
Chapter five 5.34 Operating System Concepts
Windows 2000 Threads
Windows 2000 processes and threads are all implemented as objects.
Implements the one-to-one mapping. basic thread components
- a thread id- register set- separate user and kernel stacks- private data storage area(local variables)
A thread can be one of six states: ready( 就绪 ), standby( 备用 ), running( 运行 ), waiting( 等待 ), transition( 转换 ), and terminated( 终止 ).
Chapter five 5.35 Operating System Concepts
ResourceAvailable
Unblock/ResumeResource Available
UnblockResource Not Available
Block/Suspend
Terminate
SwitchPick toRun
Preempted
Transition Waiting Terminated
Not Runnable
Runnable
Ready
Standby
Running
Windows 2000 thread states
Chapter five 5.36 Operating System Concepts
Windows 2000 Process Object Attributes
Process IDSecurity DescriptorBase priorityDefault processor affinityQuota limitsExecution timeI/O countersVM operation countersException/debugging portsExit status
Chapter five 5.37 Operating System Concepts
Windows 2000 Thread Object Attributes
Thread IDThread contextDynamic priorityBase priorityThread processor affinityThread execution timeAlert statusSuspension countImpersonation tokenTermination portThread exit status
Chapter five 5.38 Operating System Concepts
3 main data structures of a thread
ETHREAD(executive thread block, 执行体线程块 ) - in kernel space KTHREAD(kernel thread block ,内核线程块 ) - in kernel space TEB(thread environment block ,线程环境块 ) - in user process space
Chapter five 5.39 Operating System Concepts
Windows NT/2000 fibers(构造 )
Also called light-weight thread
Invisible to OS, similar to user-level thread
Win32 APIs for fiber:
CreateFiber, SwitchToFiber, ConverThreadToFiber
Chapter five 5.40 Operating System Concepts
Java Threads
Java threads may be created by:
Creating(extending) Thread class
- thread is actually created when the start method in the class is invoked.
Java threads are managed by the JVM.
Chapter five 5.41 Operating System Concepts
Java Thread States