平面图装修机械设计流线凸缘齿槽二端网络误差椭圆卤钨灯泡黏弹性入库检验
一个行程是一个单一执行緖控制的执行程  文件类型:DOC/Microsoft Word  文件大小:字节
Chapter 5 Threads 执行绪
A process was an executing program with a single thread of control
一个行程是一个单一执行緖控制的执行程.
Many modern OSs now provide features for a process to contain multiple threads of control
许多近代的作业系统现在都提供一个行程中包含许多执行緖控制的特性.
5.1 Overview---概论
A thread (lightweight process, LWP) is a basic unit of CPU utilization
一个执行緖(轻量级行程)是CPU使用时的一个基本单位.
It comprise a thread ID, a program counter, a register set, and a stack
它是由执行緖ID,程式计数器,一组暂存器和堆叠空间组成的.
It share with other threads belonging to the same process its code section, data section, and other OS resources, such as open files and signals
它和属於同一行程的其它执行緖共用程式码区域,资料区域和作业系统资源.
如开启的档案和信号.
A traditional (heavyweight) process has a single thread of control
传统的行程(重量级行程)只有单一执行緖控制.
If a process has multiple threads of control, it can do more than one task at a time
因为多执行緖有多个执行緖控制,所以行程可以一次执行一项以上的任务.
5.1.1 Motivation---动机
Many software packages that run on modern desktop PCs are multithread
许多桌上型PC执行的套装软体都是多执行緖(multithread).
An application typically is implemented as a separate process with several thread of control
应用程式通常都制作成有许多执行緖控制的个别行程
e.g., a web browser might have one thread display images or text while another thread retrieves data from the network
如:网页浏览器可能有一个执行緖显示颢像或文字,而另一执行緖则从网路撷取资料.
e.g., a word processor may have a thread for displaying graphics, another for reading keystrokes from the user, and a third thread for performing spelling and grammar checking in the background
如:文书处理器可能有一个执行緖在显示图形,另一个执行緖从使用者读入按键.
If the web server ran as a traditional single-threaded process, it would be able to service only one client at a time
如果网页伺服器以传统的单执行緖(single-threaded)行程执行,那麼它只能一次服务一个客户.
The amount of time that a client might have to wait for its request to be serviced could be enormous
客户必须等待其要求被服务的时间可能很长.
Process creation is very heavyweight, as shown is Chapter 4
行程的产生对系统是重担.如第四章
If the new process will perform the same tasks as the existing process, why incur all that overhead
如果新行程和现存行程执行相同的工作,为何要增加额外的负担呢
It is generally more efficient for one process that contains multiple threads to serve the same purpose
通常比较有效率的方法是让一个行程包含许多执行緖来达到相同的目的.
This approach would multithread the web-server process
网页伺服器的行程变成多执行緖
The server would create a separate thread that would listen for client requests
伺服器将产生一个个别的执行緖倾听客户端的要求.
When a request was made, it would create another thread to service the request
当有要求提出时,伺服器将产生另一个执行緖来服务此要求,而非产另一个行程.
Typically, RPC servers are multithreaded
执行绪在远程程序呼叫(RPC)的系统也扮演重要的角色.
When a server receives a message, it services the message using a separate thread
当伺服器接收到讯息时,它就使用另一个个别的执行緖服务此讯息.
This allows the server to service several concurrent requests
这允许伺服器服务一些并行产生的要求.
5.1.2 Benefits---利益
The benefits of multithreaded programming have 4 categories:
撰写多执行绪程式的好处可以分成四个主要类别
Responsiveness反应度:
Multithreading an interactive application may allow a program to continue running even if part of it is blocked or is performing a lengthy operation
将交谈式的应用程式多执行緖化,可以在一个程式某一部份被暂停,或程式在执行冗长操作时,依然继续执行.
e.g., a multithreaded web browser could still allow user interaction in one thread while an image is being loaded in another thread
例:一个多执行緖的网页浏览器在一个执行緖载入影像时,它还能以另一执行緖跟使用者交谈.
Resource Sharing资源分享:
Threads share the memory and the resources of the process to which they belong
执行緖间将共用它们所属行程的记忆体和资源.
The benefit of code sharing is that it allows an application to have several different threads of activity all within the same address space
程式码共用的好处是让应用程式有数个不同的执行緖在同一位址空间活动.
Economy经济:
Allocating memory and resources for process creating is costly
行程产生所配置的记忆体和资源耗费很大.
In general, it is much more time consuming to create and manage processes than threads
通常,产生和维护行程会比执行緖更费时.
e.g., in Solaris 2, creating a process is about 30 times slower than is creating a thread, and context switching is about five times slower
例:在Solarid 2上产生行程大约比产生执行緖慢30倍,内容转换大约慢5倍.
Utilization of multiprocessor Architectures使用多处理器架构:
In a single-processor architecture, the CPU generally moves between each thread so quickly as to create an illusion of parallelism, but in reality only one thread is running at a time
在单一处理器的架构,CPU在每一执行緖间快速地移动,让人误以为是同时在进行,但是事实上同一时间只有一个执行緖在执行.
In a multiprocessor architecture, each thread may be running in parallel on a different processor
在多处理器的架构下,每一执行緖可以并行地在不同的处理器上执行.
Multithreading on a multi-CPU machine increases concurrency
多执行緖同时在multi-CPU machine増加.
5.1.3 User and Kernel Threads---使用者和核心执行緖
Support for threads may be provided at either
对於执行绪的支援
the user level, for user threads, or by
由使用者层次提供(使用者执行緖) .
the kernel, for kernel threads
由核心提供(核心执行緖) .
User Threads
Supported above the kernel and are implemented by a thread library at the user level
支援是在核心之上,且是由使用者层次的执行緖程式库所制作.
The library provides support for thread creation, scheduling, and management with no support from kernel
程式库提供了执行緖产生,排班和管理的支援,但没有从核心获得支援.
Without kernel intervention, user-level threads are generally fast to create and manage
不需核心介入,使用者层次的执行緖通常在产生和管理上都比较快.
However, if the kernel is single-threaded, any user-level thread performing a blocking system call will cause the entire process to block, even if other threads are available to run within the application
然而,如果核心是单执行緖的,那麼任何使用者层次的执行緖执行暂停的系统呼叫将造成整个行程暂停执行,即使在行程中有个别的执行緖可以执行一样.
Examples---例子
- POSIX Pthreads
- Mach C-threads
- Solaris 2 UI-threads
Kernel Threads---核心緖
Supported directly by the OS
由作业系统直接支援.
The kernel performs thread creation, scheduling, and management in kernel space
执行緖的产生,排班和管理是由核心在核心空间完成.
Because thread management is done by the OS, kernel threads are generally slower to create and manage than are user threads
因为执行緖的管理是由作业系统负责,所以核心执行緖通常在产生和管理下都比
使用者执行緖慢.
However, since the kernel is managing the threads, if a thread performs a blocking system call, the kernel can schedule another thread in the application for execution
然而,因为核心管理执行緖,如困一个执行緖执行暂停的系统呼叫时,核心可以安排应用程式中的其它执行緖执行.
In Multiprocessor environment, the kernel can schedule threads on different processors
在多处理器的环境,核心可以安排执行緖在不同的处理器执行.
Most contemporary OS support kernel threads:
大部份近代的作业系统都支援核心执行緖.
- Windows NT/2000
- Solaris 2
- BeOS
- Tru64 UNIX
Java provides support for threads-----Java提供执行緖的支援.
However, as Java threads are created and managed by the Java virtual machine (JVM), they do not easily fall under the realm of either user or kernel threads
然而,因为Java的执行緖是由Java虚拟机器(JVM)所产生和管理,所以它不容易归类成使用者或核心的执行緖.
5.2 Multithreading Models---多执行緖的模式
Many systems provide both user and kernel threads, resulting in different multithreading models:
许多系统都提供使用者和核心执行緖,造成有不同的多执行緖模式.
Many-to-One Model---多对一模式
One-to-One Model---一对一模式
Many-to-Many Model---多对多模式
5.2.1 Many-to-One
Many user-level threads mapped to one kernel thread
许多个使用者层次的执行緖到一个核心执行緖.
Thread management is done in user space, so it is efficient
执行緖的管理在使用者空间执行,所以很有效率.
But the entire process will block if a thread makes a blocking system call
但如果一个执行緖呼叫暂停的系统呼叫,整个行程就暂停.
Because only one thread can access the kernel at a time, multiple threads are unable to run in parallel on multiprocessors
因为一次只有一个执行緖可以存取核心,数个执行绪不能在多个理器上并行地执行.
e.g., Green threads – a thread library available for Solaris 2 – use this model
e.g., Green threads--- Solaris 2的执行緖程式库---使用此模型.
Used on OSs that do not support kernel threads
不支援核心执行緖的作业系统
5.2.2 One-to-One
Each user-level thread maps to a kernel thread
每一个使用者执行緖到一个核心执行緖.
It provides more concurrency than the many-to-one model by allowing another thread to run when a thread makes a blocking system call
它提供了比多对一模式更多的并行功能,因为当一个执行緖执行暂停的系统呼叫时,它允许另一个执行緖执行,当一个执行緖执行暂停的系统呼叫时.
It also allows multiple threads to run in parallel on multiprocessors
它允许多个执行緖在多处理器上并行的执行.
The only drawback to this model is that creating a user thread requires creating the corresponding kernel thread
此模式的唯一缺点是,产生使用者执行緖时就要产生相对应的核心执行緖.
Because the overhead of creating kernel threads can burden the performance of an application, most implementations of this model restrict the number of threads supported by the system
因为产生核心执行緖的额外负担可能造成应用程式性能上的负担,这种模式的大部份实作都限制了系统支援的执行緖个数.
Examples
- Windows NT/2000
- OS/2
5.2.3 Many-to-Many Model
Multiplexes many user-level threads to a smaller or equal number of kernel threads
许多使用者执行緖到较少或相等数目的核心执行緖.
The number of kernel threads may be specific to either a particular application or a particular machine
核心执行緖的数目对於某一特殊应用或是某个特定机器可能是一特定数目.
More kernel threads on a multiprocessor allocated than on a uniprocessor
AP在多处理器上所分配的核心执行緖可能比在处理器上多.
Developers can create as many user thread as necessary
程式发展者可以产生自己所需要的执行緖.
The corresponding kernel threads can run in parallel on a multiprocessor
相对应的核心执行緖可以在多处理器上平行地执行.
When a thread performs a blocking system call, the kernel can schedule another thread for execution
当一个执行緖执行暂停的系统呼叫时,核心可以安排另一个执行緖执行.
Examples:
Solaris 2, IRIX, HP-UX, and True64 UNIX
5.3 Threading Issues---执行绪的事项
Some issues to consider with multithread programs:
一些多执行绪程式考虑的事项.
5.3.1 The fork and exec System Calls--- The fork and exec系统呼叫
In Chapter 4, the fork system call is used to create a separate, duplicate process
在第四章,系统呼叫fork如何被用来产生另一个复制的行程.
In a multithreaded program, the semantics of the fork and exec system calls change
在一个多执行緖的程式中,fork和exec等系统呼叫的语音也改变了.
If one thread in a program calls fork,
如果程式中的一个执行绪呼叫fork,
does the new process duplicate all threads or
新的行程复制所有的执行緖,或者
is the new process single-threaded
是新的行程是单执行緖呢
Some UNIX systems have chosen to have two versions of fork,
有些UNIX系统选择了拥有两种版本的fork
one that duplicates all threads and
一个是复制所有的执行緖.
another that duplicates only the thread that invoked the fork system call
另一个则是只复制呼叫系统呼叫fork的那一个执行緖.
Two versions of fork depends upon the application:
使用那一种版本的fork取决於应用.
If exec is called immediately after forking, then duplicating only the calling thread is appropriate
如果执行fork之后马上呼叫exec,只复制呼叫的执行緖是较为妥当的.
As the program specified in the parameters to exec will replace the process, duplicating all threads is unnecessary
因为exec参数所设定的程式将取代现有行程则复制所有的行程就不是必要的
If the separate process does not call exec after forking, the separate process should duplicate all threads
如果被复制的行程在执行fork后没有呼叫exec,则此行程应该复制所有的执行緖.
5.3.2 Cancellation---取消
Thread cancellation is the task of terminating a thread before it has completed
执行緖取消是在一个执行緖完成之前结束它.
e.g., if multiple threads are concurrently searching through a database and one thread returns the result, the remaining threads might be cancelled
如:如果多执行緖并行地搜寻一个资料库,而其中一个执行緖传回结果,剩余的执行緖就该被删除掉.
e.g., when a user presses a button on a web browser that stops a web page from loading any further, the thread loading the page is cancelled
如:当一个使用者对网页浏览器按下按钮停止网页进一步下载时,载入网页的执行绪就被取消.
A thread that is to be cancelled is often referred to as the target thread
被取消的执行绪通常称为"目标执行緖" .
Cancellation of a target thread may occur in 2 different scenarios:
目标执行緖的取消可能发生两种不同情况
Asynchronous cancellation:(非同步取消)
One thread immediately terminates the target thread
一个执行緖立即终止目标执行緖.
Deferred cancellation:(延迟取消)
The target thread can periodically check if it should terminate, allowing the target thread an opportunity to terminate itself in an orderly fashion
目标执行绪可以周期地检查它是否该被取消,这允许目标执行緖有机会以有条不紊的方式结束自己.
The difficulty with cancellation occurs in situations where
取消此执行緖会发生困难
Resources have been allocated to a cancelled thread or
资源己经分配给一个被取消的执行緖.
If a thread was cancelled while in the middle of updating data it is sharing with other threads
假如执行緖正在更新和其它执行緖所分享之资料时
This becomes especially troublesome with asynchronous cancellation
非同步的取消变得尤其麻烦.
The OS often will not reclaim all resources
作业系统无法取叵所有的资源.
Therefore, cancelling a thread asynchronously may not free a necessary system-wide resource
因此,非同步地取消一个执行緖可能无法释放整个系统需要的资源.
Alternatively in deferred cancellation, cancellation will occur only when the target thread checks to determine if it should be cancelled or not
取消的动作只有在目标执行緖自己检查并决定它是否该取消时才发生.
at a point when it can safely be cancelled
允许执行緖在它能安全地被取消.
Pthreads refers to such points as cancellation points
Pthreads称这些点为取消点
Most OSs allows a process or thread to be cancelled asynchronously
大部份作业系统允许一个行程或执行緖非同步地被取消.
The Pthread API provides deferred cancellation
表示Pthread API的作业系统允许延迟取消.
5.3.4 Thread Pools---执行緖池
In a multithreading web server, whenever the server receives a request, it creates a separate thread to service the request
在多执行緖的网页伺服器.每当伺服器接到一项要求,它就产生一个个别的执行緖以服务此要求.
However, A multithreaded server has potential problems:
然而,对一个多执行緖的伺服器有潜在的问题
The first concerns the amount of time required to create the thread prior to servicing the request, compounded with the fact that this thread will be discarded once it has completed its work
第一个问题是,在服务此项要求前产生执行緖所必须花的时间量,以及执行緖完成其工作时必须抛弃的事实.
If we allow all concurrent requests to be serviced in a new thread, we have not placed a bound on the number of threads concurrently active in the system
如果我们对於所有同时发生的要求都用一个新的执行緖来服务,并不限制系统中同时活动的执行緖个数.
Unlimited threads could exhaust system resources, such as CPU time or memory
没有限制的执行緖可能耗尽系统资源,如CPU时间和记忆体.
One solution to this issue is to use thread pools:
这个问题的一个解答是使用执行緖池(thread pools)
Create a number of threads at process startup and place them into a pool, where they sit and wait for work
行程开始执行时产生一些执行緖,并且放入一个池中,这些执行緖就坐著等待工作.
When a server receive a request, it awakens a thread from this pool – if one is available – passing it the request to service
当伺服器接到一项要求时,它就从池中唤醒一个执行緖-如果有的话-传给要求者执行服务.
Once completes its service, it returns to the pool awaiting more work
当执行緖完成其服务时,它就返回池中等待其它工作.
If the pool contains no available thread, the server waits
如果池中没有执行緖时,伺服器就等待直到有为止.
The number of threads in the pool can be set heuristically based upon factors such as
储存池中的执行緖个数可以根据像是.
the number of CPUs in the system
系统中CPU的个数.
the amount of physical memory, and
实体记忆体的大小
the expected numbe of concurrent client requests
预期中客户同时要求的个数
More sophisticated thread-pool architectures can dynamically adjust the number of threads in the pool according to usage patterns
更复杂的执行緖池架构可以根据使用的型态动态地调整池中的执行緖个数.
Provide the further benefit of having a small pool – thereby consuming less memory – when the load on the system is low
此架构提供了进一步的优点:当系统负载低的时候就拥有较小的池(因此耗费最少的记忆体).
5.3.5 Thread-Specific Data 执行绪的特定资料
Threads belonging to a process share the data of the process
属於同一行程的执行緖分享此行程的资料.
However, each thread might need its own copy of certain data in some circumstances – called thread-specific data
然而,在某些情况下,每一个执行緖可能需要某些资料的自有拷贝.称这些资料为执行緖的特定资料(thread-specific data).
e.g., in a transaction-processing system, we might service each transaction in a separate thread
如,在交易处理系统中,我们可能以个别的执行緖服务每一笔交易.
Furthermore, each transaction may be assigned a unique identifier
而且每一笔交易可能被设定一个独一无二识别码.
To associate each thread with its unique identifier we could use thread-specific data
为了使每一个执行緖和识别码相结合,我们可以使用执行緖的特定资料.
Most thread libraries – including Win32 and Pthreads – provide some form of support for thread-specific data
大部份的执行绪程式库-包括Win32和Pthreads-对执行緖的特定资料都提供某些形式的支援.
Java provides support as well
Java也提供此支援.
·上一篇:第一章
·下一篇:模型机设计
赞助商链接
下载链接