应用题
5.
(1)使用P,V操作与信号量 struct {
char name[10] ; int number; }Seat[100];
semaphore mutex,numOfSeat; int i;mutex=1; numOfSeat =100; for(int i=0;i<100;i++ ) {
A[i].number=i; A[i].name=null; } cobegin
process Reader_i(char name[ ]) {
P(numOfSeat); P(mutex);
for (int i=1; i< 100 ;i++)
{
if (Seat[i].name==null )
Seat [i].name=name;
}
V(mutex);
sit(Seat[i].number);
P(mutex);
Seat[i].name=null; V(mutex); V(numOfSeat); } coend.
(2)使用管程
type readbook=MONTOR { semaphore R;
int R_count, i, numOfSeat; char name[100] ; numOfSeat =0; InterfaceModule IM;
DEFINE readbook( ), readerleave ( );
USE enter(), leave(),wait( ), signal( ); void readercome(char readerName[ ]) { enter (IM);
if (numOfSeat >=100) wait(R,R_count,IM); numOfSeat = numOfSeat +1; for (int i=0;i<100;i++) {
if (name[i]==null ) name[i]=readerName; } sit(i); leave(IM ); }
void readerleave(char readerName) { enter(IM); numOfSeat --; for(int i=0;i<100;i++)
if (name[i]==readerName) name[i]=null; signal(R,R_count,IM); leave(IM); } cobegin
process Reader_ i ( ) {
readbook.readercome(readerName); read();
readbook.readerleave(readerName); leaveReadroom(); } coend
29.
(1)系统处于安全状态,存在安全序列:P0,P3,P4,P1,P2。
(2)不能分配,否则系统会处于不安全状态。若执行P2 发出的request1(1, 2, 2, 2),此时available=(0, 4, 0, 0),不能满足后续分配。
46.
(1)使用P,V操作与信号量 int buffer[9];
int count,getptr,putptr; count=0;getpt=0;putpt=0; semaphore S1,S2,SPUT,SGET; S1=1;S2=1;SPUT=1;SGET=0; cobegin Producei() {
Consumei() {
while(true) { produce(); P(SPUT); P(S1);
saveint(buffer [putptr]); putptr=(putptr+1) % 9; saveint(buffer [putptr]); putptr=(putptr+1) % 9; saveint(buffer [putptr]); putptr=(putptr+1) % 9; V(SGET); V(SGET); V(SGET); V(S1); } }
coend (2)使用管程
TYPE get_put = MONITOR int buffer[9];
int count,getptr,putptr; semaphore SP,SG;
int y; while(true) { P(SGET); P(S2);
y=buffer[getptr]; getptr=(getptr+1) % 9; count++; if (count==3) { count=0; V(SPUT);
} V(S2); consume(y);
}
}
int SP_count,SG_count; count=0;getptr=0;putptr=0; InterfaceModule IM; DEFINE put, get;
USE wait, signal, enter, leave;
void put(int a1,int a2,int a3) { enter(IM);
if (count>6) wait(SP,SP_count,IM); count=count+3; buffer[putptr]=a1; putptr=(putptr+1) % 9; buffer[putptr]=a2; putptr=(putptr+1) % 9; buffer[putptr]=a3; putptr=(putptr+1) % 9; signal(SG,SG_count,IM); leave(IM); }
void get(int b) { enter(IM);
if (count= =0)
wait(SG,SG_count,IM); b=buffer[getptr]; getptr=(getptr+1) % 9; count++; if (count < 7)
signal(SP,SP_count, IM); else if (count > 0) signal(SG,SG_count,IM); leave(IM); } cobegin
process Producei( ) { while(true) {
produce();
get-put.put(a1,a2,a3); } }
coend
process Consumeri( ) { while(true) { { get-put.get(b); consume(b);
} }
54.
(1)使用P,V操作与信号量
var semaphore S,Sa,Sb,mutexa,mutexb; S=mutexa=mutexb=1;Sa=Sb=0; enum {paper,pen} box; cobegin
process Storekeeper ( ) { while(true) {
P(S);
{take a material into box}; if((box)==paper)
V(Sa); else
V(Sb); } }
process Ai( ) { while(true) {
P(Sa); P(mutexa);
{take the pen from box}; V(mutexa);
V(S);
{write a letter}; } }
process Bi( ) { while(true) {
P(Sb); P(mutexb);
{take the paper from box}; V(mutexb); V(S);
{write a letter}; } } coend
(2)使用管程
TYPE paper&pen=monitor semaphore S,S1,S2;
int S_count,S1_count,S2_count; enum box {paper,pen}; box=null; InterfaceModule IM;
DEFINE put,get1,get2; USE enter,wait,signal,leave; void put( ) { enter(IM); {take a material}; if((box)!=null)
wait(S,S_count,IM); else
box=material; if ((box)==pen)
signal(S1,S1_count,IM); else
signal(S2,S2_count,IM); leave(IM); }
void get1( ) { enter(IM);
if ((box)==null) ‖ (box)!=pen)
wait(S1,S1_count,IM); else
{ take pen from box}; signal(S,S_count,IM);
leave(IM); }
void get2( ) { enter(IM);
if((box)==null ‖ (box)!=paper)
wait(S2,S2_count,IM); else
{ take the paper from box;} signal(S,S_count,IM); leave (IM); } cobegin
process Storekeeper( ) { while(true) {
paper&pen.put( ); }
process Ai( ) { while(true) {
paper&pen.get( ); {write a letter};
} }
process Bi( ) { while(true) {
paper&pen.get( ) {write a letter}; } } coend
59.
(1)使用P,V操作与信号量 box1[N/5]; box2[4*N/5] ;
semaphore mutex1,mutex2,S1,S2,S3,S4; int counter,in1,in2,out1,out2; S1=N/5;S2=4N/5;S3=S4=0; counter=in1=in2=out1=out2=0; mutex1=mutex2=1; cobegin
process worker1( ) {
while(true) { {make a carriage;} P(S1); P(mutex1);
{put carriage in box1(in1)}; in1=(in1+1) % (N/5); V(mutex1); V(S3); } }
process worker2( ) { while(true) { {make a wheel} P(S2); P(mutex2);
{put wheel in box2(in2)}; in2=(in2+1) % (4*N/5); counter=counter+1;
if(counter==4) {counter=0;V(S4);} V(mutex2); } }
process worker3( ) { while(true) { P(S3); P(mutex1);
{take carriage from box1(out1);} out1=(out1+1) % (N/5); V(mutex1) V(S1); P(S4); P(mutex2);
{take wheel from box2(out2);} out2=(out2+1) % (4*N/5); {take wheel from box2(out2);} out2=(out2+1) % (4*N/5); {take wheel from box2(out2);} out2=(out2+1) % (4*N/5); {take wheel from box2(out2);} out2=(out2+1) % (4*N/5); V(S2); V(mutex2); {fix up car}; }
} coend (2)使用管程
type produce_toy_car=monitor box1[N/5]; box2[4*N/5];
semaphore S1,S2,S3,S4;
int S1_count,S2_count,S3_count,S4_count; int counter1,counter2,count,in1,in2,out1,out2; counter1=counter2=count=in1=in2=out1=out2=0; InterfaceModule IM; DEFINE put1,put2,take; USE wait,signal,enter,leave; void put1( ) { enter(IM);
if(counter1==N/5) wait(S1,S1_count,IM); {put carriage in box1(in1)}; in1=(in1+1) % (N/5); counter1=counter1+1; signal(S3,S3_count,IM); leave(IM); }
void put2( ) { enter(IM);
if(counter2==(4*N/5)) wait(S2,S2_count,IM); {put wheel in box2(in2);} in2=(in2+1) % (4*N/5); counter2=counter2+1; count=count+1;
if(count==4) {count=0; signal(S4,S4_count,IM);} leave(IM); }
void take( ) { enter(IM);
if(counter1==0) wait(S3,S3_count,IM); {take carriage from box1(out1);} out1=(out1+1) % (N/5); counter1=counter1-1;
if (counter2<4) wait(S4,S4_count,IM); {take carriage from box2(out2);} out2=(out2+1) % (4*N/5); {take wheel from box2(out2);} out2=(out2+1) % (4*N/5); {take wheel from box2(out2);}
out2=(out2+1) % (4*N/5); {take wheel from box2(out2);} out2=(out2+1) % (4*N/5); counter2=counter2-4; signal(S1,S1_count,IM); signal(S2,S2_count,IM); leave(IM); }
因篇幅问题不能全部显示,请点此查看更多更全内容