《青岛理工大学操作系统作业答案资料》由会员分享,可在线阅读,更多相关《青岛理工大学操作系统作业答案资料(12页珍藏版)》请在金锄头文库上搜索。
1、第1章 作业综合题1、设内存中有三道程序A、B、C,它们按A、B、C的优先次序执行。它们的计算和I/O操作时间如表所示(单位:ms)。三道程序的操作时间 程序操作ABC计算306020I/O403040计算101020假设三道程序使用相同设备I/O操作,即程序是以串行方式使用设备,调度程序的执行时间忽略不计,试计算出在单道和多道两种情况下,完成这三道程序各要花多少时间?要求画出多道运行的时序图。(假定在多道方式下采用的是基于优先级的非抢占调度程序)解:采用单道方式运行这三道程序,运行次序为A、B、C,故总的运行时间为:(30+40+10)+(60+30+10)+(20+40+20)=260ms
2、 采用多道方式运行这三道程序,A、B、C这三道进程的运行存在并行,故总的运行时间如图所示为180ms第二章1、如图所示,有一计算进程和一打印进程,它们共享一个单缓冲区,计算进程不断地计算出结果并将它放入单缓冲区中,打印进程则负责从单缓冲区中取出每一个结果进行打印。请用信号量来实现它们的同步关系。答:方法一:从临界资源的角度来思考:本题中有两类临界资源:第一类是计算进程争用的空闲缓冲区,初始状态下有一个空闲缓冲可供使用,设置信号量empty,初值为1;第二类是打印进程争用的已放入缓冲区中的打印结果,初始状态下缓冲区中无结果可打印,设置信号量full,初值为0。var full, empty: s
3、emaphore:=0,1;begin parbegin cp:begin repeat computer next number; wait(empty); add the number to buffer; signal(full); until false end pp:begin repeat wait(full); take a number from buffer; signal(empty); print the number; until false end parendend2、试用信号量解决读者写者问题,使得写者与读者优先级根据到达顺序确定(读写平等)。然后用到达序列:R1
4、, R2, W1, R3, R4, W2进行测试列出类似如下测试结果进程行为rmutex=1wmutex=1Readcount=0状态备注R1到达rmutex=0rmutex=1wmutex=0Readcount=1执行/就绪第1位读者1)典型错误代码讲解:不增加任何信号量Var rmutex, wmutex:semaphore=1,1; Readcount:integer =0; begin parbegin Reader:begin repeat wait(rmutex); if Readcount=0 then wait(wmutex); Readcount = Readcount+1;
5、 signal(rmutex); perform read operation; wait(rmutex); Readcount= Readcount-1; if Readcount=0 then signal(wmutex); signal(rmutex); until false; end writer:begin repeat if readcount0 then wait(rumtex); wait(wmutex); perform write operation; signal(rmutex); signal(wmutex); until false; end parend end到
6、达序列:R1, R2, W1, R3, R4, W2进程行为rmutex=1wmutex=1Readcount=0状态备注R1到达rmutex=0rmutex=1wmutex=0Readcount=1执行/就绪第1位读者R2到达rmutex=0rmutex=1Readcount=2执行/就绪W1到达rmutex=0阻塞1阻塞Readcount0R3到达阻塞1阻塞rmutex=0R4到达阻塞2阻塞rmutex=0W2到达阻塞3阻塞rmutex=0R1离开阻塞4阻塞rmutex=0R2离开阻塞5阻塞rmutex=0产生死锁2)解决方案 var S, rmutex, wmutex: semaphor
7、e:=1, 1,1; readcount: integer:= 0;reader: begin repeat wait(S); wait(rmutex); if readcount=0 then wait(wmutex); readcount:=readcount+1; signal(rmutex); signal(S); perform read operation; wait(rmutex); readcount:=readcount-1; if readcount=0 then signal(wmutex); signal(rmutex); until false end writer:
8、 begin repeat wait(S); wait(wmutex); perform write operation; signal(wmutex); signal(S); until false end到达序列:R1, R2, W1, R3, R4, W2进程行为S=1rmutex=1wmutex=1readcount=0备注R1到达S=0S=1rmutex=0rmutex=1wmutex=0readcount=1第一个读者执行/就绪R2到达S=0S=1rmutex=0rmutex=1readcount=2执行/就绪W1到达S=0阻塞1第一个写者R3到达阻塞1R4到达阻塞2W2到达阻塞3
9、R1离开rmutex=0rmutex=1readcount=1R2离开rmutex=0rmutex=1wmutex=1readcount=0负责唤醒W1W1被唤醒wmutex=0执行/就绪W1离开S=1wmutex=1负责唤醒R33、请给出一个写者优先的“读者写者”问题的算法描述。然后用到达序列:R1, R2, W1, R3, R4, W2进行测试列出类似如下测试结果进程行为rmutex=1wmutex=1Readcount=0状态备注R1到达rmutex=0rmutex=1wmutex=0Readcount=1执行/就绪第1位读者答:为使写者优先,可在原来的读优先算法基础上增加一个初值为1的
10、信号量S,使得当至少有一个写者准备访问共享对象时,它可使后续的读者进程等待写完成。初值为0的整型变量writecount用来对写者进行计数;初值为1 的互斥信号量mutex用来实现多个写者对writecount的互斥访问。读者与写者进程算法描述如下: var S, mutex, rmutex, wmutex: semaphore:=1,1, 1,1; writecount, readcount: integer:=0,0;reader: begin repeat wait(S); wait(rmutex); if readcount=0 then wait(wmutex); readcount
11、:=readcount+1; signal(rmutex); signal(S); perform read operation; wait(rmutex); readcount:=readcount-1; if readcount=0 then signal(wmutex); signal(rmutex); until false end writer: begin repeat wait(mutex); if writecount=0 then wait(S); writecount:=writecount+1; signal(mutex); wait(wmutex); perform write operation; signal(wmutex); wait(mutex); writecount:=writecount-1; if writecount=0 then signal(S); signal(mutex); until false end到达序列:R1, R2, W1, R3, R4, W2进程行为S=1mutex=1rmutex=1wmutex=1writecount=0readcount=0备注R1到达S=0S=