1 / 36
文档名称:

homework参考答案.ppt

格式:ppt   大小:374KB   页数:36页
下载后只包含 1 个 PPT 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

homework参考答案.ppt

上传人:在水一方 2019/1/31 文件大小:374 KB

下载得到文件列表

homework参考答案.ppt

相关文档

文档介绍

文档介绍:第二章6、答:设信号量empty用于表示空盘子的数量,信号量apple用于计数,表示盘子中的苹果数目,信号量orange用于计数,表示盘子中的桔子数目。Semaphoreempty=1,apple=0,orange=0dad(){ while(1){ prepareanapple; P(empty); putanappleontheplate; V(apple); }}mom(){while(1){prepareanorange;P(empty);putanorangeontheplate;V(orange);}}第二章son(){ while(1) { P(orange); takeanorangefromtheplate; V(empty); eattheorange; }}daughter(){while(1){ p(apple); takeanapplefromtheplate; v(empty); eattheapple;}}第二章7、答:为了使写者优先,在原来的读优先算法基础上增加一个初值为1的信号量S,使得当至少有一个写者准备访问共享对象时,它可使后续的读者进程等待写完成;初值为0的整型变量writecount,用来对写者进行计数;初值为1的互斥信号量wmutex,用来实现多个写者对writecount的互斥访问。第二章writer(){while(1){P(wmutex);if(writecount==0)P(S);writecount++;V(wmutex);P(mutex);写文件;V(mutex);P(wmutex);writecount--;If(writecount==0)V(S);V(wmutex);}}第二章reader(){while(1){P(S);P(rmutex);if(readcount==0)P(mutex);readcount++;V(rmutex);V(S);读文件;P(rmutex);readcount--;if(readcount==0)V(mutex);V(rmutex);}}第二章9、答:信号量sofa:表示等候椅数,初值为n信号量empty:表示理发椅空,初值为1信号量full:表示理发椅上有顾客,初值为0count:记录等候椅上的人数,初值为0信号量mutux:用来实现对变量count的互斥访问Varmutex,sofa,empty,full:semaphore=1,n,1,0;count:integer:=0;第二章Guest:beginrepeat P(mutex); if(count>=n)then begin V(mutex);离开; endelseBarber:beginrepeat P(full); 剪发; V(empty);untilfalseend第二章begincount:=count+1;if(count>1)then//多个顾客时,坐等候椅上V(mutex);P(sofa);坐沙发等;P(empty);坐椅子上;V(sofa);V(full); else//只有一个顾客时,坐到理发椅上beginV(mutex);P(empty);坐椅子上;V(full);end第二章剪发离开;P(mutex);count:=count-1;V(mutex);enduntilfalseend第二章11、答:本题中中共有三类进程,相当于机房管理员进程guard,学生进程student和教师进程teacher。相应的信号量和各个进程描述如下:puter=2m;/*对应于计算机的资源信号量*/semaphorestudent=0;/*对应于欲进入机房的学生*/semaphoreenter=0;/*用来控制学生是否可进入机房*/semaphorefinish=test=0;/*用来同步学生和教师——教师须检查实****完毕的学生*/