操作系统实验报告线程并发拷贝程序.doc

上传人:scccc 文档编号:13786746 上传时间:2022-01-23 格式:DOC 页数:27 大小:148KB
返回 下载 相关 举报
操作系统实验报告线程并发拷贝程序.doc_第1页
第1页 / 共27页
操作系统实验报告线程并发拷贝程序.doc_第2页
第2页 / 共27页
操作系统实验报告线程并发拷贝程序.doc_第3页
第3页 / 共27页
亲,该文档总共27页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《操作系统实验报告线程并发拷贝程序.doc》由会员分享,可在线阅读,更多相关《操作系统实验报告线程并发拷贝程序.doc(27页珍藏版)》请在三一文库上搜索。

1、操作系统:线程(进程)并发拷贝程序 附录一:程序代码 #include #include #include #include #include#include#include#definePSIZE 4096/*管道文件的大小*/#defineBSIZE 128/*默认缓冲区的大小*/#defineNOFILE 20/*u_ofile3575表可分配的个数*/#defineNFILE 20Afile表可分配的个数*/#defineNPIPE 20/*pipecb3575可分配的个数*/*进程的u_file表*/int u_ofile3575NOFILE;/*模拟file表*/struetcha

2、r f_flag;/*读写标志,w表示写,:r表示读*/可分配;int f_count;/*表示此表项的状态,二0表示此表项没被使用,二1表示此表项在被使用,不可再分配*/int f_inode;/*对应的 pipecb3575 表下标*/long f_offset;/*读写指针,当前已读或已写个数*/fileNFILE;/*管道控制块*/struetchar *p_addr; /*管道文件基地址*/int p_size;/*管道文件大小,PSIZE*/int p_count;/*二2表示读写都在被进行,二1表示在被读或被写,二0表 示管道没被使用,可分配*/pipecb3575NPIPE;/

3、*模拟管道文件*/char *pfile;/*管道的写入写出端*/int fd2;/*锁机制,实现互斥*/pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER:/*进程间通信,实现同步*/pthread_cond_t rflag = PTHREAD_COND_INITIALIZER;/*读信号量*/ pthread_cond_t wflag = PTHREAD_COND_INITIALIZER;/*写信号量*/*线程创建函数只能传一个参数,用结构体来封装所有参数*/struet arg_setchar *fname;/*文件名 */int f;/*传

4、递 fdp*/;/*u_ofile3575 表初始化*/int u_of订e_init3575()printf(init the u_of订e3575n); int i;for(i=0;iN0FILE;i+)u_ofile3575i二 T;u_ofile35750=0;u_ofile3575l=0;u_ofile35752=0;return 0;/*创建管道*/int pipe_simulate3575(int a)printf(start to create a pipen); int i;if(u_ofile3575i=-l)a0=匚/*读*/u_ofil e3575i = 0;/* 读端

5、 */break;for(i;i=N0FILE)printf (u_ofile3575 分酉己失败,failure to create a pipen);:return 2;pfile = (char *)malloc (PSIZE*sizeof (char) ;/*申请模拟管道用的内存空间*/if (pf i le二二NULL) /*申请可能不成功*/printf (failure to create a pipen);return -1;for(i=0;iNFILE;i+)if(filei. f_count!二1)f订ei. f_flag = r ;/*读标志*/_inode = 0;/*

6、读对应 pipecb3575 表下标*/filei. f_count = l;/*file0这个表项在被使用,不可再分配*/f订ei.f_offset 二 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

7、;if(i二NFILE)printf (failure to create a pipen);:return -1;for(i=0;i=NPIPE)printf (failure to create 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_ofile35

8、75ai . f_inode. p_addr;/*if(p!=NULL)free(p) ; _inode. p_count = 0; /*管道控制块计数清零*/ fileu_ofile3575ai. f_count = 0; /*file 表项计数清零*/ u_ofile3575ai = T; /*u_ofile3575 表项清空*/ ai = -1; /*fd讥清空*/printf(secceed close the pipen);return 0;/*写管道*/int numwrite_simulate3575;int write_simulate3575 (int fd, char *c

9、a, int n)_inode;/*读管道对应的 pipecb3575表的卜标*/int nl二n;/*次应该写入的字符个数*/int wstart二0;/*计数器,写入字符个数*/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) ;/*互斥锁,相当于进入临界区*/offr 二 filer.f_offset;A赋值读指针*/of

10、fw = f订ew. f_offset ;/*赋值写指针*/if (offw+nl-PSIZE) of f r) /* 不能一次写完 */if (pipecb3575 pf. p_count=0) /*对文件的复制操作已进行结 束,管道文件被释放*/printfC对文件的复制操作已进行结束,管道文件被释放n);:return 0;elsem 二 PSIZE+offt-offw;/*最多可写入数*/for (wstar t 二0; wstartm; wstart+)*(pipecb3575pf. p_addr+offw%PSIZE) = *ca;ca+;offw+;filew.f_offset

11、二 offw;/*重定位写指针位置*/nl = nl-m;/*剩余需要读的字符个数*/printf (weak up the read thread , pipe is readablen/z); pthread_cond_signal (ftrflag) ;/*唤醒读线程,管道可读*/ printf (zwrite thread is blocked and under the statement of waitingn);pthread_cond_wait (&wf lag, &lock) ;/*写线程封锁等待*/*一次性可将ca中内容全部写入管道*/offr 二 filer. f_off

12、set;offw 二 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);printf(Secceed (me

13、morypipe)input data in memory *ca into pipen);return n;/*返回写入字符个数*/*读管道*/int num:read_simu.ate3575;_inode;/*读管道对应的 pipecb3575 表的卜标int rstart二0;/*计数器,读出字符个数*/int i 二 0;for (i ; iNFILE; i+) /*寻找读管道对应的读管道的端*/if (filei. f_flag=,w )&(filei. f_inode=pf)w 二 i;break;elsecontinue;printf (z,add the lockn);pth

14、read_mutex_lock (&lock) ;/*互斥锁,相当于进入临界区*/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_count-;/*文件的复制以完成,释放管道 文件的空间*/printf(the write point is closed, the co

15、py of the fileis finishedn);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 statement of waitingn);pthread_cond_wait (&rf lag, &lock) ; /*读线程封锁等待*/offr 二 filer. f_offset;offw 二 filew. f

16、_offset;m = nmemory) output data from the pipe intomemory *carT);return m;/*线程调用,读源文件,写管道*/void *pwrite3575(void *a)_inode. p_count-;/*文件已读完,关闭管道 写端*/elseperror (args-fname) ; /*打开源文件可能不成功*/return NULL;printf (Secceed (filepipe) input data from the original fileinto piperT);return NULL;/*线程调用,写目标文件,

17、读管道*/void *pread3575(void *a)name=argv 1 ;/*源文件名*/args0. f=fdl ;/*管道文件写端*/argsl. fname=argv2 ;/* 目标文件名*/argsl. f=fdO ;/*管道文件读端*/pthread_create (&t, NULL, pwrite3575, (void *)&args0) 创建了线 程,写管道*/pread3575(void *)&argsl) ;/*主线程调用,读管道*/ pthread_join(t, NULL) ;/*等待写线程结束*/close_simulate3575(fd);read_simu

18、late3575write_simulate3575printf(nnumof二 %dnz/, numread_simulate3575);printf(numof二 %dnz/, 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

19、13:07rwr-r- 1 john john 84835 2012-07-11 13:22rwr-r- 1 john john 73728 2012-12-17 16:48rwr-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工总用量5163J

20、ohn1John1John1John1John1JohnJohnJohnJohnJohnJohnJohn503686495441208396984835737282012-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-x1Jo

21、hnJohn2083969-rwxr-xr-x1JohnJohn84835-rwxr-xr-x1JohnJohn73728-rwxr-xr-x1JohnJohn19924rwri*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

22、:562012-12-1722:532012-12-1722:552012-12-1722:51copyl2012-12-1722:56copy22012-12-1722:49stal2012-12-1722:57sta22012-12-1722:50tc2012-12-1722:46start 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 lockpi

23、pe is empty , nothing to outputweak up the write thread , make the pipe writableread thread is blocked and under the statement of waiting(filepipe)input data from the original file into pipe(filememory)input data in original file into memory *caSecceed (filememorv) input data in original file into m

24、emory *ca(memorypipe)input data in memory *ca into pipeadd the lockrelease the lockSecceed (memorypipe)input data in memory *ca into pipeSecceed (filepipe)input data from the original file into pipeweak the write thread , make the pipe writablerelease the lockSecceed (pipememory) output data from th

25、e pipe into memory *ca(memoryfile)output data in memory *ca into fileSecceed (memoryfile)output data from the pipe into new file(pipememory)output data from pipe into memory *caadd the lockpipe is empty , nothing to outputthe write point is closed, the copy of the file is finishedSecceed (pipefile)output data from the pipe into new filestart to close the pipe you have createdsecceed close the pipe num of readp 二 2num of writep 二 1

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1