Chapter 5并发性: 互斥和同步

Download Report

Transcript Chapter 5并发性: 互斥和同步

Operating
Systems:
Internals
and Design
Principles
Chapter 5
并发性:
互斥和同步
Seventh Edition
By William Stallings
“ 为控制同步事件而正确地进行程序设计,是系统编程中
最困难的方面之一。早期的多道程序和实时系统的程序
员们曾使用过的Ad hoc技术,就经常被一些难以捉摸的
编程错误所困扰,而且这些错误只有在一些动作以某些
相对罕见的次序发生时才会出现。发生这些错误所需要
的精确条件很难复现,所以这些错误很难定位。”
—THE COMPUTER SCIENCE AND
ENGINEERING RESEARCH STUDY,
MIT Press, 1980
 操作系统设计的核心问题是进程和线
程的管理:
 多道程序设计
 多处理器技术
 分布式处理器技术
多应用程序
结构化应用
多个活动应用共
享处理器时间
模块化设计和结
构化程序设计的
扩展
操作系统结
构
操作系统自身可
能作为一组线程
或进程实现
与并发相关的关键术语

原子操作:不可分割的一组指令

临界区:访问共享资源的一段代码

死锁:相互等待其他进程做完某事,而不能继续执行

活锁:

互斥:一个进程在临界区访问共享资源时其他进程不能
进入临界区访问共享资源

竞争条件:多进程读写一个共享数据,结果依赖于它们
的执行时间

饥饿:可运行进程被调度程序无限期忽视
5.1 并发的原理

交替和重叠,能被看作并发的实例,代表相同的
问题



多道程序在单处理器系统中交替执行
多道程序在多处理器系统中重叠执行
单处理器– 进程的相对运行速度不可预测



取决于其他进程的活动
操作系统处理中断的方式
操作系统的调度策略
带来的困难
 全局资源的共享充满了危险(如,对全局
变量的读写)
 OS很难对资源进行最优化分配(避免死锁)
 很难定位程序设计中的错误(结果不确定、
不可再现)
一个例子
多处理器
进程P1
chin = getchar();
…
chout = chin;
putchar(chout);
…
进程P2
…
chin = getchar();
chout = chin;
…
putchar(chout);
•
单处理器
void echo()
{
chin = getchar();
chout = chin;
putchar(chout);
}
局共
资享
源的
』全
局
变
量
『
或
全
 在多进程或线程读写数据室发生
 最终结果取决于执行的次序

竞争的“loser”(后更新更新全局变量的
进程)决定变量的最终值
操作系统关注的问题

并发带来的OS设计和管理问题:
 跟踪不同的进程(进程控制块)
 必须为每个活跃进程分配和释放各种资源(CPU、
RAM、FILE、I/O)
 保护每个进程的数据和物理资源,免受其他进程的影
响
 确保进程功能和输出与执行速度无关
感知程度
关系 一个进程对其他进 潜在的控制问题
程的影响
不知道对方 竞争 一个进程的结果与 互斥、死锁(可复用的资源)、饥
的存在
另一进程无关
饿
可能影响执行时间
间接知道对 通过 一个进程的结果可 互斥、死锁(可复用的资源)、饥
方存在(共享 共享 能依赖于从另一进 饿、数据相关性
对象)
合作 程获得的信息
可能影响执行时间
直接知道对 通过 一个进程的结果可 死锁(可消耗的资源)
方存在(通信 通信 能依赖于从另一进
饥饿
原语)
合作 程获得的信息
可能影响执行时间
进程间的资源竞争
 并发进程使用同一资源时发生冲突
 I/O devices, memory, processor time, clock
面对三个控制问题
• 互斥 mutual exclusion
• 死锁 deadlock
• 饥饿 starvation
互斥机制
Figure 5.1
Illustration of Mutual Exclusion

必须强制。一次只许一个进程进入临界区

在非临界区停止的进程不干涉其他进程

不能发生deadlock或starvation

当没有进程访问临界区时,需要临界区的进程可立即

对进程的相对执行速度和处理器的数量没有限制

一个进程驻留在临界区的时间必须是有限的
采用哪种方法?
软件 or 硬件
5.2 互斥:硬件的支
持
– 单处理器系统
– 效率
– 禁用中断保证互斥执行
– 不适合多处理器系统
while(true){
/*禁用中断*/
/*临界区*/
/*启用中断*/
/*其余程序*/
}

专用硬件指令
 Compare&Swap指令




也称为“compare &exchange”指令
compare内存值memory value与测试值 test value
如果两个值相同,发生swap
原子性执行 carried out atomically
int compare_and_swap(int *word, int testval, int newval){
int oldval;
oldval = *word;
if (oldval == testval) *word = newval;
return oldval;
}
Figure 5.2 Hardware Support for Mutual Exclusion
Exchange 指令
Figure 5.2 Hardware Support for Mutual Exclusion
适应于任何数量的进程间共享内
存,不管是单处理器还是多处理
器

简单并易于证明
 可支持多个临界区,每个临界区可
以有自己的变量

机器指令的缺点
使用了忙等待Busy-waiting ,进程等待进
入临界区前,继续消耗CPU
 可能饥饿:当一个进程离开临界区时,随
机选择等待进程
 可能死锁:单处理器,进程P1使用
compare & swap指令进入临界区,然后被中
断,让给更高优先级的P2,P2使用相同资源

5.3 信号量
信号量->管程->消息
信号量
An integer value used for signaling among processes. Only three
operations may be performed on a semaphore, all of which are
atomic: initialize, decrement, and increment. The decrement
operation may result in the blocking of a process, and the increment
operation may result in the unblocking of a process. Also known as a
counting semaphore or a general semaphore
二元信号量
A semaphore that takes on only the values 0 and 1.
互斥量
Similar to a binary semaphore. A key difference between the two is
that the process that locks the mutex (sets the value to zero) must be
the one to unlock it (sets the value to 1).
条件变量
A data type that is used to block a process or thread until a particular
condition is true.
管程
A programming language construct that encapsulates variables,
access procedures and initialization code within an abstract data type.
The monitor's variable may only be accessed via its access
procedures and only one process may be actively accessing the
monitor at any one time. The access procedures are critical sections.
A monitor may have a queue of processes that are waiting to access
it.
事件标志
A memory word used as a synchronization mechanism. Application
code may associate a different event with each bit in a flag. A thread
can wait for either a single event or a combination of events by
checking one or multiple bits in the corresponding flag. The thread is
blocked until all of the required bits are set (AND) or until at least
one of the bits is set (OR).
信箱/消息
A means for two processes to exchange information and that may be
used for synchronization.
自旋锁
Mutual exclusion mechanism in which a process executes in an
infinite loop waiting for the value of a lock variable to indicate
availability.
信号量Semaphore
一个整数变量,
定义三个操作:
除此之外没有
其他方法监视
或操作信号量
1) 初始化为一个非负数
2) semWait 使信号量减1
(P)
3) semSignal使信号量加1
(V)
信号量定义的三个结论
进程在将信号量减1
之前,无法知道该
进程是否会被阻塞
一个进程对信号量
加1后,另一个进程
被唤醒,并发运行。
单处理器系统中无
法知道哪个进程继
续运行。
发出信号后,不必
知道是否有另一个
进程在等待,被解
除阻塞的进程数量
是0还是1
信号量原语定义
Semaphore Primitives
二元信号量原语
Binary Semaphore Primitives
 需要一个队列来保存在信号量上等待的进程
Strong Semaphores
• 最先被阻塞的变量被最先取出(FIFO)
Weak Semaphores
• 没规定进程从队列中移除顺序的信号量
Producer/Consumer
生产者/消费者问题
一般描述:
• 一个或多个生产者产生数
据并将其置于缓冲区中
• 某个时刻一个消费者从缓
冲区中取出一项数据
• 任意时刻只可有一个主题
(生产者或消费者)访问
缓冲区
问题:
• 确保生产者不能
向满的缓冲区中
写数据
• 消费者不能从空
缓冲区中拿数据
Buffer 结构
producer:
while(true){
/*生产v*/
b[in] = v;
in++;
}
consumer:
while(true){
while(in <= out);
w = b[out];
out++;
/*消费w*/
}
Figure 5.9 An Incorrect Solution to the Infinite-Buffer Producer/Consumer Problem Using Binary Semaphores
Figure 5.10 A Correct Solution to the Infinite-Buffer Producer/Consumer Problem Using Binary Semaphores
用
信
号
量
的
解
决
方
法
有限循
环缓冲
区
信
号
量
的
解
决
方
案
Figure 5.13 A Solution to the Bounded-Buffer Producer/Consumer Problem Using Semaphores
信号量的实现
 semWait
和semSignal 操作必须作为原语
实现
 可由硬件或固件firmware实现
 可以用软件实现,如Dekker’s
法
 使用硬件支持方案实现互斥
或Peterson’s 算
5.4 管程
信号量->管程->消息
管程Monitors
是一个程序语言结构,提供与信号量相同的功能并
易于控制
 已在许多编程语言中实现




并发Pascal, Pascal-Plus, Modula-2, Modula-3, and Java
也被当作一个程序库实现,使用利用其锁定任何对
象
是个软件模块,由一个或多个过程、一个初始化序
列和局部数据组成
管程的特征
局部数据变量只
能由管程的过程
访问,外部过程
不能访问
任何时候,只能
由一个进程在管
程中执行
一个进程通过调
用管程的过程进
入管程
同步支持Synchronization

通过使用条件变量condition variables 提供对同步
的支持,这些变量在管程中并只能由管程访问

两个函数访问条件变量:


cwait(c): 调用进程的执行在条件c上阻塞,管程可被
另一个进程使用。
csignal(c): 恢复执行在cwait之后由于某些条件而阻塞
的进程。
• 与信号量的不同?
Figure 5.15 Structure of a Monitor
Figure 5.16 A Solution to the Bounded-Buffer Producer/Consumer Problem Using a Monitor
5.5 消息传递
信号量->管程->消息

进程交互需要满足两个要求:
同步
• 确保互斥

通信
• 交换信息
消息传递提供了这两个功能

在分布式系统、共享内存多处理器和但处理器系统中适用
消息传递

消息传递的实际功能以一对原语实现:
send (destination, message)
receive (source, message)

一个进程以message 的形式发送给另一个由
destination指定的进程

一个进程通过receive原语接收来自source进程发来的
message
消息传递设计中的特点
同步:阻塞与非阻塞
寻址:直接与间接
格式:内容与长度(是否可
变)
排队规则:FIFO与优先级
Table 5.5 Design Characteristics of Message Systems for Interprocess Communication and Synchronization
 发送者和接收者都被阻塞,直到消息被传递
 有时被称为会合rendezvous
 考虑了进程的紧密同步
非阻塞Send
非阻塞send, 阻塞receive
• 发送者继续,但接收者被阻塞,直到请求的消息到达
s
• 最常用的组合
• 允许尽快地运行发送一条或多条消息到多个目的地
• example – 一个向其他进程提供服务或资源的进程
非阻塞send, 非阻塞receive
• 任何一方不要求等待
 在send和receive原语中确定目标或源进程
Direct
addressing
Indirect
addressing
直接寻址


Send原语指定了目标进程的标识符
Receive原语有两种处理方式:
 要求进程显式指定发送进程

对处理并发进程间合作非常有效
 隐式寻址

Receive原语的source参数receive操作执行后返回
间接寻址
消息被写道共享数据结
构中,该结构由临时保
存消息的队列组成
在消息的使用上
有更大的灵活性
这些队列称为信
箱 mailboxes
一个进程发送消息到
mailbox 其他进程从
mailbox中获取消息
使用消息的互斥
消息通信解决有界缓冲区的生产者消费者问题
Figure 5.21 A Solution to the Bounded-Buffer Producer/Consumer Problem Using Messages
5.6 读者/写者问题
信号量->管程->消息
读者/写者问题

一个由多进程共享的数据区或文件


一些进程(读者)只能读数据区,另一些进程(写
者)只能写数据区
必须满足下列条件:
1. 任意多进程可同时读这个文件
2. 同时只允许一个进程写这个文件
3. 当一个写者写文件时,不允许读者读文件
读者优先
Figure 5.22 A Solution to the Readers/Writers Problem Using Semaphore: Readers Have Priority
写者优先
Figure 5.23 A Solution to the Readers/Writers Problem Using Semaphore: Writers Have Priority
Readers only •wsem set
进程队列的状态
in the system
•no queues
Writers only •wsem and rsem set
in the system •writers queue on wsem
•wsem set by reader
Both readers •rsem set by writer
and writers
•all writers queue on wsem
with read first •one reader queues on rsem
•other readers queue on z
•wsem set by writer
Both readers •rsem set by writer
and writers
•writers queue on wsem
with write
•one reader queues on rsem
first
•other readers queue on z
消息传递
Figure 5.24 A Solution to the Readers/Writers Problem Using Message Passing
Messages
• Useful for the enforcement of mutual exclusion discipline
Operating system themes are:
• Multiprogramming, multiprocessing, distributed processing
• Fundamental to these themes is concurrency
• issues of conflict resolution and cooperation arise
Mutual Exclusion
• Condition in which there is a set of concurrent processes, only one of
which is able to access a given resource or perform a given function
at any time
• One approach involves the use of special purpose machine
instructions
Semaphores
• Used for signaling among processes and can be readily used to enforce
a mutual exclusion discipline