




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、操作系統(tǒng):線程(進程)并發(fā)拷貝程序#include#include#include#include#include #include#include TOC o 1-5 h z #define PSIZE 4096 /* 管道文件的大小*/#define BSIZE 128#define NOFILE 20#define NFILE 20#define NPIPE 20/* 默認緩沖區(qū)的大小*/*u_ofile3705 表可分配的個數(shù)*/*file 表可分配的個數(shù)*/*pipecb3705 可分配的個數(shù)*/ /* 進程的 u_file 表 */ int u_ofile3705NOFILE; /
2、*模擬 file 表 */ struct char f_flag;/* 讀寫標志,w表示寫,r表示讀*/int f_count;/* 表示此表項的狀態(tài),=0表示此表項沒被使用,可分配;=1 表示此表項在被使用,不可再分配*/int f_inode;/* 對應的 pipecb3705 表下標 */long f_offset;/* 讀寫指針,當前已讀或已寫個數(shù)*/fileNFILE;/* 管道控制塊*/struct char *p_addr;/* 管道文件基地址*/int p_size;/*管道文件大小,PSIZE*/int p_count;/*=2 表示讀寫都在被進行,=1 表示在被讀或被寫,=
3、0 表示管道沒被使用,可分配 */pipecb3705NPIPE; TOC o 1-5 h z /* 模擬管道文件*/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;/* 寫信號量*/* 線程創(chuàng)建函數(shù)只能傳一個參數(shù),用
4、結構體來封裝所有參數(shù)*/struct arg_setchar *fname; /* 文件名 */int f;/* 傳遞 fdp*/;/*u_ofile3705 表初始化*/int u_ofile_init3705()printf(init the u_ofile3705n);int i;for(i=0;iNOFILE;i+)u_ofile3705i = -1;u_ofile37050=0;u_ofile37051=0;u_ofile37052=0;return 0;/* 創(chuàng)建管道*/int pipe_simulate3705(int a)printf(start to create a pip
5、en);int i;for(i=0;iNOFILE;i+)if(u_ofile3705i=-1)a0 = i;/* 讀 */u_ofile3705i = 0;/* 讀端 */ break;for(i;i=NOFILE)printf(u_ofile3705 分配失敗,failure to create a pipen);return -2;pfile = (char *)malloc(PSIZE*sizeof(char);/* 申請模擬管道用的內存空間*/if(pfile=NULL)/* 申請可能不成功*/printf(failure to create a pipen);return -1;f
6、or(i=0;iNFILE;i+)if(filei.f_count!=1)filei.f_flag = r;/* 讀標志 */filei.f_inode = 0;/* 讀對應 pipecb3705 表下標 */filei.f_count = 1;/*file0 這個表項在被使用,不可再分配*/filei.f_offset = 0;/* 讀指針 */u_ofile3705a0 = i;/* 讀端 */ break;for(i=0;i=NFILE)printf(failure to create a pipen);return -1;for(i=0;i=NPIPE)printf(failure t
7、o create a pipen);return -1;printf(Secceed create the pipen);return 0;/* 分配成功*/* 關閉管道*/int close_simulate3705(int a)printf(start to close the pipe you have createdn);char *p;int i;for(i=0;ipipe)input data in memory *ca into pipen);numwrite_simulate3705+; TOC o 1-5 h z long offr,offw;/* 讀寫指針,實際是讀寫字符個
8、數(shù)*/int r;/* 管道文件讀端*/int m;/* 若 ca中的字符不能一次寫完,m 用來表示一次可寫入的字符的最大數(shù)*/int w = u_ofile3705fd;/* 管道文件寫端*/int pf = filew.f_inode;/* 讀管道對應的pipecb3705 表的下標*/int n1 = n;/* 一次應該寫入的字符個數(shù)*/int wstart = 0;/* 計數(shù)器,寫入字符個數(shù)*/int i = 0;for(i;ioffr)/* 不能一次寫完*/*/ if(pipecb3705pf.p_count=0)/* 對文件的復制操作已進行結束,管道文件被釋放 printf( 對文
9、件的復制操作已進行結束,管道文件被釋放n);return 0; else m = PSIZE+offr-offw;/* 最多可寫入數(shù)*/for(wstart=0;wstartm;wstart+) *(pipecb3705pf.p_addr+offw%PSIZE) = *ca; ca+; offw+; filew.f_offset = offw;/* 重定位寫指針位置*/ TOC o 1-5 h z n1 = n1-m;/* 剩余需要讀的字符個數(shù)*/printf(weak up the read thread , pipe is readablen); pthread_cond_signal(&
10、rflag);/* 喚醒讀線程,管道可讀*/printf(write thread is blocked and under the statement of waitingn); pthread_cond_wait(&wflag,&lock);/* 寫線程封鎖等待*/*一次性可將ca中內容全部寫入管道*/offr = filer.f_offset;offw = filew.f_offset;for(wstart=0;wstartpipe)input data in memory *ca into pipen);return n;/* 返回寫入字符個數(shù)*/* 讀管道 */ int numrea
11、d_simulate3705;/read_simulate3705 diaoyongshuint read_simulate3705(int fd,char *ca,int n)/ 將管道 fd 內容讀取n 個字符到內存ca 里printf(pipememory)output data from pipe into memory *can);numread_simulate3705+; TOC o 1-5 h z long offr,offw;/* 讀寫指針,實際是讀寫字符個數(shù)*/int w;/* 管道文件寫端*/int m;/* 若 ca中的字符不能一次讀完,m 用來表示一次可讀出的字符的最
12、大數(shù)*/int r = u_ofile3705fd;/* 管道文件讀端*/int pf = filer.f_inode;/* 讀管道對應的pipecb3705 表的下標*/int rstart = 0;/* 計數(shù)器,讀出字符個數(shù)*/int i = 0;for(i;iNFILE;i+)/* 尋找讀管道對應的讀管道的端*/if(filei.f_flag=w)&(filei.f_inode=pf)w = i;break; else continue;printf(add the lockn); TOC o 1-5 h z pthread_mutex_lock(&lock);/* 互斥鎖,相當于進入臨
13、界區(qū)*/offr = filer.f_offset;/* 賦值讀指針*/offw = filew.f_offset;/* 賦值寫指針*/if(offr=offw)/* 管道空,無內容可讀*/ printf(pipe is empty , nothing to outputn); if(pipecb3705pf.p_count=1)/* 寫端關閉*/ pipecb3705pf.p_count-;/* 文件的復制以完成,釋放管道文件的空間*/printf(the write point is closed,the copy of the file is finishedn); return 0;e
14、lseprintf(weak up the write thread , make the pipe writablen); pthread_cond_signal(&wflag);/* 喚醒寫線程,管道可寫*/printf(read thread is blocked and under the statement of waitingn);pthread_cond_wait(&rflag,&lock);/* 讀線程封鎖等待*/offr = filer.f_offset;offw = filew.f_offset;m = n=(offw-offr)?n:(offw-offr);/* 得到可讀
15、字符個數(shù)*/for(rstart=0;rstartmemory)output data from the pipe into memory *can);return m;/* 線程調用,讀源文件,寫管道*/void *pwrite3705(void *a)/ 將源文件a 內容寫入管道中printf(filepipe)input data from the original file into pipen);char abuf1BSIZE; TOC o 1-5 h z struct arg_set *args=(struct arg_set *)a;/* 需要傳入多個參數(shù)時,用結構體傳*/int
16、 fdr;int n_r;/* 管道文件寫入字符數(shù)*/if(fdr=open(args-fname,O_RDONL Y)!=-1)while(n_r=read(fdr,abuf1,BSIZE)0)/* 讀文件,寫管道*/printf(filememory)input data in original file into memory *can);printf(Secceed (filememory)input data in original file into memory *can);/printf(memorypipe)input data in memory *ca into pipe
17、n);write_simulate3705(args-f,abuf1,n_r);/printf(Secceed (memorypipe)inputn);pipecb3705fileu_ofile3705args-f.f_inode.p_count-;/* 文件已讀完,關閉管道寫端 */else perror(args-fname);/* 打開源文件可能不成功*/return NULL;printf(Secceed (filepipe)input data from the original file into pipen);return NULL;/* 線程調用,寫目標文件,讀管道*/void
18、 *pread3705(void *a)/ 讀取管道,將其中內容寫入新建文件中printf(pipefile)output data from the pipe into new filen);char abuf2BSIZE;/* 緩沖區(qū) */ TOC o 1-5 h z struct arg_set *args=(struct arg_set *)a;/* 需要傳入多個參數(shù)時,用結構體傳*/int fdw;int n_w;/* 管道文件讀出字符數(shù)*/if(fdw=open(args-fname,O_CREAT|O_RDWR,0777)!=-1)while(n_w=read_simulate3
19、705(args-f,abuf2,BSIZE)0)/* 讀管道,寫文件*/printf(pipememory)output data from pipe into memory *can);/printf(Secceed (pipememory)output data from pipe into memory *can);printf(memoryfile)output data in memory *ca into filen);write(fdw,abuf2,n_w);printf(Secceed (memoryfile)output data from the pipe into ne
20、w filen);else perror(args-fname);/* 打開目標文件可能出錯*/return NULL; printf(Secceed (pipefile)output data from the pipe into new filen);return NULL;/* 主函數(shù) */int main(int argc,char *argv)int x;u_ofile_init3705(); TOC o 1-5 h z while(x=pipe_simulate3705(fd)=-1);/* 創(chuàng)建管道,即申請空間*/if(x=-2)return -1;pthread_t t;/th
21、read IDstruct arg_set args2;/*用結構體傳寫線程需要的參數(shù):文件名,管道文件讀寫端*/args0.fname=argv1;/* 源文件名*/args0.f=fd1;/* 管道文件寫端*/args1.fname=argv2;/* 目標文件名*/args1.f=fd0;/* 管道文件讀端*/pthread_create(&t,NULL,pwrite3705,(void *)&args0);/* 創(chuàng)建子線程,寫管道 */pread3705(void *)&args1);/* 主線程調用,讀管道*/pthread_join(t,NULL);/* 等待寫線程結束*/close
22、_simulate3705(fd);printf(nnum of read_simulate3705 = %dn,numread_simulate3705);printf(num of write_simulate3705 = %dn,numwrite_simulate3705);return 0;ls -l總用量 2379rw-r-r- 1 john johnrw-r-r- 1 john johnrw-r-r- 1 john john50 2012-12-17 18:06 370536864 2012-07-15 07:53 3705.doc95441 2012-08-27 22:16 37
23、05.JPG -rw-r-r- 1 john john 2083969 2010-11-08 13:07 3705.mp3rw-r-r- 1 john johnrw-r-r- 1 john johnrw-r-r- 1 john johnrw-r-r- 1 john johnrw-r-r- 1 john john84835 2012-07-11 13:22 3705.pdf73728 2012-12-17 16:48 3705.ppt19924 2012-07-03 18:51 3705.rar0 2012-12-17 22:49 sta111563 2012-12-17 22:46 tc_co
24、py.cpp./tc 3705 a3705 copy1./tc 3705 a3705.doc./tc 3705.doc a3705.doc./tc 3705.ppt a3705.ppt./tc 3705.mp3 a3705.mp3./tc 3705.rar a3705.rar copy2./tc 3705.pdf a3705.rarls -l總用量 5163-rw-r-r- 1 john john-rw-r-r- 1 john john-rw-r-r- 1 john john50 2012-12-17 18:06 370536864 2012-07-15 07:53 3705.doc95441
25、 2012-08-27 22:16 3705.JPG-rw-r-r- 1 john john 2083969 2010-11-08 13:07 3705.mp3-rw-r-r- 1 john john-rw-r-r- 1 john john-rw-r-r- 1 john john-rwxr-xr-x 1 john john-rwxr-xr-x 1 john john-rwxr-xr-x 1 john john84835 2012-07-11 13:22 3705.pdf73728 2012-12-17 16:48 3705.ppt19924 2012-07-03 18:51 3705.rar5
26、0 2012-12-17 22:51 a370536864 2012-12-17 22:51 a3705.doc95441 2012-12-17 22:53 a3705.JPG-rwxr-xr-x 1 john john 2083969 2012-12-17 22:54 a3705.mp3-rwxr-xr-x 1 john john-rwxr-xr-x 1 john john-rwxr-xr-x 1 john johnrw-r-r- 1 john johnrw-r-r- 1 john johnrw-r-r- 1 john johnrw-r-r- 1 john john84835 2012-12
27、-17 22:56 a3705.pdf73728 2012-12-17 22:53 a3705.ppt19924 2012-12-17 22:55 a3705.rar1320 2012-12-17 22:51 copy1419294 2012-12-17 22:56 copy2690 2012-12-17 22:49 sta10 2012-12-17 22:57 sta2-rwxr-xr-x 1 john john-rw-r-r- 1 john john12256 2012-12-17 22:50 tc11563 2012-12-17 22:46 tc_copy.cppcat copy1init the u_ofilestart to create a pipeSecceed create the pipe(pipefile)output data from the pipe into new file(pipememory)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 a
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二手車現(xiàn)場評估準備工作試題及答案
- 寵物營養(yǎng)師考試知識點解析試題及答案
- 2024年美容師誠信經營考量試題及答案
- 2024年小自考行政管理考試重點試題試題及答案
- 汽車美容器械的維護與保養(yǎng)試題及答案
- 2024-2025學年內蒙古巴彥淖爾一中高一下學期第一次學業(yè)診斷生物及答案
- 2024年計算機基礎考試知識整合試題及答案
- 關于寵物營養(yǎng)師職業(yè)倫理的討論試題及答案
- 美容師考試對個人素養(yǎng)要求分析試題及答案
- 2024計算機基礎考試突破技巧試題及答案
- 張朋《了凡四訓》課件
- 2023年4月全國自學考試00147人力資源管理一試題及答案
- 生藥學全套課件
- 廣東省五年一貫制語文考試題目
- 幼兒園家長進課堂講課
- 建筑工程管理畢業(yè)論文
- 全國公務員管理信息系統(tǒng)
- 工程機械設計-陳海虹課件第5章-輪式工程機械轉向系
- 脊髓損傷康復臨床路徑
- UV打印機日常維護與保養(yǎng)
- GB/T 2652-2022金屬材料焊縫破壞性試驗熔化焊接頭焊縫金屬縱向拉伸試驗
評論
0/150
提交評論