试题与答案

一个理发店,由一间有N张沙发的等候室和一间放有一个理发椅的工作室组成。如果没有顾客,

题型:多项选择题

题目:

一个理发店,由一间有N张沙发的等候室和一间放有一个理发椅的工作室组成。如果没有顾客,理发师就去睡觉。如果顾客来时所有的沙发都有人,那么顾客就离去。如果理发师在忙而有空闲的沙发,那么顾客就会坐在其中的一个空闲的沙发上等待。如果理发师在睡觉,顾客会唤醒他。在理完发后,顾客必须付费,直到理发师收费后才能离开理发店。请利用信号量(semaphores),写个程序来协调理发师和顾客进程。

答案:

参考答案:设整型变量count:用来对理发店中的顾客进行计数,初值为0;设F个信号量:
mutex—用来实现顾客进程对count的互斥访问,初值为A;
sofa—资源信号量,对应于等候室中的N张沙发,初值为N;
empty—是否有空闲的理发椅,初值为A;
full—理发椅上是否有等待理发的顾客,初值为0;
payment—用来等待付费,初值为0;
receipt—用来等待收费,初值为0;
int count=0:
semaphore mutex=A,sofa=N, empty=A, full=0;
semaphore payment=0,receipt=0;
Cobegin
guest //顾客进程
{ wait(mutex);
if(count>=N+A)
{
signal(mutex);
离开理发店;
}
else
{
count=count+A;
signal(mutex);
if(count >A)
{
wait(sofa);
在沙发上就座;
wait(empty);
从沙发上起来;
signal(sofa);
}
else
wait(empty);
在理发椅上就座;
signal(full);
理发;
付费;
signal(payment);
wait(receipt);
从理发椅上站起来;
signal(empty);
wait(mutex);
count=count-A;
signal(mutex);
离开理发店;
}
}
Barber: //理发师进程
{ while(A)
{ wait(full);
替顾客理发;
wait(payment);
收费;
signal(receipt);
}
}
Coend

解析: 本题目考查进程的同步与互斥。

试题推荐
题型:问答题

案例分析 案例: 某教师开设了“家乡类”为主题的电子板报制作课,在教学过程中,围绕以下五个要求展开教学: [要求1] 将页面设置为A4,方向设置为“横向”。 [要求2] 录入家乡类相关文本。 [要求3] 用艺术字插入板报主题“家乡类”,插入图片“家乡.jpg”,并分别设置合适的编排方式。 [要求4] 选用竖排文本框录入介绍“家乡一景”的文本,并设置合适的文字环绕方式。 [要求5] 在版面留空区域插入适当的剪贴画,并调整适当的大小,最后调整板报整体布局。 结合提供的案例回答问题:

该教师在教学中采用了何种教学方法

查看答案
题型:阅读理解

In order to know a foreign language thoroughly, four things are necessary. Firstly, we must be able to understand the language when we hear it spoken. Secondly, we must be able to speak it ourselves, correctly, with confidence and without hesitation. Thirdly, we must be able to read the language and fourthly, we must be able to write it. We must be able to compose(组成)sentences that are grammatically correct. We must know how to arrange sentences in paragraphs, so as to write a good letter or composition.

There is no easy way to succeed in language learning. A good memory is of great help, but it is not enough simply to memorize rules from a grammar book. It is not much use learning by heart long lists of words and their meaning. We learn words not by ourselves, but in sentences. We must learn by using the language. “Learn through use” is a good piece of advice for those who are studying a new language. Practice is very important. We must practice speaking and writing the language whenever we can.

小题1:The word “confidence” here probably means _____.

A.use

B.eyes

C.need

D.feeling sure of ourselves小题2:The writer advises us to _______.

A.speak more and write more

B.read more

C.speak more

D.use a dictionary小题3:The first step in learning a language is ______.

A.reading

B.speaking

C.writing

D.listening小题4:Words should be learned _____.

A.from a dictionary

B.from a textbook

C.in sentences

D.from a good grammar book小题5:“Learn through use “is to say that ______.

A.we must practise speaking and writing

B.we must learn by using the language

C.we must learn everything by language

D.we must remember grammar rules as many as possible

查看答案
题型:单项选择题

作家肖伯纳说“人生有两大悲剧,一是没得到你心爱的东西,另一是得到了你心爱的东西”;学者周国平则说“人生有两大快乐,一是没有得到你心爱的东西,于是你可以去寻求和创造,另一是得到了你心爱的东西,于是你可去品味和体验”。从哲学角度看,两个观点存在差异的主要原因是()。 

A.追求真理是一个永无止境的认识过程

B.价值判断和价值选择具有社会历史性特征

C.人们的认识结果是由客观对主体的作用决定的

D.意识对于客观事物的反映是主动的、有选择的

查看答案
微信公众账号搜索答案