1 / 107

CHAPTER 5 Concurrency : Mutual Exclusion and Synchronization ( 并发性:互斥和同步 )

CHAPTER 5 Concurrency : Mutual Exclusion and Synchronization ( 并发性:互斥和同步 ). Concurrency (并发性). Communication among processes Sharing of and competing ( 竟争 )for resources Synchronization of the activities of multiple processes Allocation of processor time to processer. Concurrency.

alton
Download Presentation

CHAPTER 5 Concurrency : Mutual Exclusion and Synchronization ( 并发性:互斥和同步 )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 5Concurrency:Mutual Exclusion and Synchronization(并发性:互斥和同步)

  2. Concurrency(并发性) • Communication among processes • Sharing of and competing (竟争)for resources • Synchronization of the activities of multiple processes • Allocation of processor time to processer

  3. Concurrency • Multiple applications • Multiprogramming • Structured application • Application can be a set of concurrent processes • Operating-system structure • Operating system is a set of processes or threads

  4. 5.1 PRINCIPLES OF NCURRENCY(并发的原理) • Difficulties with Concurrency • Sharing global resources • Management of allocation of resources • It become very difficult to locate a Programming errors

  5. A Simple Example procedure echo; Var out,in:character; begin input (in,keyboard); out: = in; output(out,display); end

  6. A Simple Example Process P1 Process P2 . . Input(in,keyboard) . . Input(in,keyboard) Out: = in out: = in Output(out,display) . . Output(out,display) . .

  7. Operating System Concerns(操作系统关注的问题) • Keep track of active processes:PCB • Allocate and deallocate resources • Processor time:scheduling • Memory:virtual memory • Files • I/O devices • Protect data and resources • Result of process must be independent of the speed of execution of other concurrent processes(应保证进程执行的结果与速度无关)

  8. Process Interaction • Processes unaware of each other - Competition - Mutual exclusion, Deadlock, Starvation • Processes indirectly aware of each other - Cooperation by sharing - Mutual exclusion, Deadlock, Starvation, Data coherence(一致性) • Process directly aware of each other - Cooperation by communication - Deadlock, Starvation

  9. 知道程度 关系 一个进程对其他进程的影响 潜在的控制问题 进程之间不知道对方 (进程间无工作联系) 竞争 1一个进程的结果与其它进程的活动无关 2进程的分时可能会受到影响 互斥 死锁(可复用的资源) 饿死 进程间接知道对方 (如共享对象) 通过共享合作 1.一个进程的结果可能依赖于从其他进程获得的信息 2.进程的分时可能会受到影响 互斥 死锁(可复用的资源) 饿死 数据一致性 进程直接知道对方 (它们有可用的通信原语) 通过通信合作 1.一个进程的结果可能依赖于从其他进程获得的信息 2.进程的分时可能会受到影响 死锁(可消费的资源) 饿死 表5.1进程的交互

  10. Competition Among Processes for Resources • Mutual Exclusion(互斥) • Critical sections(临界区) • Only one program at a time is allowed in its critical section (一次仅允许一个进程在临界区) • Example only one process at a time is allowed to send command to the printer(critical resource)(例如一次仅允许一个进程发打印命令) • Deadlock(死锁) • Starvation

  11. Competition Among Processes for Resources • Deadlock 例如,有两个进程P1、P2,竞争两个资源R1、R2。假设 占用:P1(R2) and P2(R1) 申请:P1(R1) and P2(R2) 结果:P1、P2永久等待(死锁)

  12. Competition Among Processes for Resources • Starvation(饿死) 例如,有三个进程P1、P2、P3,竞争资源R假设 占用:P1(R) 申请:P2(R) and P3(R) 释放:P1(R) 占用:P2(R) 申请:P1(R) and P3(R) 释放:P2(R) 占用:P1(R) 结果:P3(饿死)

  13. Mutual Exclusion Mechanism互斥机制 entry section critical section exit section (阅读P194,图5.1)

  14. Cooperation Among Processes by Sharing(进程间由于共享合作) • Writing must be mutually exclusive (对写共享变量必须互斥) • Critical sections are used to provide data integrity (确保数据完整性使用临界区) • 例如:两个数据保持相等:A=B,初始值相等。 P1:A=A+1; B=B+1; P2:B=2*B; A=2*A • 当P1和P2顺序执行,能保证一致。但并发执行,就可能出现A≠B,因此必须采用临界资源管理才能达到数据的一致性。

  15. Cooperation Among Processes by Communication(进程间由于通信的合作) • Because nothing is shared between processes in the act of passing messages • Mutual exclusion is not a control requirement for this sort of cooperation. • Possible to have deadlock • Each process waiting for a message from the other process • Possible to have starvation • Two processes sending message to each other while another process waits for a message

  16. Requirements for Mutual Exclusion(互斥的要求) 1. Only one process at a time is allowed in the critical section for a resource (一次仅允许一个进程进入临界区) 2. A process that halts in its non-critical section must do so without interfering with other processes(处于非临界区的进程不能干预其它进程) 3. No deadlock or starvation

  17. Requirements for Mutual Exclusion(互斥的要求) 4. A process must not be delayed access to a critical section when there is no other process using it(当没有进程在临界区时应立即进入) 5. No assumptions are made about relative process speeds or number of processors(对相关进程的速度和处理机的数目没有任何要求和限制) 6. A process remains inside its critical section for a finite time only(一个进程驻留在它的临界区中的时间必须是有限的确)

  18. Requirements for Mutual Exclusion(互斥的要求) • 空闲让进 • 忙则等待 • 有限等待 • 让权等待

  19. Approaches of Mutual Exclusion(互斥的方法) • Software Approaches( 软件方法) • Hardware Support • Semaphores • Monitors • Message Passing

  20. 5.2 Software Approaches • Memory access level • Access to the same location in main memory are serialized by some sort of memory arbiter • Dekker’s Algorithm • Peterson’s Algorithm

  21. Dekker’s Algorithm :First Attempt • Busy Waiting • Process is always checking to see if it can enter the critical section • Process can do nothing productive until it gets permission to enter its critical section

  22. Dekker’s Algorithm : First Attempt(第1种尝试) • 图5.2“爱斯基摩人的小屋协议”(P198) • 门和小屋很小,每次只能容纳一个人进入,小屋内有一个标志黑板 • 进程申请进入临界区,必须首先进入小屋并检查黑板标志是否是它 是,离开小屋,进入临界区,执行完毕,退出临界区, 并返回小屋,修改黑板标志为其他进程 否,反复进入小屋,检查黑板标志,直到标志是它自己

  23. 小黑版指示P1可进

  24. var turn:0..1; {共享的全局变量} PROCESS 0 PROCESS 1 … … while turn≠0 do {nothing}; while turn ≠ 1 do {nothing}; <critical section>; <critical section> turn:=1; turn:=0; … … • 解析 保证了互斥,但存在问题:进程“忙等”进入临界区;若黑板标志修改失败,其他进程永久阻塞

  25. Dekker’s Algorithm ★Second Attempt (第2种尝试) • Each process can examine the other’s status but cannot alter it • When a process wants to enter the critical section, It checks the other processes first • If no other process is in the critical section, it sets its status for the critical section

  26. 图5.3 每个人有一个自己的小屋 (P199) • 每个人只能检查,但不能修改其他人的黑板标志 • 若某人申请进入临界区,首先检查对方黑板是否为“false” 是,修改自己小屋黑板值为“true”,进入临界区执行,完毕,恢复黑板值为“false” 否,反复进入小屋,检查黑板标志,直到标志是“false”

  27. var flag : array [0..1] of boolean :false ; {共享全局变量} PROCESS 0 PROCESS 1 … … while flag[1] while flag[0] do {nothing}; do {nothing}; flag[0]:=true; flag[1]:=true; <critical section>; <critical section> flag[0]:=false; flag[1]:=false; … …

  28. 若进程执行完临界区,恢复自己标志为“false”失败,则其他进程永久阻塞。若进程执行完临界区,恢复自己标志为“false”失败,则其他进程永久阻塞。 • This method does not guarantee mutual exclusion 分析以下执行顺序: P0:当flag[1]=false; 执行while flag[1]; P1:当flag[0]=false; 执行while flag[0]; P0:置flag[0]=true 执行<critical section>; P1:置flag[1]=true; 执行<critical section>;

  29. Dekker’s Algorithm ★Third Attempt (第3种尝试) • Set flag to enter critical section before check other processes • If another process is in the critical section when the flag is set, the process is blocked until the other process releases the critical section

  30. 首先设标志 var flag : array [0..1] of boolean :false ; {共享的全局变量} PROCESS 0 PROCESS 1 … … flag[0]:=true; flag[1]:=true; while flag[1] while flag[0] do {nothing}; do {nothing}; <critical section>; <critical section> flag[0]:=false; flag[1]:=false; … … 解析:如果两个进程在执行while之前都把flag设置成true,那么每个进程都会以为对方进入了临界区,使自己处于阻塞,从而导致死锁。

  31. Dekker’s Algorithm ★Fourth Attempt (第4种尝试) • A process sets its flag to indicate its desire to enter its critical section but is prepared to reset the flag • Other processes are checked. If they are in the critical region, the flag is reset and later set to indicate desire to enter the critical region. This is repeated until the process can enter the critical region.

  32. var flag : array [0..1] of boolean :false ; {共享的全局变量} PROCESS 0 PROCESS 1 … … flag[0]:=true; flag[1]:=true; while flag[1] do while flag[0] do begin begin flag[0] :=false; flag[1] :=false; <delay for a short time>; <delay for a short time>; flag[0]:=true; flag[1]:=true; end; end; <critical section>; <critical section> flag[0]:=false; flag[1]:=false; … … 重置标志

  33. 试按以下顺序执行: P0:置flag[0]=true; P1:置flag[1]=true; P0:执行while flag[1]; P1:执行while flag[0]; P0:置flag[0]=false; P1:置flag[1]=false; P0:置flag[0]=true; P1:置flag[1]=true; … 解析:检查其它进程,然后重置,再检查,再重置…, 重置序列可以无线延伸,任何一个进程都不能进入自己的临界区。(这种现象称为:互斥礼让)

  34. Dekker’s Algorithm : Correct Solution • 为了解决“互斥礼让”问题,给出:Dekker’s Algorithm : Correct Solution • 该算法的主要思想是:需要两个变量: 1. turn:指出应该哪一个进入 the critical section turn=0 表示P0可以进入 turn=1 表示P1可以进入 2.Flag:指出当前哪一个在临界区 Flag=0 表示P0当前在临界区 Flag=1 表示P1当前在临界区 • 图5.4增加一个带准许进入临界区标志的小屋

  35. 指示P0可以进入临界区

  36. 代码描述(P203,图5.5) var flag: array[0..1] of boolean; turn:0..1; //准许进入临界区标志变量 begin flag[0]:=false; flag[1]:= false; turn:=1;//设P1进程在临界区 parbegin p0;p1; parend end

  37. procedure P0; Begin repeat flag[0]:=true; //初值为真, P0希望进入临界区。 while flag[1] do //查P1进程标志 if turn=1 then //turn为1,表明P1进程在临界区 begin flag[0]:=false; while turn=1 //当turn=0,表明P0进程可以进入临界区 do { nothing }; flag[0]:= true; //设标志为真,P0进入临界区 end; <critical section > ; turn:=1; //指定P1进程可进临界区 flag[0]:= false; <remainder > forever end

  38. procedure P1; begin repeat flag[1]:=true; while flag[0] do if turn=0 then begin flag [1]:=false; while turn=0 do { nothing}; flag[1]:=true end; <critical section >; turn:=0; flag[1]:= false; <remainder> forever end

  39. 解析(分析P0的执行) 1. 置flag[0]:=true; 2. 执行while flag[1] 语句有两种情况: (1)当flag[1] = false, P0进入临界区,执行完成,置turn:=1; flag[0]:=false ; (2)当flag[1] = true,检查turn的值, turn的值又有两种情况: ①turn =1 P0忙等 ②turn =0 P1已退出(但还未修改flag的值), P0立即进入

  40. Peterson’s Algorithm • 提出简单的互斥方案:turn解决同时的冲突,Flag指示进程是否在临界区。对两参量同时控制,交替进入临界区执行。 • 代码描述(图5.6,P204) • 解析: 分析P0的执行: 1.置flag[0]:=true;{阻止P1进入临界区} 2.执行while flag[1] 当flag[1]=false时,P0进入,执行完成,置flag[0]:=false; 当flag[1]=true时,P0阻塞,等待P1退出

  41. var flag:array[ 0..1]of boolean; turn:0..1; begin flag[0]:=false; flag[1]:=false; turn:=1; //假设P1进程可先进临界区 parbegin P0;P1; //见下页 parend end Figure5.6 Peterson’s Algorithm for Two Processes

  42. procedure P0; begin repeat flag[0]:=true; //P0进程希望进临界区 turn:=1; while flag[1] and turn=1 do { nothing}; <critical section>; flag[0]:=false; <remainder> forever end;

  43. procedure P1; begin repeat flag[1]:=true; turn:=0; while flag[0] and turn=0 do { nothing }; < critical section >; flag[1]:=false; < remainder > forever end;

  44. Approaches of Mutual Exclusion • Software Approaches • Hardware Support • Semaphores • Monitors • Message Passing

  45. 5.3 Mutual Exclusion: Hardware Support(互斥:硬件支持) • Interrupt Disabling(中断屏蔽) • A process runs until it invokes an operating-system service or until it is interrupted (进程运行到它调用系统服务或被中断为止) • Disabling interrupts guarantees mutual exclusion(屏蔽中断以确保互斥) • Multiprocessor(在多处理机中) • disabling interrupts on one processor will not guarantee mutual exclusion

  46. 实现互斥的过程 While (true ) { <disable interrupts> /* 屏蔽中断 */ <critical section> /* 临界区 */ <enable interrupt> /* 启用中断 */ <remainder> /* 其余部分 */ }

  47. Special Machine Instructions(专门的机器指令 ) • 在一个多处理器配置中,几个处理器共享访问一个公共的主存。在这种情况下,不存在主/从关系,处理器的间行为是无关的,表现出一种对等关系,处理器之间没有支持互斥的中断机制。处理器的设计者提出了一些机器指令,用于保证两个动作的原子性。 • 如在一个取指令周期中对一个存储器单元的读和写,或者读和测试。由于这些动作在一个指令周期中执行,它们不会受到其他指令的干扰。

  48. 两种最常见的指令描述如下: 1. Test and Set Instruction function testset ( var i:integer ) : boolean ; begin if i = 0 then begin i := 1; testset := true; end else testset :=false; end. 例如:Figure5.7 a (p206)

  49. program mutualexclusion; const n=...;(*number of processes* ); var bolt:integer; procedure PO(i:integer); begin repeat repeat { nothing } until testset(bolt) ;当bolt为0时,进入临界区, < critical section >; bolt:= 0; < remainder> forever end; begin(* main program*) bolt:=0; parbegin P(1); P(2); ... P(n); parend end (a)Test and Set Instruction

More Related