《Linux應用實例教程》課件-講義28并發程序-父子進程線程異步性_第1頁
《Linux應用實例教程》課件-講義28并發程序-父子進程線程異步性_第2頁
《Linux應用實例教程》課件-講義28并發程序-父子進程線程異步性_第3頁
《Linux應用實例教程》課件-講義28并發程序-父子進程線程異步性_第4頁
《Linux應用實例教程》課件-講義28并發程序-父子進程線程異步性_第5頁
已閱讀5頁,還剩11頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

S10、并發程序-父子進程線程異步性S10、并發程序-父子進程線程異步性S10、并發程序-父子進程線程異步性1、創建父子進程,觀察父子進程執行的順序,了解進程執行的異步行為#include<stdio.h>#include<sys/types.h>#include<unistd.h>#include<stdlib.h>intmain(){pid_tpid;char*msg;S10、并發程序-父子進程線程異步性

intk;printf("觀察父子進程執行的先后順序,了解調度算法的特征\n");pid=fork();switch(pid){case0:msg="子進程在運行";k=3;break;case-1:msg="進程創建失敗";break;S10、并發程序-父子進程線程異步性default:msg="父進程在運行";k=5;break;}while(k>0){puts(msg); sleep(1); k--; }exit(0);}S10、并發程序-父子進程線程異步性編譯:gccparent-child-fork.c-oparent-child-fork運行:./parent-child-forkS10、并發程序-父子進程線程異步性2、創建主線程和子線程,觀察多線程執行的順序,了解線程執行的異步行為#include<stdio.h>#include<pthread.h>staticintrun=1;staticintretvalue;void*threadfunc(void*arg){ int*running=arg;

S10、并發程序-父子進程線程異步性 printf("子線程初始化完畢,傳入參數為:%d\n",*running); while(*running) { printf("子線程正在運行\n"); usleep(1); } printf("子線程退出\n"); retvalue=8; pthread_exit((void*)&retvalue);}S10、并發程序-父子進程線程異步性intmain(){ pthread_tpt; intret=-1; inttimes=3; inti=0; int*ret_join=NULL; ret=pthread_create(&pt,NULL,(void*)threadfunc,&run); if(ret!=0) { printf("建立線程失敗\n"); return1; } printf("主線程創建子線程后在運行...\n");S10、并發程序-父子進程線程異步性 usleep(1); printf("主線程調用usleep(1)...\n"); for(;i<times;i++) { printf("主線程打印i=%d\n",i); usleep(1); } run=0; pthread_join(pt,(void*)&ret_join); printf("線程返回值為:%d\n",*ret_join); return0;}S10、并發程序-父子進程線程異步性編譯:gccthread.c-othread-lpthread運行:./threadS10、并發程序-父子進程線程異步性3、多線程對共享變量的非互斥訪問#include<stdio.h>#include<stdlib.h>#include<pthread.h>#include<unistd.h>#include<string.h>intnum=30,count=10;void*sub1(void*arg){inti=0,tmp;S10、并發程序-父子進程線程異步性for(;i<count;i++){

tmp=num-1; usleep(13); num=tmp;

printf("線程1num減1后值為:%d\n",num);}return((void*)0);}void*sub2(void*arg){inti=0,tmp;for(;i<count;i++){

tmp=num-1; usleep(31);S10、并發程序-父子進程線程異步性 num=tmp;printf("線程2num減1后值為:%d\n",num);}return((void*)0);}intmain(intargc,char**argv){pthread_ttid1,tid2;interr,i=0,tmp;void*tret;err=pthread_create(&tid1,NULL,sub1,NULL);if(err!=0){ printf("pthread_createerror:%s\n",strerror(err));exit(-1);

}S10、并發程序-父子進程線程異步性err=pthread_create(&tid2,NULL,sub2,NULL);if(err!=0){ printf("pthread_createerror:%s\n",strerror(err));exit(-1);}for(;i<count;i++)

{

tmp=num-1; usleep(5);

num=tmp;printf("mainnum減1后值為:%d\n",num);}printf("兩個線程運行結束\n");S10、并發程序-父子進程線程異步性 err=pthread_join(tid1,&tret);if(err!=0){ printf("cannotjoinwiththread1:%s\n",strerror(err));exit(-1);}printf("thread1exitcode%d\n",(int)tret);err=pthread_join(tid2,&tret);if(err!=0){ printf("cannotjoinwiththread1:%s\n",strerror(err));exit(-1

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論