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

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

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

探索學習新天地,共享知識資源!

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

蠟筆小方哎 的學生作業(yè):

創(chuàng)建兩個進程,一個進程負責將文件數(shù)據(jù)寫入共享內(nèi)存,一個進程負責從共享內(nèi)存讀取數(shù)據(jù)并保存成文件 #include #include #include #include #include #include #include #define SHM_PATH "." #define SHM_PROJ_ID 'X' #define SHM_SZ 100 int main(int argc, char* argv[]) { key_t key; int shmid; void* addr; char buf[SHM_SZ]; FILE* fp; if(argc < 2) { printf("Usage: %s filename\n", argv[0]); return 0; } fp = fopen(argv[1], "r"); if(fp == NULL) { perror("fopen() error!\n"); exit(-1); } key = ftok(SHM_PATH, SHM_PROJ_ID); if(key == -1) { perror("ftok() error!\n"); fclose(fp); exit(-1); } printf("key = %d\n", key); shmid = shmget(key, SHM_SZ, IPC_CREAT | 0644); if(shmid == -1) { perror("shmget() error!\n"); fclose(fp); exit(-1); } printf("shmid = %d\n", shmid); addr = shmat(shmid, NULL, 0); if(addr == (void*)-1) { perror("shmat() error!\n"); fclose(fp); exit(-1); } memset(addr, 0, SHM_SZ); while(1) { if(strcmp(addr, "") != 0) { // 如果讀進程沒有將共享內(nèi)存清空,就等待 usleep(20); continue; } memset(buf, 0, SHM_SZ); if(fgets(buf, SHM_SZ, fp) == NULL) { strcpy(addr, "quit"); break; } strcpy(addr, buf); } if(shmdt(addr) == -1) { perror("shmdt() error!\n"); fclose(fp); exit(-1); } sleep(1); // 等待一秒是為了確保在讀進程退出之后再刪除共享內(nèi) if(shmctl(shmid, IPC_RMID, NULL) == -1) { perror("shmctl() error!\n"); fclose(fp); exit(-1); } fclose(fp); return 0; } #include #include #include #include #include #include #include #define SHM_PATH "." #define SHM_PROJ_ID 'X' #define SHM_SZ 100 int main() { key_t key; int shmid; void* addr; char buf[SHM_SZ]; FILE* fp; fp = fopen("copy.txt", "w"); if(fp == NULL) { perror("fopen() error!\n"); exit(-1); } key = ftok(SHM_PATH, SHM_PROJ_ID); if(key == -1) { perror("ftok() error!\n"); fclose(fp); exit(-1); } printf("key = %d\n", key); shmid = shmget(key, SHM_SZ, IPC_CREAT | 0644); if(shmid == -1) { perror("shmget() error!\n"); fclose(fp); exit(-1); } printf("shmid = %d\n", shmid); addr = shmat(shmid, NULL, 0); if(addr == (void*)-1) { perror("shmat() error!\n"); fclose(fp); exit(-1); } while(1) { memset(buf, 0, SHM_SZ); strcpy(buf, addr); if(strcmp(buf, "") == 0) { // 從共享內(nèi)存拿到空數(shù)據(jù),表示寫進程還沒有輸入 usleep(20); continue; } // 拿到數(shù)據(jù)后清空共享內(nèi)存 memset(addr, 0, SHM_SZ); if(strcmp(buf, "quit") == 0) break; fputs(buf, fp); } if(shmdt(addr) == -1) { perror("shmdt() error!\n"); fclose(fp); exit(-1); } fclose(fp); return 0; }

得分 100
學習任務(wù)

jelasin 的學生作業(yè):

#define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static int number = 0;//共享變量 int total_of_produce = 0; // 總的生產(chǎn)產(chǎn)品的數(shù)量 int total_of_consume = 0; // 總的消費產(chǎn)品的數(shù)量 bool done = false; static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; void *product_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_cond_broadcast(&cond); // 喚醒消費者線程 pthread_mutex_unlock(&mtx); } pthread_exit((void *)0); } void* consumer_handler(void *arg) { pthread_mutex_lock(&mtx); while(number == 0) // 當產(chǎn)品數(shù)量為 0時,讓線程阻塞,并釋放鎖,這里一般設(shè)置循環(huán),防止沒有重新獲取到鎖 pthread_cond_wait(&cond,&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ù)量 } pthread_mutex_unlock(&mtx); // 消費者消費完成之后,釋放鎖 pthread_exit((void *)0); } int main(int argc,char *argv[]) { pthread_t tid; int i; int err; for (i = 1;i < argc;i++) { total_of_produce += atoi(argv[i]); // 生產(chǎn)數(shù)量的總和 err = pthread_create(&tid,NULL,product_handler,(void *)argv[i]); if (err != 0) { perror("[ERROR] pthread_create(): "); exit(EXIT_FAILURE); } pthread_detach(tid); // 線程分離,不等待線程結(jié)束 } while (done == false) { err = pthread_create(&tid,NULL,consumer_handler,NULL); if (err != 0) { perror("[ERROR] pthread_create(): "); exit(EXIT_FAILURE); } pthread_detach(tid); // 線程分離,不等待線程結(jié)束 sleep(1); // 等待1秒鐘,防止消費者線程阻塞 } return 0; } ? 5 ./main 10 10 10 線程 [138990789330496] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:1 線程 [138990789330496] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:2 線程 [138990789330496] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:3 線程 [138990789330496] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:4 線程 [138990789330496] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:5 線程 [138990642529856] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:6 線程 [138990642529856] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:7 線程 [138990757873216] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:8 線程 [138990757873216] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:9 線程 [138990757873216] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:10 線程 [138990757873216] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:11 線程 [138990757873216] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:12 線程 [138990757873216] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:13 線程 [138990757873216] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:14 線程 [138990757873216] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:15 線程 [138990757873216] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:16 線程 [138990757873216] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:17 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:16 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:15 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:14 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:13 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:12 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:11 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:10 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:9 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:8 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:7 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:6 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:5 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:4 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:3 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:2 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:1 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:0 線程 [138990789330496] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:1 線程 [138990789330496] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:2 線程 [138990642529856] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:3 線程 [138990789330496] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:4 線程 [138990789330496] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:5 線程 [138990789330496] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:6 線程 [138990642529856] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:7 線程 [138990642529856] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:8 線程 [138990642529856] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:9 線程 [138990642529856] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:10 線程 [138990642529856] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:11 線程 [138990642529856] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:12 線程 [138990642529856] 生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:13 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:12 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:11 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:10 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:9 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:8 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:7 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:6 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:5 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:4 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:3 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:2 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:1 消費一個產(chǎn)品,產(chǎn)品數(shù)量為:0 發(fā)送信號,通知等待在條件上的線程,然后解鎖互斥量。注意范例代碼中先發(fā)送信號,然后解鎖互斥量,這個順序不是必須的,也可以顛倒。標準允許任意順序執(zhí)行這兩個調(diào)用。先通知條件變量、后解鎖互斥量,效率會比先解鎖、后通知條件變量低。因為先通知后解鎖,執(zhí)行pthread_cond_wait的線程可能在互斥量已然處于加鎖狀態(tài)的時候醒來,發(fā)現(xiàn)互斥量仍然沒有解鎖,就會再次休眠,從而導致了多余的上下文切換。某些實現(xiàn)使用等待變形(wait morphing)來優(yōu)化這個問題:并不真正地喚醒執(zhí)行pthread_cond_wait的線程,而是將線程從條件變量的等待隊列轉(zhuǎn)移到互斥量的等待隊列上,從而消除無謂的上下文切換。 glibc對pthread_cond_broadcast做了類似的優(yōu)化,即只喚醒一個線程,將其他線程從條件變量的等待隊列搬移到了互斥量的等待隊列中。 先解鎖、后通知條件變量雖然可能會有性能上的優(yōu)勢,但是也會帶來其他的問題。如果存在一個高優(yōu)先級的線程,既等待在互斥量上,也等待在條件變量上;同時還存在一個低優(yōu)先級的線程,只等待在互斥量上。一旦先解鎖互斥量,低優(yōu)先級的進程就可能會搶先獲得互斥量,待調(diào)用pthread_cond_signal之后,高優(yōu)先級的進程會發(fā)現(xiàn)互斥量已經(jīng)被低優(yōu)先級的進程搶走了。

得分 100
學習任務(wù)

jelasin 的學生作業(yè):

#define _GNU_SOURCE #include #include #include #include #include #include #include #include #include void *thread1_func(void *arg) { char *stack = NULL; printf("stack address: %p\n", &stack); printf("#1: KThread id: %d\n", gettid()); printf("#1: PThread id: %lu\n", pthread_self()); return NULL; } void *thread2_func(void *arg) { char *stack = NULL; printf("stack address: %p\n", &stack); printf("#2: KThread id: %d\n", gettid()); printf("#2: PThread id: %lu\n", pthread_self()); return NULL; } int main() { pthread_t thread1, thread2; if (pthread_create(&thread1, NULL, thread1_func, NULL)!= 0) { perror("pthread_create #1"); return 1; } if (pthread_create(&thread2, NULL, thread2_func, NULL)!= 0) { perror("pthread_create #2"); return 1; } pthread_detach(thread1); pthread_detach(thread2); pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if (pthread_create(&thread1, &attr, thread1_func, NULL)!= 0) { perror("pthread_create #3"); return 1; } if (pthread_create(&thread2, &attr, thread2_func, NULL)!= 0) { perror("pthread_create #4"); return 1; } pthread_exit(NULL); return 0; } ? 5 ./main stack address: 0x7783c3dffe40 #1: KThread id: 15176 #1: PThread id: 131407810659904 stack address: 0x7783c33ffe40 #2: KThread id: 15177 #2: PThread id: 131407800174144 stack address: 0x7783bbdffe40 #1: KThread id: 15178 #1: PThread id: 131407676442176 stack address: 0x7783c29ffe40 #2: KThread id: 15179 #2: PThread id: 131407789688384

微信客服

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

幫助反饋 APP下載

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

公眾號

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