两生产者一消费者问题c代码
时间: 2023-10-04 14:09:49 浏览: 43
下面是经典的两生产者一消费者问题的C语言代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define BUFFER_SIZE 10
#define PRODUCER_NUM 2
#define CONSUMER_NUM 1
#define ITEM_NUM 20
int buffer[BUFFER_SIZE];
int count = 0;
int in = 0, out = 0;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t not_full = PTHREAD_COND_INITIALIZER;
pthread_cond_t not_empty = PTHREAD_COND_INITIALIZER;
void *producer(void *arg)
{
int id = *(int *)arg;
int item;
for (int i = 0; i < ITEM_NUM; i++) {
item = rand() % 1000;
pthread_mutex_lock(&lock);
while (count == BUFFER_SIZE) {
printf("Producer %d is waiting because buffer is full.\n", id);
pthread_cond_wait(¬_full, &lock);
}
buffer[in] = item;
printf("Producer %d produces item %d at position %d.\n", id, item, in);
in = (in + 1) % BUFFER_SIZE;
count++;
pthread_cond_signal(¬_empty);
pthread_mutex_unlock(&lock);
}
pthread_exit(NULL);
}
void *consumer(void *arg)
{
int id = *(int *)arg;
int item;
for (int i = 0; i < ITEM_NUM; i++) {
pthread_mutex_lock(&lock);
while (count == 0) {
printf("Consumer %d is waiting because buffer is empty.\n", id);
pthread_cond_wait(¬_empty, &lock);
}
item = buffer[out];
printf("Consumer %d consumes item %d at position %d.\n", id, item, out);
out = (out + 1) % BUFFER_SIZE;
count--;
pthread_cond_signal(¬_full);
pthread_mutex_unlock(&lock);
}
pthread_exit(NULL);
}
int main()
{
pthread_t producers[PRODUCER_NUM], consumer_threads[CONSUMER_NUM];
int producer_ids[PRODUCER_NUM], consumer_ids[CONSUMER_NUM];
for (int i = 0; i < PRODUCER_NUM; i++) {
producer_ids[i] = i;
pthread_create(&producers[i], NULL, producer, &producer_ids[i]);
}
for (int i = 0; i < CONSUMER_NUM; i++) {
consumer_ids[i] = i;
pthread_create(&consumer_threads[i], NULL, consumer, &consumer_ids[i]);
}
for (int i = 0; i < PRODUCER_NUM; i++) {
pthread_join(producers[i], NULL);
}
for (int i = 0; i < CONSUMER_NUM; i++) {
pthread_join(consumer_threads[i], NULL);
}
return 0;
}
```
在程序中,我们定义了一个大小为10的缓冲区`buffer`,并使用一个计数器`count`来记录缓冲区中剩余的数据项数目。两个指针`in`和`out`分别用于指示下一个可写入数据的位置和下一个可读取数据的位置。
我们使用两个条件变量`not_full`和`not_empty`分别表示缓冲区不满和不空。当缓冲区已满时,生产者会进入等待状态,等待消费者信号通知可以继续生产。当缓冲区为空时,消费者会进入等待状态,等待生产者信号通知可以继续消费。
在主函数中,我们创建了两个生产者线程和一个消费者线程,并使用`pthread_create`函数启动线程。在所有线程结束之后,我们使用`pthread_join`函数等待线程结束并释放资源。
以上就是两生产者一消费者问题的C语言代码实现。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)