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

asp网站程序优点正定城乡建设网站

asp网站程序优点,正定城乡建设网站,哪个网站教做ppt模板,陕西网站建设价格请使用条件变量实现2生产者2消费者模型#xff0c;注意1个生产者在生产的时候#xff0c;另外一个生产者不能生产。 1程序代码 #include stdio.h #include string.h #include unistd.h #include stdlib.h #include sys/types.h注意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.htypedef 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.w-s-a.com/news/262158/

相关文章:

  • 钟表东莞网站建设石家庄做网站时光
  • 织梦 图片网站源码成都建设工程安监局网站
  • 做兼职的网站策划书湖北省建设工程造价信息网
  • 企业网站网址长期做网站应该购买稳定的空间
  • 网站静态化设计html5手机网站制作
  • 深圳最简单的网站建设家居网站建设全网营销
  • 如何取消网站备案佛山网站优化公司
  • 网站开发 成都广水网站设计
  • 音乐网站建设目标合同管理系统
  • jq网站特效插件如何知道网站是否被k
  • 自己的网站怎么接广告网站搭建收费
  • 宁波大型网站制作建立一个网站 优帮云
  • 大连零基础网站建设教学电话有哪些比较好的做ppt好的网站
  • 哪个网站做logo设计我的建筑网
  • php电子商务网站开发沂源手机网站建设公司
  • html和php做网站哪个好3gcms企业手机网站整站源码asp
  • 网站建设网页设计案例云南建设厅网站删除
  • 杏坛网站制作太原做网站要多少钱呢
  • 做新闻类网站还有市场吗东莞黄页网广告
  • 地方网站做外卖专业做互联网招聘的网站有哪些
  • 网站推广公司兴田德润紧急网页升级紧急通知
  • 厦门做网站哪家强企业网站网页设计的步骤
  • 普拓网站建设济南行业网站建设
  • 燕郊 网站开发网站里的地图定位怎么做
  • 门户网站建设招标互联网创业项目概述
  • 用什么做网站比较好市场调研公司是做什么的
  • 电商网站充值消费系统绍兴网站优化
  • 深圳网站建设公司联虚拟币交易网站开发
  • 专业网站设计建设公司抖音代运营公司排名前十强
  • 做网站架构肃北蒙古族自治县建设局网站