问题 问答题

考虑有三个吸烟者进程和一个经销商进程的系统。每个吸烟者连续不断地做烟卷并抽他做好的烟卷。做一个烟卷需要烟草、纸和火柴三种原料。这三个吸烟者分别掌握有烟草、纸和火柴。经销商源源不断地提供上述三种原料,但他只将其中的两种原料放在桌子上,具有另一种原料的吸烟者就可做烟卷并抽烟,且在做完后给经销商发信号,然后经销商再拿出两种原料放在桌上,如此反复。试设计一个使经销商和抽烟者同步的算法。

答案

参考答案:本题是一个有限缓冲区的生产者消费者问题,关键是找到缓冲区资源,以及谁是生产者、谁是消费者。
本题中烟草、纸和火柴应该看作是产品,桌子是缓冲区。问题是有几种产品。烟草、纸和火柴三种原料又不能简单地看成是三种产品,因为它们并不是以单独的形式被三个吸烟者进程所竞争的,而是以固定的组合被三个进程所申请的。因此可以考虑:设置三个信号量r、s和t,分别代表三种原料组合,即r表示烟草和纸,s表示纸和火柴,t表示烟草和火柴,初值均为0;桌面上一次只能放一种组合,可以看作是放一个产品的缓冲区,设置信号量empty初值为1,控制经销商往桌子上放原料;对于三个吸烟者的申请动作也要加以判断,用三个变量smoker1、smoker2、smoker3,初值为false,当为true时,表示申请资源,得到资源后置为false。四个进程循环往复,并发执行。
parbegin
经销商进程:
Begin
P(empty);
if smoker1=true then Begin
放烟草和纸;
V(r);
End
if smoker2=true then Begin
放纸和火柴;
V(s);
End
if smoker3=true then Begin
放烟草和火柴;
V(t);
End
End
吸烟者1进程:
Begin
smoker1:=true;
P(r);
取走原料;
smoker1:=false;
V(empty);
Smoking;
End
吸烟者2进程:
Begin
smoker2:=true;
P(s);
取走原料;
smoker2:=false;
V(empty);
Smoking;
End
吸烟者3进程:
Begin
smoker3:=true;
P(t);
取走原料;
smoker3:=false;
V(empty);
Smoking;
End
Parend

阅读理解

阅读短文, 选择最佳答案。

       An old tiger does not want to look for food now. He often tells other animals to bring him something to eat.

He sees a monkey and says, "I am hungry, monkey. Get to the village and bring me a fat pig." "Oh, tiger," says 

the monkey, "I can not do that now. There is another tiger over there. He also wants a fat pig. He will not let me

get anything for you to eat. I am afraid of him." "What ?" cries the old tiger. "Show me that tiger. I will talk to 

him." "Come with me." says the monkey. The  monkey and the tiger get to a bridge over the river. "Now look 

down at the river." says the monkey. "Do you see the head ? the white teeth and the large green eyes of a tiger?"

"Yes, I do." cries the old tiger, "I will eat him up!" with these words, the tiger jumps into the water.

1. The older tiger wants               .              [ ]

A. a big pig                

B. a thin pig               

C. a fat pig

2. The monkey is afraid               the tiger.[ ]

A. from                         

B. of                            

C. about

3. The monkey and the tiger get             the bridge             the river.[ ]

A. to, over                 

B. on, on                    

C. in, in

4.              these words, the tiger eats him up.[ ]

A. Under                       

B. With                      

C. Over

5. The tiger jumps             the water.[ ]

A. in                                 

B. into                              

C. on

填空题