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

作业社区

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

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

史啦啦 的学生作业:

【图片】 //实现多个生产者与多个消费者模型,在示例的基础上进行修改,提示,需要使用 pthread_cond_broadcast 函数唤醒所有阻塞的消费者线程 #include #include #include #include #include #include static int number = 0; // 共享资源 static bool producers_done = false; // 生产者完成标志 static int active_producers = 0; // 活跃生产者计数器 static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; // 生产者线程函数 void *producer_handler(void *arg) { int cnt = atoi((char *)arg); for (int i = 0; i < cnt; i++) { pthread_mutex_lock(&mtx); number++; printf("Producer [%ld] created, total: %d\n", (long)pthread_self(), number); pthread_cond_broadcast(&cond); // 广播唤醒所有消费者 pthread_mutex_unlock(&mtx); usleep(1000); // 模拟生产耗时 } pthread_mutex_lock(&mtx); active_producers--; if (active_producers == 0) { producers_done = true; pthread_cond_broadcast(&cond); // 最终唤醒所有消费者 } pthread_mutex_unlock(&mtx); return NULL; } // 消费者线程函数 void *consumer_handler(void *arg) { while (1) { pthread_mutex_lock(&mtx); // 等待产品可消费或生产者全部完成 while (number == 0 && !producers_done) { pthread_cond_wait(&cond, &mtx); } if (number > 0) { number--; printf("Consumer [%ld] consumed, remaining: %d\n", (long)pthread_self(), number); pthread_mutex_unlock(&mtx); usleep(1500); // 模拟消费耗时 } else if (producers_done) { pthread_mutex_unlock(&mtx); break; // 退出条件:无产品且生产者已结束 } } return NULL; } int main(int argc, char *argv[]) { if (argc < 3) { fprintf(stderr, "Usage: %s ... -c \n", argv[0]); return EXIT_FAILURE; } // 解析消费者数量参数(格式:-c 3) int consumer_count = 2; // 默认2个消费者 for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "-c") == 0 && i+1 < argc) { consumer_count = atoi(argv[i+1]); break; } } active_producers = argc - 1 - (consumer_count > 0 ? 2 : 0); // 创建生产者线程 pthread_t producers[active_producers]; for (int i = 1, idx = 0; i < argc && idx < active_producers; i++) { if (strcmp(argv[i], "-c") == 0) { i++; // 跳过消费者数量参数 continue; } pthread_create(&producers[idx++], NULL, producer_handler, argv[i]); } // 创建消费者线程 pthread_t consumers[consumer_count]; for (int i = 0; i < consumer_count; i++) { pthread_create(&consumers[i], NULL, consumer_handler, NULL); } // 等待生产者完成 for (int i = 0; i < active_producers; i++) { pthread_join(producers[i], NULL); } // 等待消费者完成 for (int i = 0; i < consumer_count; i++) { pthread_join(consumers[i], NULL); } printf("All tasks completed. Final stock: %d\n", number); return 0; }

得分 100
学习任务

史啦啦 的学生作业:

【图片】 //基于互斥锁实现生产者与消费者模型 #include #include #include #include #include static int number = 0;//记录仓库场的产品数量 static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; //生产者线程的执行函数 void *thread_handler(void *arg) { int cnt = atoi((char *)arg); int i,tmp; for(i = 0;i < cnt;i++){ pthread_mutex_lock(&mtx); printf("线程 [%ld] 生产一个产品,产品数量为:%d\n",pthread_self(),++number); pthread_mutex_unlock(&mtx); } pthread_exit((void *)0); //pthread_exit(NULL); } //./a.out 1 2 3 4 ====>创建4个线程,分别生产1个产品 2个产品 3个产品 4个产品 // argc = 5 int main(int argc,char *argv[]) { pthread_t tid[3] = {0}; int i; int err; int total_of_produce = 0;//记录总的生产产品的数量 int total_of_consume = 0;//记录总的消费产品的数量 bool done = false; for (i = 1;i < argc;i++){ total_of_produce += atoi(argv[i]); // 生产产品数量的总和 err = pthread_create(&tid[i-1],NULL,thread_handler,(void *)argv[i]); //需要将每个线程生产的产品数量传递 if (err != 0) { perror("pthread_create()"); exit(EXIT_FAILURE); } } for (;;){ pthread_mutex_lock(&mtx); while(number > 0){ total_of_consume++; // 消费产品总数 printf("消费一个产品,产品数量为:%d\n",--number); done = total_of_consume >= total_of_produce; // 判断消费者数量与产品数量 sleep(1); } pthread_mutex_unlock(&mtx); if (done) break; } for(int i=0;i < argc -1;i++) { pthread_join(tid[i],NULL); } return 0; }

得分 100
讨论题

慕九州9493288 的学生作业:

一、代码 looplist.h #ifndef __looplist_H__ #define __looplist_H__ #include #include #include typedef int datatype_t; typedef struct node { datatype_t data;//数据域保存有效数据 struct node *next;//指针域保存下一个结点的地址 }loopnode_t; // 创建一个包含n个节点的不带头结点的循环链表 extern loopnode_t *create_looplist(int n); // 解决约瑟夫环问题 extern void josephus_problem(loopnode_t* head, int k, int m); // 打印循环链表中的所有数据 extern void print_looplist(loopnode_t *head); #endif looplist.c #include "looplist.h" /* 创建一个包含n个节点的不带头结点的循环链表 */ loopnode_t *create_looplist(int n) { if(n < 1) return NULL; loopnode_t *head = NULL, *p = NULL; for(int i = 1;i data = i; if(head == NULL) { head = node; head->next = head; p = head;//结点下个就是指向自己 } else { node->next = p->next;//原结点next给新结点next p->next = node;//原结点指向新结点 p = node;// p指向下一个结点 } } return head; } /* 打印循环链表中的所有数据 */ void print_looplist(loopnode_t *head) { loopnode_t *p = head; do { printf("%d ",p->data); p = p->next; } while(p != head); printf("\n"); } /* 约瑟夫环问题解决函数 */ void josephus_problem(loopnode_t* head, int k, int m) { if(head == NULL || k < 1 || m < 1) { printf("Invalid input!\n"); } loopnode_t *p = head; loopnode_t *q = NULL; // 定位到第k个节点 for(int i = 1;i < k;i++) { q = p; p = p->next; } printf("Initial looplist: "); print_looplist(head); while(p->next != p) // 当链表中不止一个节点时循环 { for(int i = 1;i < m;i++) { q = p; p = p->next; } // 跳过当前节点 q->next = p->next; printf("Eliminated data: %d\n", p->data); free(p); p = q->next; } // 输出最后一个剩余节点 printf("Last data: %d\n",p->data); free(q); } main.c #include "looplist.h" int main(int argc, const char *argv[]) { int n = 8,k = 3, m = 4; // 创建循环链表 loopnode_t *head = create_looplist(n); // 解决约瑟夫环问题 josephus_problem(head,k,m); return 0; } 二、结果 【图片】

得分 100
学习任务

慕九州9493288 的学生作业:

一、代码 linklist.h #ifndef __LINKLIST_H__ #define __LINKLIST_H__ #include #include #include typedef int datatype_t; typedef struct node { datatype_t data; struct node* next; }node_linklist_t; extern node_linklist_t* create_linklist(); extern int is_empty_linklist(node_linklist_t* head); extern void insert_linklist(node_linklist_t* head,datatype_t data); extern int reverse_linklist(node_linklist_t* head); extern int print_linklist(node_linklist_t* head); extern void free_linklist(node_linklist_t* head); #endif linklist.c #include "linklist.h" node_linklist_t* create_linklist() { node_linklist_t* head = malloc(sizeof(node_linklist_t)); if(head == NULL) { printf("malloc is fail!\n"); return NULL; } memset(head,0,sizeof(node_linklist_t)); return head; } int is_empty_linklist(node_linklist_t* head) { return head->next == NULL; } void insert_linklist(node_linklist_t* head,datatype_t data) { node_linklist_t* temp = malloc(sizeof(node_linklist_t)); node_linklist_t* p = head; if(temp == NULL) { printf("malloc is fail!\n"); return; } while(p->next != NULL && data > p->next->data) { p = p->next; } temp->data = data; temp->next = p->next; p->next = temp; return; } //链表逆序 int reverse_linklist(node_linklist_t* head) { if(is_empty_linklist(head)) { return -1; } node_linklist_t* p = head->next->next; head->next->next = NULL; while(p != NULL) { node_linklist_t* q = p->next; p->next = head->next; head->next = p; p = q; } } int print_linklist(node_linklist_t* head) { node_linklist_t* p = head; while(p->next != NULL) { printf("%d ",p->next->data); p = p->next; } printf("\n"); } //释放链表内存 void free_linklist(node_linklist_t* head) { printf("next data is:\n"); node_linklist_t* p = head; while(p != NULL) { node_linklist_t* q = p; p = p->next; print_linklist(q); free(q); // // node_linklist_t* q = p->next; // print_linklist(p); // free(p); // p = q; } } main.c #include "linklist.h" int main(int argc, const char *argv[]) { datatype_t data = 0; int n = 0; node_linklist_t* head = create_linklist(); if(head != NULL) { printf("Please input the data number you want to reverse: "); scanf("%d",&n); printf("Please insert %d data: ",n); for(int i = 0;i < n;i++) { scanf("%d",&data); insert_linklist(head,data); } print_linklist(head); printf("Are you want to reverse? Yes: 1 or No: 0 , you chose: "); scanf("%d",&n); if(n) { int ret = reverse_linklist(head); if(ret < 0) { printf("The data is empty!\n"); } print_linklist(head); } free_linklist(head); head = NULL; } return 0; } 二、结果 【图片】

微信客服

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

帮助反馈 APP下载

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

公众号

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