同步互斥问题 - 生产者消费者问题
一、问题描述
有多个进程:多个生产者进程和多个消费者进程共享一个初始为空、固定大小为 n 的缓存(缓冲区)。生产者的工作是制造数据,只有缓冲区没满时,生产者才能把消息放入到缓冲区,否则必须等待; 同时,只有缓冲区不空时,消费者才能从中取出消息,一次消费一段数据(即将其从缓存中移出),否则必须等待。由于缓冲区是临界资源,它只允许一个生产者放入消息,或者一个消费者从中取出消息,同时只允许一个生产者进行制造,多个生产者不能同时制造,也只允许一个消费之进行消费,多个消费者不能同时进行消费。
二、主要问题
- 当缓冲为空的时候,消费者不能消费,必须等待
- 当缓存为满的时候,生产者不能生产,必须等待
- 生产者消费者之间必须互斥
三、实现思路
- 设置三个信号量:empty (以记录有多少空位)、full (以记录有多少满位)以及 mutex (二进制信号量或互斥信号量,以保护对缓冲插入与删除的操作)
- 当信号量 empty 为0的时候,此时有限缓存已满,生产者必须等待,当信号量 full 为0的时候,此时有限缓存为空,消费者无法进行消费,必须等待
- 信号量 mutex 使得各个线程之间互斥,每次只允许一个线程进入临界区,一旦有线程进入自己的临界区,其余线程必须等待
- 有限缓存区为循环队列,尾部插入新的产品,头部产品先被消耗
四、实现代码
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# include <sys/types.h>
# include <pthread.h>
# include <semaphore.h>
# include <string.h>
# include <unistd.h>
#define BUFFER_SIZE 5
typedef int buffer_item;
//semaphores
sem_t empty, full, mutex;
//buffer
buffer_item buffer[BUFFER_SIZE];
int in, out;
//存储数据的结构体
struct data {
int id;
int opTime;
int lastTime;
int productId;
};
//有限缓存插入--生产
int insert_item(buffer_item item) {
/* insert item into buffer */
buffer[out] = item;
out = (out + 1) % BUFFER_SIZE;
return 0;
}
//有限缓存删除--消费
int remove_item(buffer_item *item) {
/* remove an object from buffer and then place it in item */
*item = buffer[in];
in = (in + 1) % BUFFER_SIZE;
return 0;
}
//生产者
void *producer(void* param) {
int productId = ((struct data*)param)->productId;
int lastTime = ((struct data*)param)->lastTime;
int opTime = ((struct data*)param)->opTime;
int id = ((struct data*)param)->id;
free(param);
sleep(opTime);
sem_wait(&empty);
sem_wait(&mutex);
/* critical section */
//add a item
insert_item(productId);
sleep(lastTime);
printf("Thread %d: Producer produce %d\n", id, productId);
sem_post(&mutex);
sem_post(&full);
pthread_exit(0);
}
//消费者
void *consumer(void* param) {
int lastTime = ((struct data*)param)->lastTime;
int opTime = ((struct data*)param)->opTime;
int id = ((struct data*)param)->id;
free(param);
sleep(opTime);
sem_wait(&full);
sem_wait(&mutex);
/* critical section */
//remove a item
buffer_item item;
remove_item(&item);
sleep(lastTime);
printf("Thread %d: Consumer consume %d\n", id, item);
sem_post(&mutex);
sem_post(&empty);
pthread_exit(0);
}
int main() {
//pthread
pthread_t tid; // the thread identifier
pthread_attr_t attr; //set of thread attributes
/* get the default attributes */
pthread_attr_init(&attr);
//initial the semaphores
sem_init(&mutex, 0, 1);
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
in = out = 0;
int id = 0;
while(scanf("%d", &id) != EOF) {
char role; //producer or consumer
int opTime; //operating time
int lastTime; //run time
int productId; //product id
scanf("%c%d%d", &role, &opTime, &lastTime);
struct data* d = (struct data*)malloc(sizeof(struct data));
d->id = id;
d->opTime = opTime;
d->lastTime = lastTime;
if(role == 'P') {
scanf("%d", &productId);
d->productId = productId;
pthread_create(&tid, &attr, producer, d);
}
else if(role == 'C')
pthread_create(&tid, &attr, consumer, d);
}
//释放信号量
sem_destroy(&mutex);
sem_destroy(&empty);
sem_destroy(&full);
return 0;
}
五、测试
测试数据
1 C 3 5
2 P 4 5 1
3 C 5 2
4 C 6 5
5 P 7 3 2
6 P 8 4 3
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具