为了账号安全,请及时绑定邮箱和手机立即绑定

作业社区

探索学习新天地,共享知识资源!

0 提交作业
0 布置作业
0 满分作业
得分 100
学习任务

cjozGV 的学生作业:

#include // printf, fprintf #include // atoi, exit #include // bool 类型 #include // POSIX 线程库 #include // usleep static int number = 0; // 当前库存数量 (共享变量) static int total_of_produce = 0; // 以生产总数 static int total_of_consume = 0; // 以消费总数 pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; // 互斥锁 pthread_cond_t cond = PTHREAD_COND_INITIALIZER; // 条件变量 消费者 0 库存等待 生产者生产后唤醒 void *producer(void *arg){ int cnt = atoi((char*)arg); // 从命令行获取生产数量 for (int i = 0; i < cnt;i++) { pthread_mutex_lock(&mtx); // 加锁进入临界区 number++; // 库存+1 total_of_produce++; // 总消费数+1 printf("生产者 [%lu] 生产一个产品,库存=%d\n",(unsigned long)pthread_self(),number); pthread_cond_broadcast(&cond); // 唤醒所有等待的消费者线程 pthread_mutex_unlock(&mtx); // 解锁退出临界区 usleep(100000); // 模拟生产耗时 100ms } return NULL; } void *consumer(void *arg){ while (1){ pthread_mutex_lock(&mtx); while (number == 0 && total_of_consume < total_of_produce) { pthread_cond_wait(&cond, &mtx); // 等待有货 } if (total_of_consume >= total_of_produce && number == 0){ pthread_mutex_unlock(&mtx); break; // 所有产品消费完,退出 } number--; // 消费一个产品 total_of_consume++; // 总消费+1 printf("消费者[%lu] 消费一个产品,库存=%d\n",(unsigned long)pthread_self(),number); pthread_mutex_unlock(&mtx); usleep(150000); // 模拟耗时150ms } return NULL; } int main(int argc,char *argv[]){ if (argc < 2){ fprintf(stderr,"用法:%s num1 num2 ...\n",argv[0]); exit(EXIT_FAILURE); } int num_producers = argc - 1; int num_consumers = 2; // 固定两个消费者线程 pthread_t producers[num_producers]; pthread_t consumers[num_consumers]; // 创建消费者线程 for (int i = 0; i < num_consumers;i++) { pthread_create(&consumers[i],NULL,consumer,NULL); } // 创建生产者线程 for (int i = 0; i < num_producers; i++) { pthread_create(&producers[i],NULL,producer,argv[i+1]); } // 等待所有生产者结束 for (int i = 0; i < num_producers; i++) { pthread_join(producers[i], NULL); } // 等待所有消费者结束 for (int i = 0; i < num_consumers; i++) { pthread_join(consumers[i], NULL); } pthread_mutex_destroy(&mtx); pthread_cond_destroy(&cond); printf("所有产品生产=%d, 消费=%d, 程序结束\n", total_of_produce, total_of_consume); return 0; }

得分 100
学习任务

cjozGV 的学生作业:

#include // printf, fprintf #include // atoi, exit #include // bool 类型 #include // POSIX 线程库 #include // usleep static int number = 0; // 当前库存数量 (共享变量) static int total_of_produce = 0; // 以生产总数 static int total_of_consume = 0; // 以消费总数 pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; void *producer(void *arg){ int cnt = atoi((char *)arg); // 将命令行参数转为整数,表示生产数量 for (int i = 0; i < cnt; i++) { pthread_mutex_lock(&mtx); // 加锁,进入临界区 number++; // 库存+1 total_of_produce++; // 总生产+1 printf("生产者 [%ld] 生产一个产品,产品总数=%d\n",pthread_self(),number); pthread_cond_signal(&cond); // 通知消费者有货了 pthread_mutex_unlock(&mtx); // 解锁,退出临界区 usleep(100000); // 模拟生产耗时 100ms } return NULL; } void *consumer(void *arg){ (void)arg; // 忽略未使用的参数 while (1){ pthread_mutex_lock(&mtx); // 如果库存为0且还有未消费的产品,就等待 while (number == 0 && total_of_consume < total_of_produce){ pthread_cond_wait(&cond,&mtx); } // 如果消费数已达到生产总数,且库存为0,退出循环 if (total_of_consume >= total_of_produce && number == 0){ pthread_mutex_unlock(&mtx); break; } number--; // 消费一个产品 total_of_consume++; // 总消费+1 printf("消费者 [%ld] 消费一个产品,产品总数=%d\n",pthread_self(),number); usleep(150000); // 模拟消费耗时 150ms } return NULL; } int main(int argc,char *argv[]){ if (argc < 2){ fprintf(stderr,"用法: %s num1 num2 ...\n",argv[0]); exit(EXIT_FAILURE); } int n = argc - 1; // 生产者数量 pthread_t producers[n]; // 保存生产者线程id pthread_t consumer_tid; // 消费者线程id // 创建消费者线程 pthread_create(&consumer_tid,NULL,consumer,NULL); // 创建多个生产者线程 for (int i = 0; i < n; i++) { pthread_create(&producers[i],NULL,producers,argv[i+1]); } // 等待所有生产者线程结束 for (int i = 0; i < n; i++) { pthread_join(producers[i],NULL); } // 等待所有消费者线程结束 for (int i = 0; i < n; i++) { pthread_join(consumer_tid,NULL); } pthread_mutex_destroy(&mtx); pthread_cond_destroy(&cond); printf("所有产品生产=%d,消费=%d,程序结束\n",total_of_produce,total_of_consume); return 0; }

微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号