操作系統(tǒng)實(shí)驗(yàn)報(bào)告 線程并發(fā)拷貝程序_第1頁(yè)
操作系統(tǒng)實(shí)驗(yàn)報(bào)告 線程并發(fā)拷貝程序_第2頁(yè)
操作系統(tǒng)實(shí)驗(yàn)報(bào)告 線程并發(fā)拷貝程序_第3頁(yè)
操作系統(tǒng)實(shí)驗(yàn)報(bào)告 線程并發(fā)拷貝程序_第4頁(yè)
操作系統(tǒng)實(shí)驗(yàn)報(bào)告 線程并發(fā)拷貝程序_第5頁(yè)
已閱讀5頁(yè),還剩13頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、操作系統(tǒng):線程(進(jìn)程)并發(fā)拷貝程序附錄一:程序代碼#include #include #include #include #include #include #include #define PSIZE 4096 /* #define BSIZE 128 /*管道文件的大小 */ 默認(rèn)緩沖區(qū)的大小 */ ofile3705 表可分配的個(gè)數(shù) */ /*file 表可分配的個(gè)數(shù) */#define NOFILE 20 /*u#define NFILE 20#define NPIPE 20 /* 進(jìn)程的 u_file int u_ofile3705NOFILE;/* 模擬 file 表 */ str

2、uct char f_flag;/*int f_count;/* 在被使用,不可再分配 */int f_inode;/* long f_offset;/*fileNFILE;/* 管道控制塊 */ struct char *p_addr;/*int p_size;/*int p_count;/*=2 表示讀寫都在被進(jìn)行, =1 表示在被讀或被寫, 可分配 */ pipecb3705NPIPE;/* 模擬管道文件 */ char *pfile;/* 管道的寫入寫出端 */int fd2;/* 鎖機(jī)制,實(shí)現(xiàn)互斥 */ pthread_mutex_t lock = PTHREAD_MUTEX_INI

3、TIALIZER;/* 進(jìn)程間通信,實(shí)現(xiàn)同步 */ pthread_cond_t rflag = PTHREAD_COND_INITIALIZER;/* pthread_cond_t wflag = PTHREAD_COND_INITIALIZER;/*/*pipecb3705 可分配的個(gè)數(shù) */ 表*/讀寫標(biāo)志, w 表示寫, r 表示讀 */表示此表項(xiàng)的狀態(tài), =0 表示此表項(xiàng)沒被使用,可分配; =1 表示此表項(xiàng)對(duì)應(yīng)的 pipecb3705 表下標(biāo) */讀寫指針,當(dāng)前已讀或已寫個(gè)數(shù)管道文件基地址 */ 管道文件大小, PSIZE*/*/讀信號(hào)量 寫信號(hào)量*/*/=0 表示管道沒被使用,*/

4、* 線程創(chuàng)建函數(shù)只能傳一個(gè)參數(shù),用結(jié)構(gòu)體來封裝所有參數(shù) struct arg_set文件名 */ 傳遞 fdp*/char *fname; /*int f; /*;/*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(sta

5、rt to create a pipen);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)/* 申請(qǐng)可能不成功 */ printf(failure to create a pipen)

6、;return -1; for(i=0;iNFILE;i+)if(filei.f_count!=1)filei.f_flag = r;/*/filei.f_inode = 0;/* 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 to create a pipen);return -1;printf(Secce

7、ed create the pipen);return 0;/* 分配成功 */* 關(guān)閉管道 */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+;long offr,offw;/* 讀寫指針,實(shí)際是讀寫字符個(gè)數(shù) */ int r;/* 管道文件讀端 */int m;/*int w = u_ofile3705fd

8、;/*int pf = filew.f_inode;/*int n1 = n;/*int wstart = 0;/*int i = 0;for(i;ioffr)/*對(duì)文件的復(fù)制操作已進(jìn)行結(jié)束,管道文件被釋 if(pipecb3705pf.p_count=0)/* 放*/對(duì)文件的復(fù)制操作已進(jìn)行結(jié)束,管道文件被釋放 n);printf( return 0;elsem = PSIZE+offr-offw;/*最多可寫入數(shù) */for(wstart=0;wstartm;wstart+) *(pipecb3705pf.p_addr+offw%PSIZE) = *ca; ca+;offw+;filew.f

9、_offset = offw;/*重定位寫指針位置 */n1 = n1-m;/* 剩余需要讀的字符個(gè)數(shù) */ printf(weak up the read thread , pipe is readablen); pthread_cond_signal(&rflag);/* 喚醒讀線程,管道可讀 */ printf(write thread is blocked and under the statement of waitingn);寫線程封鎖等待 */pthread_cond_wait(&wflag,&lock);/* /* 一次性可將 ca 中內(nèi)容全部寫入管道 */ offr = fi

10、ler.f_offset; offw = filew.f_offset; for(wstart=0;wstartpipe)input data in memory *ca into pipen);return n;/* 返回寫入字符個(gè)數(shù) */ /* 讀管道 */ int numread_simulate3705;/read_simulate3705 diaoyongshu int read_simulate3705(int fd,char *ca,int n)/將管道 fd 內(nèi)容讀取 n 個(gè)字符到內(nèi)存 ca里 printf(pipememory)output data from pipe in

11、to memory *can);讀寫指針,實(shí)際是讀寫字符個(gè)數(shù) */ 管道文件寫端 */ 若 ca 中的字符不能一次讀完, m 用來表示一次可讀出的字符的最大數(shù) */ 管道文件讀端 */讀管道對(duì)應(yīng)的 pipecb3705 表的下標(biāo) */ 計(jì)數(shù)器,讀出字符個(gè)數(shù) */numread_simulate3705+; long offr,offw;/* int w;/* int m;/*int r = u_ofile3705fd;/* int pf = filer.f_inode;/* int rstart = 0;/* int i = 0;for(i;iNFILE;i+)/*尋找讀管道對(duì)應(yīng)的讀管道的端

12、*/if(filei.f_flag=w)&(filei.f_inode=pf) w = i; break; else continue;printf(add the lockn);pthread_mutex_lock(&lock);/*互斥鎖,相當(dāng)于進(jìn)入臨界區(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-;/* prin

13、tf(the write point return 0; else printf(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-o

14、ffr)?n:(offw-offr);/* for(rstart=0;rstartpipe)input data from the original file into pipen);char abuf1BSIZE;struct arg_set *args=(struct arg_set *)a;/* int fdr;int n_r;/* 管道文件寫入字符數(shù) */ if(fdr=open(args-fname,O_RDONLY)!=-1) while(n_r=read(fdr,abuf1,BSIZE)0)/*printf(filememory)input*can); printf(Seccee

15、d memory *can);(pipememory)output data from the pipe into memory *can);需要傳入多個(gè)參數(shù)時(shí),用結(jié)構(gòu)體傳 */讀文件,寫管道data in original(filememory)inputdata in/printf(memorypipe)input data in memory *ca into pipen);write_simulate3705(args-f,abuf1,n_r);/printf(Secceed (memorypipe)inputn);pipecb3705fileu_ofile3705args-f.f_i

16、node.p_count-;/* 關(guān)閉管道寫端 */ else perror(args-fname);/* return NULL; printf(Secceed return NULL;/* 線程調(diào)用,寫目標(biāo)文件,讀管道 */void *pread3705(void *a)/讀取管道,將其中內(nèi)容寫入新建文件中printf(pipefile)output data from the pipe into new filen);char abuf2BSIZE;/* 緩沖區(qū) */ struct arg_set *args=(struct arg_set *)a;/* int fdw;int n_w;

17、/* 管道文件讀出字符數(shù) */ if(fdw=open(args-fname,O_CREAT|O_RDWR,0777)!=-1) 打開源文件可能不成功 */(filepipe)inputdata from the original*/fileoriginalinto memoryfile into文件已讀完,file into pipen);需要傳入多個(gè)參數(shù)時(shí),用結(jié)構(gòu)體傳 */讀管道,寫文件 */while(n_w=read_simulate3705(args-f,abuf2,BSIZE)0)/*/printf(pipememory)output data from pipe into mem

18、ory *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 newfilen);else打開目標(biāo)文件可能出錯(cuò) */ perror(args-fname);/* return NULL;printf(Secceed (pipefile)o

19、utput data from the pipe into new filen);return NULL;/* 主函數(shù) */int main(int argc,char *argv)int x; u_ofile_init3705();創(chuàng)建管道,即申請(qǐng)空間 */while(x=pipe_simulate3705(fd)=-1);/* if(x=-2)return -1;用結(jié)構(gòu)體傳寫線程需要的參數(shù) :文件名 ,管道文件讀寫端 */ 源文件名 */ 管道文件寫端 */目標(biāo)文件名 */ 管道文件讀端 */pthread_t t;/thread ID struct arg_set args2;/* ar

20、gs0.fname=argv1;/* args0.f=fd1;/* args1.fname=argv2;/* args1.f=fd0;/*創(chuàng)建子線程 , 寫管道 */pthread_create(&t,NULL,pwrite3705,(void *)&args0);/* pread3705(void *)&args1);/*主線程調(diào)用 , 讀管道 */pthread_join(t,NULL);/* 等待寫線程結(jié)束 */ close_simulate3705(fd);printf(nnum of read_simulate3705 = %dn,numread_simulate3705); pri

21、ntf(num of write_simulate3705 = %dn,numwrite_simulate3705); return 0;附錄二(實(shí)驗(yàn)結(jié)果)ls -l總用量 2379-rw-r-r-rw-r-r-1 john john1 john john50 2012-12-17 18:06 370536864 2012-07-15 07:53 3705.doc-rw-r-r-95441 2012-08-27 22:16 3705.JPG-rw-r-r-rw-r-r-rw-r-r-rw-r-r-rw-r-r-rw-r-r-1 john john1 john john 2083969 2010

22、-11-08 13:07 3705.mp31 john john1 john john1 john john1 john john1 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_copy.cpp./tc 3705 a3705 copy1./tc 3705 a3705.doc./tc 3705.doc a3705.doc./tc 3705.p

23、pt a3705.ppt./tc 3705.mp3 a3705.mp3./tc 3705.rar a3705.rar copy2./tc 3705.pdf a3705.rarls -l總用量 5163-rw-r-r-rw-r-r-1 john john1 john john50 2012-12-17 18:06 370536864 2012-07-15 07:53 3705.doc-rw-r-r-95441 2012-08-27 22:16 3705.JPG-rw-r-r-rw-r-r-rw-r-r-rw-r-r-rwxr-xr-x-rwxr-xr-x1 john john1 john joh

24、n 2083969 2010-11-08 13:07 3705.mp31 john john1 john john1 john john1 john john1 john john84835 2012-07-11 13:22 3705.pdf73728 2012-12-17 16:48 3705.ppt19924 2012-07-03 18:51 3705.rar50 2012-12-17 22:51 a370536864 2012-12-17 22:51 a3705.doc-rwxr-xr-x95441 2012-12-17 22:53 a3705.JPG-rwxr-xr-x-rwxr-xr

25、-x-rwxr-xr-x-rwxr-xr-x1 john john1 john john 2083969 2012-12-17 22:54 a3705.mp31 john john1 john john1 john john84835 2012-12-17 22:56 a3705.pdf73728 2012-12-17 22:53 a3705.ppt19924 2012-12-17 22:55 a3705.rar-rw-r-r-1320 2012-12-17 22:51 copy1-rw-r-r-1 john john1 john john 419294 2012-12-17 22:56 co

26、py21 john john1 john john -rwxr-xr-x 1 john john-rw-r-r-rw-r-r-rw-r-r- 1 john john690 2012-12-17 22:49 sta10 2012-12-17 22:57 sta212256 2012-12-17 22:50 tc11563 2012-12-17 22:46 tc_copy.cppcat copy1 init the u_ofile start to create a pipe Secceed create the pipe (pipefile)output data from the pipe into new file(pipememory)output data from pipe into memory *caadd the lock pipe is empty , nothing to output weak up the write thread , make the pipe writable read thread is blocked and under the statement of waiting (filepipe

溫馨提示

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

評(píng)論

0/150

提交評(píng)論