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

邹平网站建设优化公司世界网站排名查询

邹平网站建设优化公司,世界网站排名查询,163 com邮箱注册,网站设置主网请使用条件变量实现2生产者2消费者模型&#xff0c;注意1个生产者在生产的时候&#xff0c;另外一个生产者不能生产。 1>程序代码 #include <stdio.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h>…

请使用条件变量实现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.yidumall.com/news/26975.html

相关文章:

  • 广州网站开发债券交淘宝指数网址
  • 网站域名可以做端口映射吗如何免费发布广告
  • 烟台外贸网站建设网站模板
  • dede仿网站目录网络技术推广服务
  • 大气网站特点网络优化是干什么的
  • 山东大型网站建设防疫测温健康码核验一体机
  • 无锡网站设计厂家百度下载app下载
  • 邵阳网站建设公司百度一下你就知道 官网
  • seo建站是什么app平台搭建
  • wordpress文章专辑外贸seo优化
  • 网站排名软件下载最经典最常用的网站推广方式
  • 东莞建设网站费用2024年3月份病毒会爆发吗
  • 有关大数据的网站及网址电子商务网页制作
  • 免费国外代理网页百度百科优化排名
  • 网站建设项目设计报告上海百度seo网站优化
  • 百度网盘官网南京seo优化培训
  • 如何查询网站使用什么框架做的平台推广是什么
  • 乐清定制网站建设长沙百度提升排名
  • vs2017可以做网站吗搜狐财经峰会
  • 马可波罗网站做外贸查关键词
  • 深圳营销型网站公司北京关键词优化服务
  • 后台网站如何建设百度关键字优化精灵
  • 自学网站建设快吗百度北京总部电话
  • 手机配件网站模板seo难不难学
  • 做直播网站需要手续宁波seo外包推广排名
  • 怎么做外贸网站优化网络项目怎么推广
  • 企业网站的建立和推广黄冈网站推广厂家
  • 做精彩网站分析的方向是怎么去做网络推广
  • 与国外公司合作网站建设上海公司全网品牌推广
  • 网站建设电商网站建设黄页视频