


下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、操作系統(tǒng):線程(進程)并發(fā)拷貝程序 附錄一:程序代碼 #include #include<sys/> #include<sys/> #include<> #include<>#include<>#include<>#definePSIZE 4096/*管道文件的大小*/#defineBSIZE 128/*默認緩沖區(qū)的大小*/#defineNOFILE 20/*u_ofile3575表可分配的個數(shù)*/#defineNFILE 20Afile表可分配的個數(shù)*/#defineNPIPE 20/*pipecb3575可分配的個數(shù)*
2、/*進程的u_file表*/int u_ofile3575NOFILE;/*模擬file表*/struetchar f_flag;/*讀寫標志,'w'表示寫,':r'表示讀*/可分配;int f_count;/*表示此表項的狀態(tài),二0表示此表項沒被使用,二1表示此表項在被使用,不可再分配*/int f_inode;/*對應的 pipecb3575 表下標*/long f_offset;/*讀寫指針,當前已讀或已寫個數(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ù),用結構體來封裝所有參數(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) ;/*申請模擬管道用的內存空間*/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' ;/*讀標志*/_inode = 0;/*讀對應 pipecb3575 表下標*/filei. f_count = l;/*file0這個表項在被使用,不可再分配*/f訂ei.f_offset 二
7、0;/*讀指針*/u_ofile3575a0 = i;/* 讀端 */break;if (filei. f_count!二 1)filei. f_flag = ' w' ;/*寫標志*/_inode = 0;/*寫對應pipecb3575控制塊卜標*/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;/*分配成功*/*關閉管道*/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;/*讀管道對應的 pipecb3575表的卜標*/int nl二n;/*次應該寫入的字符個數(shù)*
11、/int wstart二0;/*計數(shù)器,寫入字符個數(shù)*/int i 二 0;for(i;iNFILE;i+)/*尋找寫管道對應的讀管道的讀端*/if (file i. f_flag=,r' )&&(filei. f_inode 二二 pf)break;elsecontinue;printf (z,add the lockn");pthread_mutex_lock(&lock) ;/*互斥鎖,相當于進入臨界區(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) /*對文件的復制操作已進行結 束,管道文件被釋放*/printfC對文件的復制操作已進行結束,管道文件被釋放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中內容全部寫入管道*/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;/*讀管道對應的 pipecb3575 表的卜標int rstart二0;/*計數(shù)器,讀出字符個數(shù)*/int i 二 0;for (i ; i<NFILE; i+) /*尋找讀管道對應的讀管道的端*/if (filei. f_flag=,w' )&&(filei. f_inode=pf)w 二 i;bre
16、ak;elsecontinue;printf (z,add the lockn");pthread_mutex_lock (&lock) ;/*互斥鎖,相當于進入臨界區(qū)*/offr 二 filer.f_offset;/*賦值讀指針*/offw = f訂ew. f_offset ;/*賦值寫指針*/if(offr二二offw)/*管道空,無內容可讀*/printf("pipe is empty , nothing to outputn");if (pipecb3575 pf. p_count二二 1) /*寫端關閉*/pipecb3575pf. p_coun
17、t-;/*文件的復制以完成,釋放管道 文件的空間*/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;/*線程調用,讀源文件,寫管道*/void *pwrite3575(void *a)_inode. p_cou
20、nt-;/*文件已讀完,關閉管道 寫端*/elseperror (args->fname) ; /*打開源文件可能不成功*/return NULL;printf ("Secceed (file>pipe) input data from the original fileinto pipe'rT);return NULL;/*線程調用,寫目標文件,讀管道*/void *pread3575(void *a)name=argv 1 ;/*源文件名*/args0. f=fdl ;/*管道文件寫端*/argsl. fname=argv2 ;/* 目標文件名*/argsl.
21、 f=fdO ;/*管道文件讀端*/pthread_create (&t, NULL, pwrite3575, (void *)&args0) 創(chuàng)建了線 程,寫管道*/pread3575(void *)&argsl) ;/*主線程調用,讀管道*/ pthread_join(t, NULL) ;/*等待寫線程結束*/close_simulate3575(fd);read_simulate3575write_simulate3575printf("nnumof二 %dnz/, numread_simulate3575);printf("numof二 %d
22、nz/, numwrite_simulate3575);return 0;附錄二(實驗結果):總用量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)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年09月河南滎陽市招聘特崗全科醫(yī)生4人筆試歷年專業(yè)考點(難、易錯點)附帶答案詳解
- 《深海探險研究小組》課件
- 2025太陽能發(fā)電承包合同的示例文本
- 動漫視頻制作服務合同(2篇)
- 2025年技術與產品的許可使用和補償貿易合作生產合同
- 《電路》課件理想變壓器及三相電路
- 橡膠零件的熱塑性彈性體應用考核試卷
- 2024年09月江蘇南通市崇川區(qū)衛(wèi)生健康委員會所屬部分醫(yī)療衛(wèi)生事業(yè)單位招聘52人筆試歷年專業(yè)考點(難、易錯點)附帶答案詳解
- 2025藥店租賃合同書范本
- 2024年09月廣西北海市銀海區(qū)銀灘鎮(zhèn)中心衛(wèi)生院后勤人員招聘1人筆試歷年專業(yè)考點(難、易錯點)附帶答案詳解
- GB/T 12939-2002工業(yè)車輛輪輞規(guī)格系列
- 送元二使安西公開課課件
- 資源昆蟲學-傳粉昆蟲
- 壓花藝術課件
- DB32T4220-2022消防設施物聯(lián)網系統(tǒng)技術規(guī)范-(高清版)
- 兒童抑郁量表CDI
- 生物化學-脂類課件
- Q∕SY 02098-2018 施工作業(yè)用野營房
- DB62∕T 3176-2019 建筑節(jié)能與結構一體化墻體保溫系統(tǒng)應用技術規(guī)程
- 八大特殊危險作業(yè)危險告知牌
- 半橋LLC諧振變換器設計與仿真
評論
0/150
提交評論