习题课:信号量
来源:互联网 发布:php b2c商城 编辑:程序博客网 时间:2024/06/06 16:53
1.某宾馆门前有一出租车停车位,假设宾馆每一位顾客出门都要乘坐出租车,并且对顾客约定:如果有其它顾客在此停车位等车则在旁等待;否则在此等车;此停车位有车则乘坐。对出租车做出约定:如果此停车位已停有车,则等待此停车位空闲;否则停到此停车位等待顾客;有顾客则搭载顾客离开。试用P、V原语编写程序描述顾客与出租车的行为。
CSemaphore PositionForCustomer=1;Semaphore PositionForTax=1;Semaphore Tax=0;Semaphore Customer=0;Void Customer(void){ V (Customer); P (PositionForCustomer); Waiting for a tax in the stopping position(); P (Tax); Go away by tax(); V (PositionForCustomer)}Void Tax(void){ V (Tax); P (PositionForTax); Waiting for a customer in the stopping position(); P (Customer); Go away with customer(); V (PositionForTax)}
2.【看电影买票问题】有3个网友,未曾谋面,他们相约去看电影,费用AA制,条件是3个人必须都到电影院的时候才能买票进入,如果缺一个人,就害怕有危险,取消活动。试用信号量和P、V操作描述3个人的行为。
semaphore S12=0; //表示第1个人告知第2个人自己已到达semaphore S13=0; //表示第1个人告知第3个人自己已到达semaphore S21=0; //表示第2个人告知第1个人自己已到达semaphore S23=0; //表示第2个人告知第3个人自己已到达semaphore S31=0; //表示第3个人告知第1个人自己已到达semaphore S32=0; //表示第3个人告知第2个人自己已到达person1(){ V(S12); V(S13); P(S21); P(S31); 买票;}person2(){ V(S21); V(S23); P(S12); P(S32); 买票;}person3(){ V(S31); V(S32); P(S13); P(S23); 买票;}
3.有四个进程R1,R2,W1,W2,它们共享可以存放一个数据的缓冲区。进程R1每次把从键盘上读入的一个数据存到该缓冲区中,供另一个进程W1打印输出;进程R2每次从磁盘上读一个数据存放到该缓冲区中,提供给W2打印输出。当一个进程把数存放到缓冲区后,在该数还没有被打印输出之前不准任何进程再向缓冲区中存数。当一个进程已把缓冲区中的数打印输出后,在缓冲区中还没有存入一个新的数据之前不准任何进程再从缓冲区中取数打印。用P, V操作来协调它们的工作。
C++Semaphore empty=1;Semaphore data1=0;Semaphore data2=0;Void Process-R1(void){ Get a data(); P (empty); Put the data into the buffer(); V (data1);}Void Process-R2(void){ Get a data(); P (empty); Put the data into the buffer(); V (data2);}Void Process-w1(void){ P (data1); Get the data from the buffer(); V (empty);}Void Process-w2(void){ P (data2); Get the data from the buffer(); V (empty);}
4.设公共汽车上,司机活动是:启动车辆、正常行车、到站停车;售票员的活动是:关车门、售票、开车门。在汽车不断地到站、停站、行驶过程中,这两个活动有什么同步关系?用信号量的P、V操作实现它们的同步。
semaphore s1=0;semaphore s2=0;main(){ driver(); busman();}driver(){ while (true) { P (s1); 启动车辆; 正常行车; 到站停车; V (s2); }}busman(){ while (true) { 关车门; V (s1); 售票; P (s2); 开车门; }}
5.某寺庙,有小、老和尚若干,有一缸水,有小和尚提水入缸供老和尚饮用。水缸可容10桶水,水取自同一井中。水井颈窄,每次只能容一个桶取水。水桶总数为3个。每次入、取缸水仅为1桶,且不可同时进行。试用P、V操作给出取水、入水的算法描述。
Semaphore mutex1=1;Semaphore mutex2=1;Semaphore empty=10;Semaphore full=0;Semaphore count=3;main(){ get( ); use( );}get( ){ while (true) { P (empty); P (count); P (mutex1); 从井中取水; V (mutex1); P (mutex2); 将水倒入水缸; V (mutex2); V (count); V (full); }}use ( ){ while (true) { P (full); P (count); P (mutex2); 从缸中取水; V (mutex2); V (empty); V (count); }}
6.【桔子苹果问题】桌上有一空盘,只允许存放一个水果。爸爸可向盘中放苹果,也可向盘中放桔子,儿子专等吃盘中的桔子,女儿专等吃盘中的苹果。规定当盘空时一次只能放一只水果供吃者取用,请用P、V操作实现爸爸、儿子、女儿三个并发进程的同步。
semaphore S1=1;semaphore S2=0;semaphore S3=0;father(){ while(1) { P(S1); if (put an apple) V(S2); else V(S3); } }daughter( ) { while(1) { P(S2); Take the apple; V(S1); } }son( ) { while(1) { P(S3); Take the orange; V(S1); } }
- 习题课:信号量
- 信号量
- 信号量
- 信号量
- 信号量
- 信号量
- 信号量
- 信号量
- 信号量
- 信号量
- 信号量
- 信号量
- 信号量
- 信号量
- 信号量
- 信号量
- <>信号量
- 信号量
- nginx代理后获取用户ip
- [Unity]怎么让skybox天空盒和主摄影机变黑
- SICP-2.3-Trees
- C# 关于控件Visible为false 却依然显示的问题
- 在python2和3共存的情况下为python3安装numpy、scipy、matplotlib
- 习题课:信号量
- 常用Gulp插件
- Android流媒体框架的AHandler消息机制分析
- 概述网站文章优化五个操作重心
- A Context-aware Natural Language Generator for Dialogue Systems
- hadoop 安装时 出现 gzip: stdin: unexpected end of file错误
- .net 本地文件上传至服务器
- HashMap和Hashtable的区别
- 事务注解