问题 问答题

三个进程P1、P2、P3互斥使用一个包含N(N>0)个单元的缓冲区。P1每次用produce()生成一个正整数并用put()送入缓冲区某一空单元中;P2每次用getodd()从该缓冲区中取出一个奇数并用countodd()统计奇数个数;P3每次用geteven()从该缓冲区中取出一个偶数并用counteven()统计偶数个数。请用信号量机制实现这三个进程的同步与互斥活动,并说明所定义信号量的含义。要求用伪代码描述。

答案

参考答案:定义信号量S1控制P1与P2之间的同步;S2控制P1与P3之间的同步;empty控制生产者与消费者之间的同步;mutex控制进程间互斥使用缓冲区。程序如下:
Var S1=0,S2=0,empty=N,mutex=1;
parbegin
p1:Begin
x=produce(); //生成一个数
p(empty); //判断缓冲区是否有空单元
P(mutex); //缓冲区是否被占用
Put();
if x%2=0
v(S2); //如果是偶数,向P3发出信号
else
V(S1); //如果是奇数,向P2发出信号
V(mutex); //使用完缓冲区,释放
End
p2:Begin
P(s1)); //收到P1发来的信号,已产生一个奇数
P(mutex); //缓冲区是否
Getodd();
Countodd():=countodd()+1;
V(mutex); //释放缓冲区
V(empty); //向P1发信号
End
P3:Begin
P(s2); //收到p1发来的信号,已产生一个偶数
P(mutex); //缓冲区是否被占用
Geteven();
Counteven():=COunteven()+1;
V(mutex); //释放缓冲区
V(empty) //向p1发信号,多出一个空单元
End
Parend

阅读理解

They can be seen more frequently than ever before on college campuses, wearing flannel(法兰绒) and thick­framed glasses while listening to music. One might find them playing unusual musical instruments, shopping at second­hand stores or expressing themselves in other unique ways.

They call themselves hipsters. Being “hip” used to mean following the latest fashion. But gradually the word has become a sign of “being cool, unique and unusual”.

Hipsters value independent thinking, progressive politics, an appreciation of creativity and intelligence. Hipsters take pains and pride in not being mainstream. However, their culture has become quite trendy. This irony(讽刺) is central to their culture and offers an interesting conflict.

“I do take things in the mainstream with a positive attitude,” says Ben Polson, a college student at Brown University. “But sometimes I also question what determines popularity, especially regarding music.” Polson represents many hipsters.

When less­known bands become popular, they often lose their former fan base in exchange for a new one. There is a famous hipster saying that goes, “I used to like that band before it got popular,” Polson quoted.

According to Polson, bands' music changes when they go mainstream. They become less experimental, doing things just to save popularity and fans. The original elements that we were drawn to slowly disappear for the sake of popularity.

Many young adults have started to view hipsters' outlook as cool and they are adopting hipsters' anti­mainstream culture mind to themselves. This has led to specialized brands, stores and music for the hipster consumers. But on the other hand, some such stores, including clothing labels Urban Outfitters and American Apparel, have gained mainstream popularity. This has seemingly made the anti­mainstream culture become weaker.

“A lot of people that are self­defined hipsters aren't really hipsters. They're just trying to act as hipsters to seem cooler,” says Amanda Leopold, a college student from Oberlin College, US. Although Leopold has many unusual tastes and seems quite individualist, she refuses to classify herself as a hipster.

小题1:In the passage, hipsters refer to people ________.

A.who are fans of pop music

B.who study in different colleges

C.who appear cool and unusual

D.who try to attract others小题2:According to the author, ________.

A.hipsters are the pioneers of the mainstream

B.hipsters are unhappy with the mainstream

C.hipsters' culture exists mainly in music field

D.hipsters' culture can also become popular小题3:What is the main idea of the seventh paragraph?

A.Urban Outfitters and American Apparel are famous clothing labels.

B.Hipster consumers have more specialized brands, stores and music.

C.Anti­mainstream culture can gain mainstream popularity.

D.The culture of hipsters is becoming more and more terrible.小题4:When describing hipsters, the author speaks ________.

A.in an ironic voice

B.in an objective voice

C.in an angry voice

D.in a funny voice

单项选择题