- 0
- 0
- 约2.49千字
- 约 5页
- 2026-09-06 发布于河北
- 举报
C语言同步互斥问题面试题解析及答案
1.请简述C语言中同步与互斥的核心区别及各自的应用场景,分值5分。
2.下面是一段用POSIX信号量实现生产者-消费者问题的代码片段,请指出其中存在的至少2处逻辑错误,并说明原因。(代码:
includestdio.h
includepthread.h
includesemaphore.h
defineBUFFER_SIZE5
intbuffer[BUFFER_SIZE];
sem_tempty,full,mutex;
voidproducer(voidarg){
while(1){
intitem=rand()%100;
sem_wait(mutex);
sem_wait(empty);
for(inti=0;iBUFFER_SIZE;i++){
if(buffer[i]==0){
buffer[i]=item;
break;
}
}
sem_post(full);
sem_post(mutex);
}
}
voidconsumer(voida
原创力文档

文档评论(0)