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

网站布局设计理由跨境电商怎么做

网站布局设计理由,跨境电商怎么做,网站定制报价,cnd设计网是干嘛的请使用条件变量实现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.mmbaike.com/news/73144.html

相关文章:

  • 建设项目环境影响评价公示网站网站优化公司怎么选
  • 阿里云外贸建站卢松松外链工具
  • 用easyui做的网站可以免费投放广告的平台
  • 南山网站设计方案网络营销推广优化
  • 网站框架搭建设计搜狗网站收录
  • 海淀做网站seo数据是什么
  • 重庆市建设施工安全网站网站开发平台有哪些
  • 网站制作真人游戏娱乐平台怎么做武汉网络推广有哪些公司
  • 优斗士网站建设网址浏览大全
  • 联通的网站是谁做的大数据免费查询平台
  • 楼盘怎么在网站上做推广百度热搜榜排名
  • 南京网站建设招聘社交媒体营销三种方式
  • 学校网站建设是什么意思seo实战技巧
  • wordpress+模版+推荐谷歌seo是什么
  • 网站开发员属于网站设计方案
  • 域名备案通过后怎么做网站seo常见的优化技术
  • 阿里有做网站网络营销策划方案论文
  • 手机网站制作服务seo流量是什么意思
  • 郑州高新区做网站开发的公司农夫山泉软文300字
  • 怎么做网站音乐厦门seo报价
  • 做网站平台应该注意哪些百度推广客户端app
  • 公司网站后台维护怎么做衡阳网站建设公司
  • 网站规划和布局厦门网站制作
  • 怎么成立网站陕西网站推广公司
  • 网站建设的中期目标关键词有哪些
  • mastergo网页设计教程搜索引擎优化策略有哪些
  • 郑州的建设网站有哪些手续太原百度推广开户
  • 新开网络游戏排行百度关键词优化手段
  • 网站建设综合技能实训网站友情链接出售
  • 邛崃做网站深圳网站建设公司排名