在有N个缓冲区的生产者消费者的问题中,下列叙述中哪些是错误的?producer() { int item; while(TRUE) { item = produce_item(); P(empty); P(mutex); insert_item(item); V(mutex) V(full); } } consumer() { int item; while(TRUE) { P(full); P(mutex); item = remove_item(); V(mutex); V(mutex); consume_item(item); } }
A.信号量empty的初值为N
B.信号量full的初值为0
C.信号量mutex的初值为0
D.P(full)和P(mutex)两条语句可以颠倒顺序
E.V(mutex)和V(mutex)两条语句可以颠倒顺序
第1题:
阅读以下说明和C程序代码,将应填入______处的语句写在答题纸的对应栏内。
[说明]
函数MultibaseOutput(long n,int B)的功能是:将一个无符号十进制整数n转换成 B(2≤B≤16)进制数并输出。该函数先将转换过程中得到的各位数字入栈,转换结束后再把B进制数从栈中输出。有关栈操作的诸函数功能见相应函数中的注释。C代码中的符号常量及栈的类型定义如下:
define MAXSIZE 32
typedef struct{
int * elem; /* 栈的存储区 */
int max; /* 栈的容量,即栈中最多能存放的元素个数 */
int top; /* 栈顶指针 */
}Stack;
[C代码]
int InitStack(Stack * S,int n) / * 创建容量为n的空栈 */
{ S->elem=(int *)malloc(n * sizeof(int));
if(S->elem==NULL)return-1;
S->max=n; (1)=O;return 0;
}
int Push(Stack * S,int item) / * 将整数item压入栈顶 * /
{ if(S->top==S->max){ printf(“Stack is full! \n”);return-1;}
(2)=item;return 0;
}
int StackEmpty(StackS) {return (! S.top)? 1:0;} / * 判断栈是否为空 * /
int Pop(Stack *S ) / * 栈顶元素出栈 * /
{ if(! S->top){printf(“Pop an empty stack! \n”);return-1;}
return (3);
}
void MultibaseOutput(long n,int B)
{ int m;StackS;
if (InitStack(&S,MAXSIZE)){printf(“Failure! \n”);return;}
do {
if(Push(&S, (4) )){printf(“Failure! \n”);return;}
n=(5);
}while(n!=0);
while(! StackEmpty(S)){ / * 输出B进制的数 * /
m=Pop(&S);
if(m<10)printf(“%d”,m); / * 小于10,输出数字 * /
else printf(“%c”,m+55); / * 大于或等于10,输出相应的字符 * /
}
printf(“\n”);
}
第2题:
#include<iostream.h> void main( ) { int n=9; while(n>6) { n--; cout < < n;} } 该程序的输出结果是
A.987
B.876
C.8765
D.9876
第3题:
试题四
阅读以下说明和C代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
[说明]
函数MultibaseOutput(long n, int B)的功能是:将一个无符号十进制整数n转换成B(2≤B≤16)进制数并输出。该函数先将转换过程中得到的各位数字入栈,转换结束后再把B进制数从栈中输出。有关栈操作的诸函数功能见相应函数中的注释。C代码中的符号常量及栈的类型定义如下:
#define MAXSIZE 32
typedef struct {
int *elem; /* 栈的存储区 */
int max; /* 栈的容量,即栈中最多能存放的元素个数 */
int top; /* 栈顶指针 */
}Stack;
[C代码]
int InitStack(Stack *S, int n) /* 创建容量为n的空栈 */
{ S->elem = (int *)malloc(n * sizeof(int));
if(S->elem == NULL) return -1;
S->max = n; (1) = 0 ; return 0;
}
int Push(Stack *S, int item) /* 将整数item压入栈顶 */
{ if(S->top == S->max){ printf("Stack is full!\n"); return -1;}
(2) = item ; return 0;
}
int StackEmpty(Stack S) { return (!S.top) ? 1 : 0; } /* 判断栈是否为空 */
int Pop(Stack *S) /* 栈顶元素出栈 */
{ if(!S->top) { printf("Pop an empty stack!\n"); return -1;}
return (3) ;
}
void MultibaseOutput(long n, int B)
{ int m; Stack S;
if (InitStack(&S, MAXSIZE)) {printf("Failure!\n"); return;}
do {
if (Push(&S, (4) )) {printf("Failure!\n"); return;}
n = (5) ;
}while(n != 0);
while(!StackEmpty(S)) { /* 输出B进制的数 */
m = Pop(&S);
if(m < 10) printf("%d", m); /* 小于10,输出数字 */
else printf("%c", m + 55); /* 大于或等于10,输出相应的字符 */
}
printf("\n");
}
第4题:
下列程序是死循环的是 ( )
A.for(;;)
B.int s=36; while(s)=s;
C.int m=10; do{m- -;}while(m>0);
D.int n=5; while(n>1) { n- -; if(n<0)break; }
第5题:
下列程序是死循环的是
A.for(;;);
B.int s=36; while(S)--S;
C.int m=10; do { m- - }while(m>0);
D.int n=5; while(n>1) { n- -; if(n<0)break; }
第6题:
下列关于生产者消费者的叙述中,哪一个是正确的?
A.生产者往缓冲区放产品前要先使用P操作确保缓冲区有空闲槽
B.生产者往缓冲区放产品前要先使用V操作确保缓冲区有空闲槽
C.消费者从缓冲区取产品前要先用P操作确保缓冲区互斥使用
D.生产者往缓冲区放产品前要先用P操作确保缓冲区互斥使用
第7题:
A.1
B.6
C.8
D.9
第8题:
●试题一
阅读下列函数说明和C代码,把应填入其中n处的字句写在答卷的对应栏内。
【函数1.1说明】
函数strcpy(char*to,char*from)将字符串from复制到字符串to。
【函数1.1】
void strcpy(char*to,char*from)
{while( ( 1 ) );}
【函数1.2说明】
函数merge(int a[ ],int n,int b[ ],int m,int *c)是将两个从小到大有序数组a和b复制合并出一个有序整数序列c,其中形参n和m分别是数组a和b的元素个数。
【函数1.2】
void merge(int a[ ],int n,int b[ ],int m,int *c)
{ int i,j;
for(i=j=0;i<n && j<m;)
*c++=a[i]<b[j]? a[i++]:b[j++];
while( (2) )*c++=a[i++];
while( (3) )*c++=b[j++];
}
【函数1.3说明】
递归函数sum(int a[ ],int n)的返回值是数组a[ ]的前n个元素之和。
【函数1.3】
int sum(int a[ ],int n)
{ if(n>0)return (4) ;
else (5) ;
}
●试题一
【答案】(1)*to++=*from++ 或 (*to++=*from++)!=′\0′
(2)i<n 或 i!=n(3)j<m 或 j!=m
(4)a[n-1]+sum(a,n-1);或 a[0]+sum(a+1,n-1);(5)return 0;
【解析】(1)完成字符的复制,并使指针指向下一元素。(2)当出现某一数组所有元素均已合并完成,则将另一数组其他元素直接进行复制。b串已复制完。(3)判断a串是否已复制完。(4)递归函数sun()完成数组n个元素的累加,当n>0时,使用规模为n-1的函数进行递归。(5)当n<=0时,返回0即可。
第9题:
本题程序中实现了一个“生产者一消费者问题”。生产者产生一个随机数存入DataPool类中,消费者从中取出数据。DataPool类一次只能存放一个数据。请更正题中带下划线的部分。
注意:不改变程序的结构,不得增行或删行。
class DataPool
{
private int data;
private boolean isFull;
public DataPool()
{
isFull=false;
}
public synchronized void putData(int d)
{
if(isFull= =true)
{
try
{
this.notify();
}
catch(InterruptedException e)
{}
}
data=d;
isFull=true;
System.out.println("生产了一个数据:"+data);
this.notify();
}
public synchronized int getData()
{
if(isFull= =false)
{
try
{
this.wait();
}
catch(InterruptedException e)
{}
}
isFull=false;
System.out.println("消费了一个数据"+data);
this.wait();
return this.data;
}
boolean getIsFull()
{
return isFull;
}
}
class Producer extends Thread
{
DataPool pool;
public Producer(DataPool pool)
{
this.pool=pool;
}
public void run()
{
for(int i=0; i<10; i++)
{
int data=(int) (Math.random()*1000);
try
{//用于生产数据
sleep(data);
}
catch(InterruptedException e)
{}
pool.putData(data);
}
}
}
class Consumer implements Runnable
{
DataPool pool;
public Consumer(DataPool pool)
{
this.pool=pool;
}
public void run()
{
for(int i=0; i<10; i++)
{
int data=pool.getData();
try
{//用于处理数据
sleep((int) (Math.random()*1000));
}
catch(InterruptedException e)
{}
}
}
}
public class advance
}
public static void main(String[] args)
{
Data Pool pool=new Data Pool();
Producer pro=new Producer(pool);
Runnable con=new Consumer(pool);
Thread conTh=new Thread(con);
&n
第10题:
下列函数原型声明中,错误的是
A.int function(int m,int n);
B.int function(int,int);
C.int function(int m=3,int n);
D.int function(int&m,int&n);