件模块,它提供一种互斥机制,进程可以互斥地调用管程的过程。
(1) 共享性; (2) 安全性; (3) 互斥性。
25.试述产生死锁的必要条件、死锁产生的原因及预防死锁的方法。 (1) (2) (3) (4)
互斥条件;
占有和等待条件; 不剥夺条件; 循环等待条件。
进程推进顺序不当、PV操作使用不妥、同类资源分配不均或对某些资源的使用未加限制等,不仅与系统拥有的资源数量有关,而且与资源分配策略、进程对资源的使用要求以及并发进程的推进顺序有关。
(1) 破坏条件1(互斥条件);
(2) 破坏条件2(占有和等待条件); (3) 破坏条件3(不剥夺条件); (4) 破坏条件4(循环等待条件)。
二、 2、
答:不同
(1):初值为1,范围为[-n+1,1];(2):初值为m,范围为[-n+m,m]。 5、答:1) 使用信号量和P、V操作:
var name: array[1..100] of A;
A=record
number:integer; name:string; end
for i:=1 to 100 do {A[i].number:=i; A[i].name:=null;} mutex,seatcount:semaphore; i:integer;mutex:=1;seatcount:=100; cobegin {
process readeri(var readername:string)(i=1,2,…) {
P(seatcount); P(mutex);
for i:=1 to 100 do i++
精选
if A[i].name=null then A[i].name:=readername;
reader get the seat number =i; /*A[i].number V(mutex)
进入阅览室,座位号i,座下读书;
P(mutex);
A[i] name:=null; V(mutex); V(seatcount); 离开阅览室; } } coend.
2) 使用管程操作:
TYPE readbook=monitor VAR R:condition; Interface Module IM; i,seatcount:integer;
name:array[1..100] of string; DEFINE readercome,readerleave; USE check,wait,signal,release;
procedure readercome(readername) begin
check(IM);
if seatcount≥100 wait(R,IM) seatcount:=seatcount+1; for i=1 to 100 do i++
if name[i]==null then name[i]:=readername; get the seat number=i; release(IM); end
procedure readerleave(readername) begin
check(IM); seatcount--;
for i=1 to 100 do i++
if name[i]==readername then name[i]:=null; release(IM); end begin
seatcount:=100;name:=null;
精选
end
cobegin
process readeri(i=1,2.…) begin
readercome(readername); read the book;
readerleave(readername); leave the readroom; end coend.
6、答:实质上是两个进程的同步问题,设信号量S1和S2分别表示可拣白子和黑子,不失一般性,若令先拣白子。
var S1,S2:semaphore;
S1:=1;S2:=0; cobegin {
process P1 begin repeat P(S1); 拣白子
V(S2); until false; end
process P2 begin repeat P(S2); 拣黑子
V(S1); until false;
end } coend.
16、答:(1)用信号量和P、V操作。 var S,S1,S2,S3;semaphore; S:=1;S1:=S2:=S3:=0;
flag1,flag2,flag3:Boolean; flag1:=flag2:=flag3:=true; cobegin
精选
{
process 供应者
begin
repeat P(S);
取两样香烟原料放桌上,由flagi标记; /*flage1、flage2、flage3代表烟草、纸、火柴
if flag2&flag3 then V(S1); /*供纸和火柴 else if flag1&flag3 then V(S2); /*供烟草和火柴 else V(S3); untile false; end
process 吸烟者1
begin
repeat P(S1); 取原料; 做香烟; V(S); 吸香烟; untile false; process 吸烟者2
begin
repeat P(S2);
取原料; 做香烟; V(S); 吸香烟; untile false; process 吸烟者3
begin
repeat P(S3); 取原料; 做香烟; V(S); 吸香烟; untile false; }
coend.
/*供烟草和纸 精选
相关推荐: