当前位置: 首页 > news >正文

02.01 生产者消费者

请使用条件变量实现2生产者2消费者模型,注意1个生产者在生产的时候,另外一个生产者不能生产。

1>程序代码
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;#define BUFFER_SIZE 5
#define PRODUCER_COUNT 2
#define CONSUMER_COUNT 2// 缓冲区
int buffer[BUFFER_SIZE];
// 缓冲区索引
int in = 0;
int out = 0;
// 缓冲区中的元素数量
int count = 0;// 互斥锁和条件变量
pthread_mutex_t mutex;
pthread_cond_t not_full;
pthread_cond_t not_empty;// 生产者互斥锁
pthread_mutex_t producer_mutex;// 生产者线程函数
void *producer(void *arg) {int id = *(int *)arg;while (1) {// 加锁,确保同一时间只有一个生产者可以生产pthread_mutex_lock(&producer_mutex);// 加锁,保护共享资源pthread_mutex_lock(&mutex);// 等待缓冲区有空闲位置while (count == BUFFER_SIZE) {pthread_cond_wait(&not_full, &mutex);}// 生产一个数据buffer[in] = rand() % 100;printf("Producer %d produced %d at position %d\n", id, buffer[in], in);in = (in + 1) % BUFFER_SIZE;count++;// 通知消费者缓冲区有数据了pthread_cond_signal(&not_empty);// 解锁pthread_mutex_unlock(&mutex);// 解锁生产者互斥锁pthread_mutex_unlock(&producer_mutex);// 模拟生产时间sleep(1);}return NULL;
}// 消费者线程函数
void *consumer(void *arg) {int id = *(int *)arg;while (1) {// 加锁,保护共享资源pthread_mutex_lock(&mutex);// 等待缓冲区有数据while (count == 0) {pthread_cond_wait(&not_empty, &mutex);}// 消费一个数据int item = buffer[out];printf("Consumer %d consumed %d from position %d\n", id, item, out);out = (out + 1) % BUFFER_SIZE;count--;// 通知生产者缓冲区有空闲位置了pthread_cond_signal(&not_full);// 解锁pthread_mutex_unlock(&mutex);// 模拟消费时间sleep(1);}return NULL;
}int main(int argc, const char *argv[])
{// 初始化互斥锁和条件变量pthread_mutex_init(&mutex, NULL);pthread_cond_init(&not_full, NULL);pthread_cond_init(&not_empty, NULL);pthread_mutex_init(&producer_mutex, NULL);// 创建生产者和消费者线程pthread_t producers[PRODUCER_COUNT];pthread_t consumers[CONSUMER_COUNT];int producer_ids[PRODUCER_COUNT];int consumer_ids[CONSUMER_COUNT];for (int i = 0; i < PRODUCER_COUNT; i++) {producer_ids[i] = i;pthread_create(&producers[i], NULL, producer, &producer_ids[i]);}for (int i = 0; i < CONSUMER_COUNT; i++) {consumer_ids[i] = i;pthread_create(&consumers[i], NULL, consumer, &consumer_ids[i]);}// 等待线程结束for (int i = 0; i < PRODUCER_COUNT; i++) {pthread_join(producers[i], NULL);}for (int i = 0; i < CONSUMER_COUNT; i++) {pthread_join(consumers[i], NULL);}// 销毁互斥锁和条件变量pthread_mutex_destroy(&mutex);pthread_cond_destroy(&not_full);pthread_cond_destroy(&not_empty);pthread_mutex_destroy(&producer_mutex);return 0;
}
2>运行效果
http://www.lryc.cn/news/529975.html

相关文章:

  • mac 手工安装OpenSSL 3.4.0
  • kamailio-ACC_JSON模块详解【后端语言go】
  • ArkTS语言介绍
  • 海外问卷调查之渠道查,企业经营的指南针
  • spring和Mybatis的逆向工程
  • 【Android】问deepseek存储访问
  • Android记事本App设计开发项目实战教程2025最新版Android Studio
  • python学习——函数的返回值
  • 【竞技宝】裂变天地S1:BB0-2PARI淘汰出局
  • 数据分析系列--⑨RapidMiner训练集、测试集、验证集划分
  • 实践Rust:编写一个猜数字游戏
  • JavaFX - 3D 形状
  • 阿里新发的大模型Qwen2.5-max如何?
  • 文本复制兼容方案最佳实现落地。
  • x86-64数据传输指令
  • LigerUI在MVC模式下的响应原则
  • java CountDownLatch和CyclicBarrier
  • 力扣动态规划-17【算法学习day.111】
  • 读书笔记-《你的灯亮着吗?》
  • MATLAB实现多种群遗传算法
  • tf.Keras (tf-1.15)使用记录3-model.compile方法
  • Prometheus 中的 Exporter
  • 网工_HDLC协议
  • leetcode 2563. 统计公平数对的数目
  • Debian 10 中 Linux 4.19 内核在 x86_64 架构上对中断嵌套的支持情况
  • FLTK - FLTK1.4.1 - demo - bitmap
  • 数据结构 树1
  • android主题设置为..DarkActionBar.Bridge时自定义DatePicker选中日期颜色
  • MySQL 如何深度分页问题
  • 1.攻防世界easyphp