收藏 分享(赏)

《操作系统概念》第六版作业解答2.ppt

上传人:jmydc 文档编号:5552143 上传时间:2019-03-07 格式:PPT 页数:23 大小:146.50KB
下载 相关 举报
《操作系统概念》第六版作业解答2.ppt_第1页
第1页 / 共23页
《操作系统概念》第六版作业解答2.ppt_第2页
第2页 / 共23页
《操作系统概念》第六版作业解答2.ppt_第3页
第3页 / 共23页
《操作系统概念》第六版作业解答2.ppt_第4页
第4页 / 共23页
《操作系统概念》第六版作业解答2.ppt_第5页
第5页 / 共23页
点击查看更多>>
资源描述

1、Chapter 7进程同步n 进程的同步隐含了系统中并发进程之间存在的两种相互制约关系:竞争(互斥)与协作(同步)n 互斥: 两个并行的进程 A、 B, 如果当 A进行某个操作时, B不能做这一操作,进程间的这种限制条件称为进程互斥,这是引起资源不可共享的原因。 n 同步: 我们把进程间的这种必须互相合作的协同工作关系,称为进程同步。n 进程之间的互斥是由于共享系统资源而引起的一种间接制约关系n 进程之间的同步是并发进程由于要协作完成同一个任务而引起的一种直接制约关系n 如果无进程在使用共享资源时,可允许任何一个进程去使用共享资源(即使某个进程刚用过也允许再次使用),这是通过 进程互斥 的方式

2、来管理共享资源n 如果某个进程 通过共享资源得到指定消息 时,在指定消息未到达之前,即使无进程在共享资源仍不允许该进程去使用共享资源,这是通过采用进程同步 的方式来管理共享资源。n 有些问题是互斥问题,有些是同步问题,有些是互斥同步混合问题实现临界区互斥的基本方法n 临界资源:一些被 共享的资源 ,具有 一次仅允许一个进程使用 的特点n 临界区: 并发进程访问临界资源 的那段 必须互斥执行的程序n 实现临界区互斥一个遵循的准则n 有空让进,临界区空闲时,允许一个进程进入执行n 无空等待,有进程在临界区执行时,要进入的进程必须等待n 让权等待,有进程在临界区执行时,要求进入的进程必须立即释放CP

3、U而等待n 有限等待,不应该使进入临界区的进程无限期地等待在临界区之外n 实现方法:软件方法、硬件方法临界区问题的解决方案 -满足三个基本条件n Mutual Exclusion(互斥条件 ): If process Pi is executing in its CS, then no other processes can be executing in their CSsn Progress(进入条件 ): If no process is executing in its CS and some processes wish to enter their CSs, then only t

4、hose processes that are not executing in their RSs can participate in the decision on which will enter its CS next, and this selection cannot be postponed indefinitely.n Bounded Waiting(有限等待的条件 ): There exists a bound, or limit, on the number of times that other processes are allowed to enter their

5、CSa after a process has made a request to enter its CS and before that request is granted.信号量n 信号量表示资源的物理实体typedef struct int value;/系统初始化时根据代表资源类可用的数量给其赋值struct process *L;/等待使用该资源的进程列表 semaphoren 信号量的操作: P( wait)、 V( signal)n P相当于申请资源n V相当于释放资源n 操作系统利用信号量的状态对进程和资源进行管理,根据用途不同,可以把信号量分为 公用信号量 和 私有信号量

6、n 公用信号量 ,用于解决进程之间 互斥 进入临界区n 私有信号量 ,用于解决异步环境下进程之间的 同步 ,n 利用信号量解决临界区互斥,设置一个公用信号量 Mutext,初始值为 1,任何要使用临界区资源的进程:调用 P(Mutext) ,使用临界区,调用V(Mutex)n 利用信号量和 PV操作实现进程同步,对所有协作关系的并发进程,他们在使用共享资源时必须 互通消息 ,仅当进程收到指定的消息后才能使用共享资源,否则需等待,直到指定的消息到达。经典同步问题n 生产者 -消费者n 同步,生产者将生产的产品放入缓存区,消费者从缓存区取用产品,所以他们要互通消息,生产者放之前要测试缓存区是不是满

7、,消费者在从缓存区取之前要测试是不是为空。n 互斥,任何时候只有一个生产者或者消费者可以访问缓存区n 读者 -写者n 读写互斥访问n 写写互斥访问n 允许多个读者同时读,多个读者共享读者计数器变量,互斥操作n 哲学家就餐读者 -写者(写者优先)n int readcount=0, writecount=0;/读者、写者计数n semaphore rmutex=1, wmutex=1;/读者、写者分别互斥访问 readcount, writecountn semaphore rwmutex=1;/读者、写者互斥访问文件n semaphore r=1;/所有读者排队n semaphore rw=1

8、;/一个读者与一个写者竞争访问文件n /读者n don wait(r);/其他读进程在 r上排队n wait(rw);/一个读进程与一个写进程在 rw上竞争n wait(rmutex);n readcount+;n if(readcount =1) wait(rwmutex);n signal(rmutex);n signal(rw);n signal(r);n 读数据 /CSn wait(rmutex);n readcount-;n if(readcount =0) signal(rwmutex);n signal(rmutex); n while(1);/写者Dowait(wmutex);

9、writecount+;if(writecount=1) wait(rw);/一个写进程在 rw竞争signal(wmutex);wait(rwmutex);/其他写进程在 rwmutex上排队写数据 /CSwait(wmutex);writecount-;if(writecount=0) signal(rw); /都写完通知读进程signal(wmutex); While(1)读者 -写者(两者公平)n int readcount=0;/读者计数n semaphore rmutex=1;/读者互斥访问 readcountn semaphore rwmutex=1;/读者、写者互斥访问文件n

10、semaphore rw=1;/读者与写者竞争访问文件n /读者n don wait(rw);/读进程与写进程在 rw上竞争n wait(rmutex);n readcount+;n if(readcount =1) wait(rwmutex);n signal(rmutex);n signal(rw);n 读数据 /CSn wait(rmutex);n readcount-;n if(readcount =0) signal(rwmutex);n signal(rmutex); n while(1);/写者Dowait(rw);/读者写者竞争wait(rwmutex);写数据 /CSsign

11、al(wmutex); signal(rw); While(1)哲学家就餐n 共享变量n semaphore chopstick5, mutex;/Initially all values are 1n Philosopher i:do wait(mutex);wait(chopsticki)wait(chopstick(i+1) % 5)signal(mutex);eatsignal(chopsticki);signal(chopstick(i+1) % 5);think while (1);Chapter 7n 7.4 The first known correct software so

12、lution to the critical-section problem for two threads was developed by Dekker. The two threads, T0 and T1, share the following variables:Boolean flag2; /* initially false */int turn;The structure of thread Ti (i=0 or 1), with Tj (j=1 or 0) being the other thread, is shown as:do flag i = true;while

13、( flag j )if (turn = j)flag i = false;while (turn = = j);flag i = true;critical sectionturn = j;flag i = false;remainder section while (1);Prove that the algorithm satisfies all three requirements for the critical-section problem.互斥:只能有一个在临界区Pi在临界区, Pj想进,看 flag某进程进入临界区之前, Pi、 Pj都置 flag为 true,看 turn,

14、只有进了的进程退出临界区以后另一个才能进进度:当前没有进程在临界区,只有一个进程试图进,看 flag两个都试图进,看 turn,进了进程在有限时间内复位 flag有限等待:Pi被拒绝进入临界区, Pj已在临界区或者获准进入,当 Pj退出临界区,置 turn为 i,复位 flag, Pi可以进7-cont.n 7.5 The first known correct software solution to the critical-section problem for n processes with a lower bound on waiting of n - 1 turns was pr

15、esented by Eisenberg and McGuire. The processes share the following variables:enum pstate fidle, want in, in csg;pstate flagn;int turn;All the elements of flag are initially idle; the initial value of turn is immaterial (between 0 and n-1). The structure of process Pi is shown as:Prove that the algo

16、rithm satisfies all three requirements for the critical-section problem.7-cont.n 7.7 Show that, if the wait and signal operations are not executed atomically, then mutual exclusion may be violated.7-cont.n 7.8 The Sleeping-Barber Problem. A barbershop consists of a waiting room with n chairs and the

17、 barber room containing the barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop. If the barber is busy but chairs are available, then the customer sits in one of the free ch

18、airs. If the barber is asleep, the customer wakes up the barber. Write a program to coordinate the barber and the customers.n 理发师和顾客同步,理发师必须由顾客唤醒,理发师给一个顾客理发完,要让理发完的顾客退出,让等待顾客进入,顾客互斥的占用 n个位置n /共享变量n semaphore Scuthair, Snumchair;/ Scuthair制约理发师 , Snumchair制约顾客n Scuthair=0; Snumchair=0;n barber:n do n

19、 wait(Scuthair);/检查是否有顾客,无就睡眠n 给某个顾客理发n signal(Snumchair);/让理发完的顾客退出,让等待的一个顾客进入n while (1);n Customer i:n wait(Snumchair);/申请占用椅子n signal(Scuthair);/给理发师发一个信号n 坐在椅子上等着理发n /共享变量n semaphore Scuthair, Mutexchair;/ Scuthair给理发师 , Mutexchair制约顾客对椅子的互斥占领n int number = 0;/顾客的共享变量,记录已经有的顾客数n Scuthair=0; Mut

20、exchair =1;n Customer i:n wait(Mutexchair);/申请对共享变量 number的操作(申请占用椅子)n if(number = = n-1)signal(Mutexchair); exit;n number = number +1;n signal(Scuthair);/给理发师发一个信号n signal(Mutexchair);n 等待理发 n 理发完毕 n wait(Mutexchair);/申请对共享变量 number的操作n number = number -1;n signal(Mutexchair);n 离开理发店n barber:n do n

21、 wait(Scuthair);/检查是否有顾客,无,就睡眠n 给某个顾客理发n while (1);7-cont.n 7.9 The Cigarette-Smokers Problem. Consider a system with three smoker processes and one agent process. Each smoker continuously rolls a cigarette and then smokes it. But to roll and smoke a cigarette, the smoker needs three ingredients: to

22、bacco, paper, and matches. One of the smoker processes has paper, another has tobacco, and the third has matches. The agent has an infinite supply of all three materials. The agent places two of the ingredients on the table. The smoker who has the remaining ingredient then makes and smokes a cigaret

23、te, signaling the agent on completion. The agent then puts out another two of the three ingredients, and the cycle repeats. Write a program to synchronize the agent and the smokers.n 原料供应者和三个吸烟者之间需要同步n /共享变量n semaphore Sa, Ss;/分别控制供应者和吸烟者n Sa = 1; Ss =0;n /供应者n wait(Sa);/桌面上没有原料n 任意丢两样东西到桌面n signal(

24、Ss);/通知吸烟者n /吸烟者n wait(Ss);/查看桌面原料,同时只能有一个吸烟者查看n if(是我需要的两种原料 ) n n 取原料,卷烟,吸烟n signal(Sa);/通知供应者可以放新的原料n n else n n signal(Ss);/让别的吸烟者查看n 吸烟者另解 -更细n /共享变量n semaphore Sa;/控制供应者n semaphore S1, S2, S3;/控制三个吸烟者n Sa = 1; S1 = S2 = S3 = 0;n /供应者n flag f1, f2, f3;/三个标记,分别代表纸,烟草,火柴n wait(Sa);/桌面上没有原料n 任意丢两样

25、东西到桌面n if(f2 /通知有纸的吸烟者n elseif(f1 /通知有烟草的吸烟者n elsen signal(S3); /通知有火柴的吸烟者n /有纸的吸烟者n wait(S1);/等待桌面上有需要的原料n 取烟草,火柴,卷烟,吸烟n signal(Sa);/通知供应者可以放新的原料n /有烟草的吸烟者n wait(S2);/等待桌面上有需要的原料n 取纸,火柴,卷烟,吸烟n signal(Sa);/通知供应者可以放新的原料n /有火柴的吸烟者n wait(S3);/等待桌面上有需要的原料n 取纸,烟草,卷烟,吸烟n signal(Sa);/通知供应者可以放新的原料Chapter 8n

26、 资源是有限的,对资源的需求可能是无限的n 当占有了部分资源而渴求更多的资源的时候,可能会引起deadlock(死锁)n 计算机资源具有两类不同的性质:可抢占和不可抢占n 可抢占资源:当资源从占用进程剥夺走时,对进程不产生破坏性影响, CPU和主存n 不可抢占资源:当资源从占用进程剥夺走时,可能引起进程计算失败,打印机、绘图仪、n 通常,死锁涉及的是不可抢占资源n 死锁:一组进程是死锁的,该组中的每一个进程正在等待这组中其他进程占有的资源时可能引起的一种错误现象。n 死锁产生的原因n 根本原因,系统资源不足n 进程推进顺序不当n 死锁产生的必要条件n 互斥,占有和等待,不可剥夺,循环等待死锁处

27、理策略n 忽略不计n 预防,设法破坏四个必要条件n 避免n 为进程分配资源时,每当完成分配后,立即检查系统是否处于安全状态,若是,分配成功,否则,分配作废,让其阻塞等待n 资源分配图算法、银行家算法n 需要进程对资源需求的先验知识n 设法找到一种安全的进程执行序列n 检测与恢复n 采用资源动态分配算法,当进程请求分配资源时,只要系统有可用资源就满足,系统定期启动死锁检测算法,检查是否有环(进程等待图)Chapter 8n 8.4 Consider the traffic deadlock depicted in following Figure.n a. Show that the four

28、necessary conditions for deadlock indeed hold in this example.n b. State a simple rule that will avoid deadlocks in this system.8 cont.n 8.6 In a real computer system, neither the resources available nor the demands of processes for resources are consistent over long periods (months). Resources brea

29、k or are replaced, new processes come and go, new resources are bought and added to the system. If deadlock is controlled by the bankers algorithm, which of the following changes can be made safely (without introducing the possibility of deadlock), and under what circumstances?n a. Increase Availabl

30、e (new resources added)n b. Decrease Available (resource permanently removed from system)n c. Increase Max for one process (the process needs more resources than allowed, it may want more)n d. Decrease Max for one process (the process decides it does not need that many resources)n e. Increase the nu

31、mber of processesn f. Decrease the number of processes8-cont.n 8.9 Consider a system consisting of m resources of the same type, being shared by n processes. Resources can be requested and released by processes only one at a time. Show that the system is deadlock-free if the following two conditions

32、 hold:n a. The maximum need of each process is between 1 and m resourcesn b. The sum of all maximum needs is less than m + nn 已知:资源 m个,进程 n个,资源的请求和释放一次一个n Maxi 1, m, Maxi m+nn 因为 Needi + Allocationi = Maxin 所以 Needi + Allocationi = Maxi m+nn 假设存在死锁,则n 所有资源分配完成即 Allocationi = mn 由 Needi + Allocationi

33、 m+nn 得 Needi n,故有进程 Pi所需资源为 0n 由于 Maxi 1, m, 所以 Pi已经分配至少一个资源,他执行完以后至少可以释放一个资源,由已知可知资源的请求一次只一个,所以不会存在死锁。8-cont.n 8.13 Consider the following snapshot of a system:n Answer the following questions using the bankers algorithm:n a. What is the content of the matrix Need?n b. Is the system in a safe state?n c. If a request from process P1 arrives for (0,4,2,0), can the request be granted immediately?n a. Need = Max Allocationn 0 0 0 0n 0 7 5 0n 1 0 0 2n 0 0 2 0n 0 6 4 2n b.安全算法找安全序列n c.先请求算法,再安全算法找安全序列,如果安全,请求可以响应。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 中等教育 > 职业教育

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报