鞍山师范学院 操作系统原理 期末试卷(B)评分标准
(2007~2008学年度第2学期)
数学系:计算机科学与技术 专业
写S时,其他进程对S不能读写,但多个进程同时对S读是允许的。试用P、V操作正确实现P1和P2的同步与互斥。要求:不死锁,并发度高。 答案: 算法如下:
semaphore rmutex=1,wmutex=1,count=0; P1( ){ while(1){ P(rmutex); Count=count+1;
If (count==1) then P(wmutex); V(rmutex); 读S; P(rmutex); Count=count-1;
If (count==0) then V(wmutex); V(rmutex); P(wmutex); 写F; V(wmutex); } } P2( ){
while(1){
P(wmutex); 写S; V(wmutex);
第 6 页 共 7 页
鞍山师范学院 操作系统原理 期末试卷(B)评分标准
(2007~2008学年度第2学期)
数学系:计算机科学与技术 专业
} }
第 7 页 共 7 页