


版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、操作系統(tǒng):線程(進程)并發(fā)拷貝程序 附錄一:程序代碼 #include #include<sys/> #include<sys/> #include<> #include<>#include<>#include<>#definePSIZE 4096/*管道文件的大小*/#defineBSIZE 128/*默認(rèn)緩沖區(qū)的大小*/#defineNOFILE 20/*u_ofile3575表可分配的個數(shù)*/#defineNFILE 20Afile表可分配的個數(shù)*/#defineNPIPE 20/*pipecb3575可分配的個數(shù)*
2、/*進程的u_file表*/int u_ofile3575NOFILE;/*模擬file表*/struetchar f_flag;/*讀寫標(biāo)志,'w'表示寫,':r'表示讀*/可分配;int f_count;/*表示此表項的狀態(tài),二0表示此表項沒被使用,二1表示此表項在被使用,不可再分配*/int f_inode;/*對應(yīng)的 pipecb3575 表下標(biāo)*/long f_offset;/*讀寫指針,當(dāng)前已讀或已寫個數(shù)*/fileNFILE;/*管道控制塊*/struetchar *p_addr; /*管道文件基地址*/int p_size;/*管道文件大小,PS
3、IZE*/int p_count;/*二2表示讀寫都在被進行,二1表示在被讀或被寫,二0表 示管道沒被使用,可分配*/pipecb3575NPIPE;/*模擬管道文件*/char *pfile;/*管道的寫入寫出端*/int fd2;/*鎖機制,實現(xiàn)互斥*/pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER:/*進程間通信,實現(xiàn)同步*/pthread_cond_t rflag = PTHREAD_COND_INITIALIZER;/*讀信號量*/ pthread_cond_t wflag = PTHREAD_COND_INITIALIZER;/*寫
4、信號量*/*線程創(chuàng)建函數(shù)只能傳一個參數(shù),用結(jié)構(gòu)體來封裝所有參數(shù)*/struet arg_setchar *fname;/*文件名 */int f;/*傳遞 fdp*/;/*u_ofile3575 表初始化*/int u_of訂e_init3575()printf("init the u_of訂e3575n"); int i;for(i=0;i<N0FILE;i+)u_ofile3575i二 T;u_ofile35750=0;u_ofile3575l=0;u_ofile35752=0;return 0;/*創(chuàng)建管道*/int pipe_simulate3575(int
5、a)printf("start to create a pipen"); int i;if(u_ofile3575i=-l)a0=匚/*讀*/u_ofil e3575i = 0;/* 讀端 */break;for(i;i<N0FILE;i+)if (u_ofile3575i=-l)al二譏/*寫*/u_ofile3575i = 1;/* 寫端 */break;if(i>=N0FILE)printf ("u_ofile3575 分酉己失敗,failure to create a pipen");:return 2;pfile = (char *
6、)malloc (PSIZE*sizeof (char) ;/*申請模擬管道用的內(nèi)存空間*/if (pf i le二二NULL) /*申請可能不成功*/printf (''failure to create a pipen);return -1;for(i=0;i<NFILE;i+)if(filei. f_count!二1)f訂ei. f_flag = 'r' ;/*讀標(biāo)志*/_inode = 0;/*讀對應(yīng) pipecb3575 表下標(biāo)*/filei. f_count = l;/*file0這個表項在被使用,不可再分配*/f訂ei.f_offset 二
7、0;/*讀指針*/u_ofile3575a0 = i;/* 讀端 */break;if (filei. f_count!二 1)filei. f_flag = ' w' ;/*寫標(biāo)志*/_inode = 0;/*寫對應(yīng)pipecb3575控制塊卜標(biāo)*/filei. f_count = l;/*f訂el這個表項在被使用,不可再分配*/f訂ei.f_offset 二 0;/*寫指針*/u_ofil e3575 al = i;/* 寫端 */break;if(i二NFILE)printf (''failure to create a pipen);:return -1
8、;for(i=0;i<NPIPE;i+)if (pipecb3575i. p_count=0)pipecb3575i.p_addr 二 pfile;/*給管道文件基地址賦值*/pipecb3575i.p_size 二 PSIZE;/*管道文件大小*/pipecb3575i. p_count = 2;/*讀寫都在進行,此 pipecb3575表項不可再分*/fileu_ofile3575a0. f_inode = i;fileu_ofile3575al. f_inode 二 i;break;if(i>=NPIPE)printf (''failure to create
9、 a pipen");return -1;printf (z,Secceed create the pipen");return 0;/*分配成功*/*關(guān)閉管道*/int close_simulate3575(int a)printf("start to close the pipe you have createdn"); char *p;int i;p=pipecb3575f ile u_ofile3575ai . f_inode. p_addr;/*if(p!=NULL)free(p) ; _inode. p_count = 0; /*管道控制塊計
10、數(shù)清零*/ fileu_ofile3575ai. f_count = 0; /*file 表項計數(shù)清零*/ u_ofile3575ai = T; /*u_ofile3575 表項清空*/ ai = -1; /*fd譏清空*/printf("secceed close the pipen");return 0;/*寫管道*/int numwrite_simulate3575;int write_simulate3575 (int fd, char *ca, int n)_inode;/*讀管道對應(yīng)的 pipecb3575表的卜標(biāo)*/int nl二n;/*次應(yīng)該寫入的字符個數(shù)*
11、/int wstart二0;/*計數(shù)器,寫入字符個數(shù)*/int i 二 0;for(i;iNFILE;i+)/*尋找寫管道對應(yīng)的讀管道的讀端*/if (file i. f_flag=,r' )&&(filei. f_inode 二二 pf)break;elsecontinue;printf (z,add the lockn");pthread_mutex_lock(&lock) ;/*互斥鎖,相當(dāng)于進入臨界區(qū)*/offr 二 filer.f_offset;A賦值讀指針*/offw = f訂ew. f_offset ;/*賦值寫指針*/if (offw+
12、nl-PSIZE) >of f r) /* 不能一次寫完 */if (pipecb3575 pf. p_count=0) /*對文件的復(fù)制操作已進行結(jié) 束,管道文件被釋放*/printfC對文件的復(fù)制操作已進行結(jié)束,管道文件被釋放n);:return 0;elsem 二 PSIZE+offt-offw;/*最多可寫入數(shù)*/for (wstar t 二0; wstartm; wstart+)*(pipecb3575pf. p_addr+offw%PSIZE) = *ca;ca+;offw+;filew.f_offset 二 offw;/*重定位寫指針位置*/nl = nl-m;/*剩余需要
13、讀的字符個數(shù)*/printf ("weak up the read thread , pipe is readablen/z); pthread_cond_signal (ftrflag) ;/*喚醒讀線程,管道可讀*/ printf (z'write thread is blocked and under the statement of waitingn");pthread_cond_wait (&wf lag, &lock) ;/*寫線程封鎖等待*/*一次性可將ca中內(nèi)容全部寫入管道*/offr 二 filer. f_offset;offw 二
14、 filew. f_offset;for(wstart二0;wstartnl;wstart+)/*printf C%dn/Z, pipecb3575pf. p_addr) ;*/ *(pipecb3575pf. p_addr+offw%PSIZE) = *ca;/*printf("%dn",wstart);*/ca+;of f w卄;filew. f_offset 二 offw;pthread_cond_signal(&rflag);printf(”release the lockn");pthread_mutex_unlock(&1ock);pr
15、intf("Secceed (memory>pipe)input data in memory *ca into pipen);return n;/*返回寫入字符個數(shù)*/*讀管道*/int num:read_simu.ate3575;_inode;/*讀管道對應(yīng)的 pipecb3575 表的卜標(biāo)int rstart二0;/*計數(shù)器,讀出字符個數(shù)*/int i 二 0;for (i ; i<NFILE; i+) /*尋找讀管道對應(yīng)的讀管道的端*/if (filei. f_flag=,w' )&&(filei. f_inode=pf)w 二 i;bre
16、ak;elsecontinue;printf (z,add the lockn");pthread_mutex_lock (&lock) ;/*互斥鎖,相當(dāng)于進入臨界區(qū)*/offr 二 filer.f_offset;/*賦值讀指針*/offw = f訂ew. f_offset ;/*賦值寫指針*/if(offr二二offw)/*管道空,無內(nèi)容可讀*/printf("pipe is empty , nothing to outputn");if (pipecb3575 pf. p_count二二 1) /*寫端關(guān)閉*/pipecb3575pf. p_coun
17、t-;/*文件的復(fù)制以完成,釋放管道 文件的空間*/printf("the write point is closed, the copy of the fileis finished'n");return 0;elseprintf(''weak up the write thread , make the pipe writablen/z);pthread_cond_signal (&wflag) ;/*喚醒寫線程,管道可寫*/printf (''read thread is blocked and under the st
18、atement of waitingn");pthread_cond_wait (&rf lag, &lock) ; /*讀線程封鎖等待*/offr 二 filer. f_offset;offw 二 filew. f_offset;m = n<=(offw-offr)n: (offwoffr) ;/*得到可讀字符個數(shù)*/for(rstart二0;rstartm;rstart+)*ca 二 *(pipecb3575pf. p_addr+offr%PSIZE);ca+;of ft卄;filer.f_offset 二 offr;/*重定位讀指針位置*/printf (
19、"weak the write thread , make the pipe writable'rT);pthread_cond_signal(&wflag);printf (”release the lockn);pthread_mutex_unlock(&1ock);printf ("Secceed (pipe>memory) output data from the pipe intomemory *carT);return m;/*線程調(diào)用,讀源文件,寫管道*/void *pwrite3575(void *a)_inode. p_cou
20、nt-;/*文件已讀完,關(guān)閉管道 寫端*/elseperror (args->fname) ; /*打開源文件可能不成功*/return NULL;printf ("Secceed (file>pipe) input data from the original fileinto pipe'rT);return NULL;/*線程調(diào)用,寫目標(biāo)文件,讀管道*/void *pread3575(void *a)name=argv 1 ;/*源文件名*/args0. f=fdl ;/*管道文件寫端*/argsl. fname=argv2 ;/* 目標(biāo)文件名*/argsl.
21、 f=fdO ;/*管道文件讀端*/pthread_create (&t, NULL, pwrite3575, (void *)&args0) 創(chuàng)建了線 程,寫管道*/pread3575(void *)&argsl) ;/*主線程調(diào)用,讀管道*/ pthread_join(t, NULL) ;/*等待寫線程結(jié)束*/close_simulate3575(fd);read_simulate3575write_simulate3575printf("nnumof二 %dnz/, numread_simulate3575);printf("numof二 %d
22、nz/, numwrite_simulate3575);return 0;附錄二(實驗結(jié)果):總用量2379-rwr一一r一一 1JohnJohn502012-12-1718:06 3575-rwr一一r一一 1JohnJohn368642012-07-1507:53-rwr-r- 1 john john 95441 2012-08-27 22:16rwr-r- 1 john john 2083969 2010-11-08 13:07rwr-r- 1 john john 84835 2012-07-11 13:22rwr-r- 1 john john 73728 2012-12-17 16:4
23、8rwr-r- 1 john john 19924 2012-07-03 18:51rwr-r- 1 john john0 2012-12-17 22:49 stalrwr-r- 1 john john 11563 2012-12-17 22:46./tc 3575 a3575 > copyl./tc 3575/tc /tc/tc /tc > copy2 /tcirwi*i*IW Irw rrw rrw rrw r工總用量5163John1John1John1John1John1JohnJohnJohnJohnJohnJohnJohn503686495441208396984835
24、737282012-12-172012-07-152012-08-272010-11-082012-07-112012-12-1718:06 357507:5322:1613:0713:2216:48-rwr-r- 1 john john 19924 2012-07-03 18:51-rwxr-xr-x1JohnJohn50-rwxr-xr-x1JohnJohn36864-rwxr-xr-x1JohnJohn95441-rwxr-xr-x1JohnJohn2083969-rwxr-xr-x1JohnJohn84835-rwxr-xr-x1JohnJohn73728-rwxr-xr-x1John
25、John19924rwri*1JohnJohn1320rwri*1JohnJohn419294rwri*1JohnJohn690rwri*1JohnJohn0-rwxr-xr-x1JohnJohn12256rwri*cat copyl1JohnJohn11563init the u ofile2012-12-1722:51a35752012-12-1722:512012-12-1722:532012-12-1722:542012-12-1722:562012-12-1722:532012-12-1722:552012-12-1722:51copyl2012-12-1722:56copy2201
26、2-12-1722:49stal2012-12-1722:57sta22012-12-1722:50tc2012-12-1722:46start to create a pipeSecceed create the pipe(pipe>file)output data from the pipe into new file(pipe>memory)output data from pipe into memory *caadd the lockpipe is empty , nothing to outputweak up the write thread , make the pipe writableread thread is blocked and under the statement of waiting(file>pipe)input data from the original file into pipe(fi
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 大數(shù)據(jù)營銷策略咨詢合同
- 農(nóng)業(yè)產(chǎn)業(yè)園區(qū)投資合作協(xié)議
- 建筑工程玻璃貼膜合同
- 股權(quán)激勵保密協(xié)議書
- 公關(guān)攝影師聘用合同
- 合伙美容院合同
- 財產(chǎn)分割離婚協(xié)議
- 工業(yè)互聯(lián)網(wǎng)平臺建設(shè)與優(yōu)化方案
- 裝修施工安全合同協(xié)議書
- 醫(yī)院大樓裝修工程承包協(xié)議書
- 上海??茖哟巫灾髡猩荚嚵?xí)題集②(含答案)
- 某市政道路施工交通疏導(dǎo)方案
- 世界主要國際組織課件
- 語言學(xué)綱要(新)課件
- 心理評估與診斷簡介課件
- 移動式壓力容器充裝復(fù)審換證考試重點題庫(180題)
- 小班安全《湯姆走丟了》PPT課件教案反思微視頻
- 作物栽培學(xué)課件棉花
- 最新小學(xué)二年級口算及豎式計算練習(xí)題
- 生產(chǎn)與運作管理-陳榮秋
- 金雞冠的公雞繪本課件
評論
0/150
提交評論