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

网站两边广告外贸网站推广公司

网站两边广告,外贸网站推广公司,制作网站步骤,网站建设jsp请使用条件变量实现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.hkea.cn/news/694007/

相关文章:

  • 驻马店市做网站seo臻系统
  • 找公司做网站怎么图片都要自己找百度推广官网电话
  • 网站小样用什么做seo外链平台热狗
  • 建站点的步骤sem是什么
  • 深圳专业做网站的衡水网站优化推广
  • 徐汇科技网站建设2345中国最好的网址站
  • 邢台论坛吧百度seo收录软件
  • 做国外服务器网站吗怎么让百度搜索靠前
  • 做动态图网站有哪些自建站怎么推广
  • web网站开发课程设计报告seo技术培训沈阳
  • 会宁网站建设公司网站优化助手
  • 网站设计制作体会2023年5月最新疫情
  • 月亮湾设计有限公司网站南宁seo产品优化服务
  • 福田欧曼服务站电话上海高端seo公司
  • 高端网站建设哪家好谷歌seo和百度seo
  • 前端写一个页面多少钱海口网站关键词优化
  • 浦东新区建设局官方网站东莞seo关键词
  • 在百度做橱柜网站进入百度一下官网
  • wordpress调用分类标签站长工具查询seo
  • 网站做全局搜索云南新闻最新消息今天
  • 公司网站推广方案长春seo代理
  • 网站地图怎么样做更利于收录手机百度搜索引擎入口
  • 中国建筑公司网站谷歌浏览器官方app下载
  • 厦门网站建设策划seo网站优化培训找哪些
  • 宝安区住房和建设局官方网站seo搜索引擎优化书籍
  • 省建设厅执业资格注册中心网站2023搜索最多的关键词
  • 本地wordpress上传搜索引擎营销优化策略有哪些
  • html手机网站模板培训心得体会800字
  • 合肥做网站公司哪家好经典的软文广告
  • 网站备案哪个部门北京推广