第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

作業(yè)社區(qū)

探索學(xué)習(xí)新天地,共享知識資源!

0 提交作業(yè)
0 布置作業(yè)
0 滿分作業(yè)
得分 100
學(xué)習(xí)任務(wù)

史啦啦 的學(xué)生作業(yè):

【圖片】 //實現(xiàn)多個生產(chǎn)者與多個消費者模型,在示例的基礎(chǔ)上進行修改,提示,需要使用 pthread_cond_broadcast 函數(shù)喚醒所有阻塞的消費者線程 #include #include #include #include #include #include static int number = 0; // 共享資源 static bool producers_done = false; // 生產(chǎn)者完成標(biāo)志 static int active_producers = 0; // 活躍生產(chǎn)者計數(shù)器 static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; // 生產(chǎn)者線程函數(shù) 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); // 模擬生產(chǎn)耗時 } pthread_mutex_lock(&mtx); active_producers--; if (active_producers == 0) { producers_done = true; pthread_cond_broadcast(&cond); // 最終喚醒所有消費者 } pthread_mutex_unlock(&mtx); return NULL; } // 消費者線程函數(shù) void *consumer_handler(void *arg) { while (1) { pthread_mutex_lock(&mtx); // 等待產(chǎn)品可消費或生產(chǎn)者全部完成 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; // 退出條件:無產(chǎn)品且生產(chǎn)者已結(jié)束 } } return NULL; } int main(int argc, char *argv[]) { if (argc < 3) { fprintf(stderr, "Usage: %s ... -c \n", argv[0]); return EXIT_FAILURE; } // 解析消費者數(shù)量參數(shù)(格式:-c 3) int consumer_count = 2; // 默認(rèn)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); // 創(chuàng)建生產(chǎn)者線程 pthread_t producers[active_producers]; for (int i = 1, idx = 0; i < argc && idx < active_producers; i++) { if (strcmp(argv[i], "-c") == 0) { i++; // 跳過消費者數(shù)量參數(shù) continue; } pthread_create(&producers[idx++], NULL, producer_handler, argv[i]); } // 創(chuàng)建消費者線程 pthread_t consumers[consumer_count]; for (int i = 0; i < consumer_count; i++) { pthread_create(&consumers[i], NULL, consumer_handler, NULL); } // 等待生產(chǎn)者完成 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
學(xué)習(xí)任務(wù)

史啦啦 的學(xué)生作業(yè):

【圖片】 //基于互斥鎖實現(xiàn)生產(chǎn)者與消費者模型 #include #include #include #include #include static int number = 0;//記錄倉庫場的產(chǎn)品數(shù)量 static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; //生產(chǎn)者線程的執(zhí)行函數(shù) 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] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:%d\n",pthread_self(),++number); pthread_mutex_unlock(&mtx); } pthread_exit((void *)0); //pthread_exit(NULL); } //./a.out 1 2 3 4 ====>創(chuàng)建4個線程,分別生產(chǎn)1個產(chǎn)品 2個產(chǎn)品 3個產(chǎn)品 4個產(chǎn)品 // argc = 5 int main(int argc,char *argv[]) { pthread_t tid[3] = {0}; int i; int err; int total_of_produce = 0;//記錄總的生產(chǎn)產(chǎn)品的數(shù)量 int total_of_consume = 0;//記錄總的消費產(chǎn)品的數(shù)量 bool done = false; for (i = 1;i < argc;i++){ total_of_produce += atoi(argv[i]); // 生產(chǎn)產(chǎn)品數(shù)量的總和 err = pthread_create(&tid[i-1],NULL,thread_handler,(void *)argv[i]); //需要將每個線程生產(chǎn)的產(chǎn)品數(shù)量傳遞 if (err != 0) { perror("pthread_create()"); exit(EXIT_FAILURE); } } for (;;){ pthread_mutex_lock(&mtx); while(number > 0){ total_of_consume++; // 消費產(chǎn)品總數(shù) printf("消費一個產(chǎn)品,產(chǎn)品數(shù)量為:%d\n",--number); done = total_of_consume >= total_of_produce; // 判斷消費者數(shù)量與產(chǎn)品數(shù)量 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 的學(xué)生作業(yè):

一、代碼 looplist.h #ifndef __looplist_H__ #define __looplist_H__ #include #include #include typedef int datatype_t; typedef struct node { datatype_t data;//數(shù)據(jù)域保存有效數(shù)據(jù) struct node *next;//指針域保存下一個結(jié)點的地址 }loopnode_t; // 創(chuàng)建一個包含n個節(jié)點的不帶頭結(jié)點的循環(huán)鏈表 extern loopnode_t *create_looplist(int n); // 解決約瑟夫環(huán)問題 extern void josephus_problem(loopnode_t* head, int k, int m); // 打印循環(huán)鏈表中的所有數(shù)據(jù) extern void print_looplist(loopnode_t *head); #endif looplist.c #include "looplist.h" /* 創(chuàng)建一個包含n個節(jié)點的不帶頭結(jié)點的循環(huá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;//結(jié)點下個就是指向自己 } else { node->next = p->next;//原結(jié)點next給新結(jié)點next p->next = node;//原結(jié)點指向新結(jié)點 p = node;// p指向下一個結(jié)點 } } return head; } /* 打印循環(huán)鏈表中的所有數(shù)據(jù) */ void print_looplist(loopnode_t *head) { loopnode_t *p = head; do { printf("%d ",p->data); p = p->next; } while(p != head); printf("\n"); } /* 約瑟夫環(huán)問題解決函數(shù) */ 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個節(jié)點 for(int i = 1;i < k;i++) { q = p; p = p->next; } printf("Initial looplist: "); print_looplist(head); while(p->next != p) // 當(dāng)鏈表中不止一個節(jié)點時循環(huán) { for(int i = 1;i < m;i++) { q = p; p = p->next; } // 跳過當(dāng)前節(jié)點 q->next = p->next; printf("Eliminated data: %d\n", p->data); free(p); p = q->next; } // 輸出最后一個剩余節(jié)點 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; // 創(chuàng)建循環(huán)鏈表 loopnode_t *head = create_looplist(n); // 解決約瑟夫環(huán)問題 josephus_problem(head,k,m); return 0; } 二、結(jié)果 【圖片】

得分 100
學(xué)習(xí)任務(wù)

慕九州9493288 的學(xué)生作業(yè):

一、代碼 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"); } //釋放鏈表內(nèi)存 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; } 二、結(jié)果 【圖片】

微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號