Transcript 第五章

Chapter 5: Threads
 Overview
 Multithreading Models
 Threading Issues
 Pthreads(POSIX thread)
 Solaris 2 Threads
 Windows 2000 Threads
 Linux Threads
 Java Threads
Operating System Concepts
5.1
Chapter five
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
Operating System Concepts
5.2
Chapter five
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
Operating System Concepts
5.3
Chapter five
Single and Multithreaded Processes
Operating System Concepts
5.4
Chapter five
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:可以充分利用多处理器体系结
构,使得一个进程中的线程在不同的处理器上运行,提高进程执
行的并行度
Operating System Concepts
5.5
Chapter five
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)
Operating System Concepts
5.6
Chapter five
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(需要进行线程间同步)
Operating System Concepts
5.7
Chapter five
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
Operating System Concepts
5.8
Chapter five
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
Operating System Concepts
5.9
Chapter five
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
Operating System Concepts
5.10
Chapter five
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
Operating System Concepts
5.11
Chapter five
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
Operating System Concepts
5.12
Chapter five
Advantages and inconveniences of ULT
 Advantages
 Inconveniences
 Thread switching does
 Most system calls are
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
Operating System Concepts
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
5.13
Chapter five
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
Operating System Concepts
5.14
Chapter five
Advantages and inconveniences of KLT
 Advantages
 Inconveniences
 the kernel can
 thread switching within the
simultaneously schedule
many threads of the
same process on many
processors
 blocking is done on a
thread level
 kernel routines can be
multithreaded
same process involves the
kernel. We have 2 mode
switches per thread switch
 this results in a significant
performance slowing down
Operating System Concepts
5.15
Chapter five
Multithreading Models
 Many-to-One
 One-to-One
 Many-to-Many
Operating System Concepts
5.16
Chapter five
Many-to-One
 Many user-level threads mapped to single
kernel thread.(纯用户级线程)
 Usually used on systems that do not support
kernel threads.
Operating System Concepts
5.17
Chapter five
Many-to-One Model
Operating System Concepts
5.18
Chapter five
One-to-One
 Each user-level thread maps to kernel thread.
(纯核心级线程)
 Examples
- Windows 95/98/NT/2000
- OS/2
Operating System Concepts
5.19
Chapter five
One-to-one Model
Operating System Concepts
5.20
Chapter five
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
Operating System Concepts
5.21
Chapter five
Many-to-Many Model
Operating System Concepts
5.22
Chapter five
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.
Operating System Concepts
5.23
Chapter five
 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.
Operating System Concepts
5.24
Chapter five
 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
Operating System Concepts
5.25
Chapter five
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.
Operating System Concepts
5.26
Chapter five
Threads on linux(LinuxThreads)
 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, signalhandling, 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
Operating System Concepts
5.27
Chapter five
#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);
}
Operating System Concepts
5.28
Chapter five
Solaris 2 Threads
Operating System Concepts
5.29
Chapter five
Solaris Process
Operating System Concepts
5.30
Chapter five
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
Operating System Concepts
5.31
Chapter five
Solaris: Lightweight Process States
LWP states are independent of ULT states
(except for bound ULTs)
Operating System Concepts
5.32
Chapter five
Solaris: user-level thread states
(attached to a LWP)
Operating System Concepts
5.33
Chapter five
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(终止).
Operating System Concepts
5.34
Chapter five
Windows 2000 thread states
Runnable
Standby
Pick to
Run
Preempted
Ready
Unblock/Resume
Resource Available
Resource
Available
Switch
Transition
Waiting
Running
Block/
Suspend
Terminate
Terminated
Unblock
Resource Not Available
Not Runnable
Operating System Concepts
5.35
Chapter five
Windows 2000 Process Object
Attributes
Process ID
Security Descriptor
Base priority
Default processor affinity
Quota limits
Execution time
I/O counters
VM operation counters
Exception/debugging ports
Exit status
Operating System Concepts
5.36
Chapter five
Windows 2000 Thread Object
Attributes
Thread ID
Thread context
Dynamic priority
Base priority
Thread processor affinity
Thread execution time
Alert status
Suspension count
Impersonation token
Termination port
Thread exit status
Operating System Concepts
5.37
Chapter five
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
Operating System Concepts
5.38
Chapter five
Windows NT/2000 fibers(构造)
 Also called light-weight thread
 Invisible to OS, similar to user-level thread
 Win32 APIs for fiber:
CreateFiber, SwitchToFiber,
ConverThreadToFiber
Operating System Concepts
5.39
Chapter five
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.
Operating System Concepts
5.40
Chapter five
Java Thread States
Operating System Concepts
5.41
Chapter five