欢迎来到天天文库
浏览记录
ID:6666258
大小:29.50 KB
页数:3页
时间:2018-01-21
《操作系统——进程同步练习》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1.请用信号量机制写出算法解决下列"过独木桥"问题:同一方向的人可以连续过桥,当某一方向有人过桥时,另一方向的人必须等待;当某一方向无人过桥时,另一方向的行人可以过桥。解:semaphoremutex,leftm,rightm;mutex.value=1;leftm.value=1;rightm.value=1;intcountleft,countright;countleft=0;countright=0;voidLeftToRight(){wait(leftm);if(countleft==0)thenwait(mutex);countleft++;signal(leftm);通过独木
2、桥;wait(leftm);countleft--;if(countleft==0)thensignal(mutex);signal(leftm);}2.一家四口父亲、母亲、儿子、女儿,现有一个盘子,父亲只往盘子里放苹果,母亲只往盘子里放桔子,儿子只吃桔子,女儿只吃苹果。盘子中只能放一只水果。请用信号量机制分别为父亲、母亲、儿子、女儿编写程序,使他们不发生冲突。解:semaphoremutex,apple,orage,empty;mutex.value=1;apple.value=0;orage.value=0;empty.value=1;Voidfather(){Wait(empty);
3、wait(mutex);putanappleonthedisk;signal(mutex);signal(apple);}Voidmather(){Wait(empty);wait(mutex);putanorangeonthedisk;signal(mutex);signal(orange);}Voidson(){Wait(apple);Wait(mutex);Getanapplefromdisk;Signal(mutex);Signal(empty);}Voiddauter(){Wait(orange);Wait(mutex);Getanorangefromdisk;Signal(mu
4、tex);Signal(empty);}
此文档下载收益归作者所有